GithubHelp home page GithubHelp logo

arcanereinz / typeorm-model-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kononnable/typeorm-model-generator

0.0 0.0 0.0 2.49 MB

Generates models for TypeORM from existing database.

License: MIT License

TypeScript 99.21% JavaScript 0.79%

typeorm-model-generator's Introduction

typeorm-model-generator

npm version codecov

Generates models for TypeORM from existing databases. Supported db engines:

  • Microsoft SQL Server
  • PostgreSQL
  • MySQL
  • MariaDB
  • Oracle Database
  • SQLite

Installation

Changelog

2021-02-28:

  • added --generateTinyintTransformer to convert tinyint(1) <=> boolean when reading and writing to the database and also maps tinyint(1) <signed|unsigned> to boolean

  • added --generateBigintTransformer to convert bigint <=> number when reading and writing to the database and also map bigint <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.

Versions

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.

Global module

To install module globally simply type npm i -g typeorm-model-generator in your console.

Npx way

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 by npm i -g npm

Database drivers

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.

Usage

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]

Examples

  • 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 .
      
  • 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
      
  • 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 .
      

Use Cases

Please take a look at few workflows which might help you with deciding how you're gonna use typeorm-model-generator.

Naming strategy

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

typeorm-model-generator's People

Contributors

kononnable avatar greenkeeper[bot] avatar boenrobot avatar pc3b3r avatar greenkeeperio-bot avatar nangchan avatar ricki-bumbledev avatar dependabot[bot] avatar iteufel avatar arimus avatar kevinguanchedarias avatar tienleok avatar miltonhowe avatar ngty avatar ghostmatt avatar madbbb avatar 0xflotus avatar bertyhell avatar catsmiaow avatar chlorine avatar dustinsmith1024 avatar jasonvideoblocks avatar livan-pro avatar maia-everett avatar moshensky avatar rmachado avatar timonvs avatar neko-neko avatar drsugiaichigo00781vn avatar adrielcodeco avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.