3

I am working on an API using Nest js that has to connect to an existing database.

There are so many tables that cannot be manage manually for creating the entity tables in Nest.

I am using sequelize.

Is there a way that I can auto generate models. Sequelize-auto seems to only work well for express. I need something that can generate class based model entities.

Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42

1 Answers1

1

I get the solution, you can use sequelize-typescript-generator library to create the entity and models automatically based on schema and connection.

From the description:

You can run this globally if you already install the package

For the usage of the command

-h, --host                  Database IP/hostname                      
-p, --port                  Database port. Defaults:
                            - MySQL/MariaDB: 3306
                            - Postgres: 5432
                            - MSSQL: 1433                             
-d, --database              Database name                             
-s, --schema                Schema name (Postgres only). Default:
                            - public                                  
-D, --dialect               Dialect:
                            - postgres
                            - mysql
                            - mariadb
                            - sqlite
                            - mssql                        
-u, --username              Database username                         
-x, --password              Database password                         
-t, --tables                Comma-separated names of tables to process
-T, --skip-tables           Comma-separated names of tables to skip   
-i, --indices               Include index annotations in the generated models
                                                                    
-o, --out-dir               Output directory. Default:
                            - output-models                           
-C, --case                  Transform tables and fields names
                            with one of the following cases:
                            - underscore
                            - camel
                            - upper
                            - lower
                            - pascal
                            - const
                            You can also specify a different
                            case for model and columns using
                            the following format:
                            <model case>:<column case>
-S, --storage               SQLite storage. Default:
                            - memory                               
-L, --lint-file             ES Lint file path                        
-l, --ssl                   Enable SSL                               
-r, --protocol              Protocol used: Default:
                            - tcp                                     
-a, --associations-file     Associations file path                    
-g, --logs                  Enable Sequelize logs                    
-n, --dialect-options       Dialect native options passed as json string.
-f, --dialect-options-file  Dialect native options passed as json file path.                             
-R, --no-strict             Disable strict typescript class declaration. 

Example of the command:

stg \
-D mysql \
-h localhost \
-p 3306 \
-d myDatabase \ 
-u myUsername \
-x myPassword \
--indices \
--case camel \
--out-dir pathofthegeneratedfiletakeplace \
--clean \
Gagantous
  • 432
  • 6
  • 29
  • 69