Generates models for TypeORM from existing databases. Supported db engines:
- Microsoft SQL Server
- PostgreSQL
- MySQL
- MariaDB
- Oracle Database
- SQLite
2021-02-28:
-
added
--generateTinyintTransformer
to convert tinyint(1) <=> boolean when reading and writing to the database and also mapstinyint(1) <signed|unsigned>
to boolean -
added
--generateBigintTransformer
to convert bigint <=> number when reading and writing to the database and also mapbigint <signed|unsigned>
to number -
added
--cascade
to add cascade option to property definitions based on user input -
added
--generateValidators
to add class-validator constraints to property definitions based on column properties -
added
--smartStrictMode
to add optional chaining (?.) to property type that are nullable or has default or is auto-increment or foreign key -
added
--noTypeorm
suppress typeorm imports and decorators. for use with--generateValidators
-
added comment at top of class that shows source schema.table_name and options for generating classes
-
rename .tomg-config to .toml-config.json
-
WARNING: Use
--generateBigintTransformer
with caution since this can cause 32-bit/53-bit overflows when coverting to/from 64-bit integers. -
NOTE: TypeORM tranformers cannot be used on auto-increment columns therefore it is not generated for identity columns.
Typeorm-model-generator comes with preinstalled driver for each supported db(except for oracle). However if you want to use it as a dev-dependency you may want to install your db driver manually to reduce dependency footprint, reduce time spent in the CI. In such case you can use version without preinstalled db drivers - npm i typeorm-model-generator@no-engines
.
To install module globally simply type npm i -g typeorm-model-generator
in your console.
Thanks to npx you can use npm modules without polluting global installs. So nothing to do here :)
To use
npx
you need to use npm at version at least 5.2.0. Try updating your npm bynpm i -g npm
All database drivers except oracle are installed by default. To use typeorm-model-generator with oracle database you need to install driver with npm i oracledb
and configure oracle install client on your machine.
There are two way to use this utility:
- Use step by step wizard which will guide you though the process - just type
npx typeorm-model-generator
in your console. - Provide all parameters through command line(examples below)
Use npx typeorm-model-generator --help
to see all available parameters with their descriptions. Some basic parameters below:
Usage: typeorm-model-generator -h <host> -d <database> -p [port] -u <user> -x
[password] -e [engine]
Options:
--help Show help [boolean]
--version Show version number [boolean]
-h, --host IP address/Hostname for database server [string] [default: "127.0.0.1"]
-d, --database Database name(or path for sqlite). You can
pass multiple values separated by comma. [string] [required] [default: ""]
-u, --user Username for database server [string] [default: ""]
-x, --pass Password for database server [string] [default: ""]
-p, --port Port number for database server [number] [default: 0]
-e, --engine Database engine [required] [choices: "mssql", "postgres", "mysql", "mariadb", "oracle", "sqlite"]
-o, --output Where to place generated models [default: "./output"]
-s, --schema Schema name to create model from. Only for
mssql and postgres. You can pass multiple
values separated by comma eg. -s
scheme1,scheme2,scheme3 [string] [default: ""]
-i, --instance Named instance to create model from. Only for mssql.[string]
--ssl Use SSL connection [boolean] [default: false]
--noConfig Do not create tsconfig.json and ormconfig.json [boolean] [default: false]
--cf, --case-file Convert file names to specified case [choices: "pascal", "param", "camel", "none"] [default: "pascal"]
--ce, --case-entity Convert class names to specified case [choices: "pascal", "camel", "none"] [default: "pascal"]
--cp, --case-property Convert property names to specified case [choices: "pascal", "camel", "snake", "none"] [default: "camel"]
--eol Force EOL to be LF or CRLF [choices: "LF", "CRLF"] [default: "LF"]
--pv, --property-visibility Defines which visibility should have
the generated property [choices: "public", "protected", "private", "none"] [default: "none"]
--lazy Generate lazy relations [boolean] [default: false]
--cascade (ONLY MySQL) Generate cascade relations
(eg. --cascade true) (eg. --cascade
'insert, update, remove, soft-remove, recover') [boolean] [default: false]
-a, --active-record Use ActiveRecord syntax for generated models [boolean] [default: false]
--namingStrategy Use custom naming strategy [string] [default: ""]
--relationIds Generate RelationId fields [boolean] [default: false]
--skipSchema Omits schema identifier in generated entities [boolean] [default: false]
--generateConstructor Generate constructor allowing partial initialization[boolean] [default: false]
--generateTinyintTransformer (ONLY MySQL) Generate transformer that
converts boolean <=> tinyint(1) <signed|usigned>. [boolean] [default: false]
--generateBigintTransformer (ONLY MySQL - NOT FOR AUTO-INCREMENT) Generate
transformer that converts number <=> bigint
<signed|unsigned>. Use with caution since this can
cause interger overflows. Typeorm does not support
transformers on auto-increment columns. [boolean] [default: false]
--generateValidators (ONLY MySQL) Generate class-validator constraints
based on column properties. [boolean] [default: false]
--smartStrictMode (ONLY MySQL) add optional chaining to property type
if nullable or has default or is auto-increment or
foreign key [boolean] [default: false]
--noTypeorm (ONLY MySQL) Omit typeorm imports and decorators
when writing out typescript files. For use with
--generateValidators [boolean] [default: false]
--disablePluralization Disable pluralization of OneToMany,
ManyToMany relation names [boolean] [default: false]
--skipTables Skip schema generation for specific tables.
You can pass multiple values separated by comma [string] [default: ""]
--tables Generate specific tables. You can pass
multiple values separated by comma [string] [default: ""]
--strictMode Mark fields as optional(?) or non-null(!) [choices: "none", "?", "!"] [default: "none"]
--index Generate index file [boolean] [default: false]
--defaultExport Generate index file [boolean] [default: false]
- Creating model from local MSSQL database
- Global module
typeorm-model-generator -h localhost -d tempdb -u sa -x !Passw0rd -e mssql -o .
- Npx Way
npx typeorm-model-generator -h localhost -d tempdb -u sa -x !Passw0rd -e mssql -o .
- Global module
- Creating model from local Postgres database, public schema with ssl connection
- Global module
typeorm-model-generator -h localhost -d postgres -u postgres -x !Passw0rd -e postgres -o . -s public --ssl
- Npx Way
npx typeorm-model-generator -h localhost -d postgres -u postgres -x !Passw0rd -e postgres -o . -s public --ssl
- Global module
- Creating model from SQLite database
- Global module
typeorm-model-generator -d "Z:\sqlite.db" -e sqlite -o .
- Npx Way
npx typeorm-model-generator -d "Z:\sqlite.db" -e sqlite -o .
- Global module
Please take a look at few workflows which might help you with deciding how you're gonna use typeorm-model-generator.
If you want to generate custom names for properties in generated entities you need to use custom naming strategy. You need to create your own version of NamingStrategy and pass it as command parameter.
typeorm-model-generator -d typeorm_mg --namingStrategy=./NamingStrategy -e sqlite -db /tmp/sqliteto.db