使用Sequelize在NestJS中自动生成模型,从现有的SQL Server数据库生成。

4

我正在使用 Nest js 开发一个 API,需要连接到一个现有的数据库。

由于存在太多无法手动管理的表格,因此在 Nest 中创建实体表格十分困难。

我正在使用 Sequelize。

是否有一种方法可以自动生成模型。 Sequelize-auto 似乎只适用于 express。我需要一些能够生成基于类的模型实体的东西。

1个回答

1
我知道解决方案,你可以使用sequelize-typescript-generator库根据模式和连接自动创建实体和模型。
从描述中可以看出:

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 \

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接