繁体   English   中英

如何在 NestJS 和 TypeORM 中生成实体文件

[英]How to generate entity files in NestJS and TypeORM

我来自 Java 背景,我们有工具可以直接从数据库生成 Java 实体( DTOsDAOsJPAs等)文件,现在正在处理NestJS项目并使用TypeORM ,我有我的表 ~ 30 表实现MySQL ,是否可以生成实体文件

您所要求的通常是 ORM 的数据库优先方法,TypeORM 本身不支持 AFAIK。

我最好的建议是实现 JSON 到 TS 模型,然后使用 TypeORM 装饰器映射所需的列。 那将是你最好的选择。

目前,您可以实现独立表,然后与其他表一起使用。 我知道这是一个漫长的过程。 但是现代 ORM 是为使用实体优先方法而设计的。

您可以使用sequelize-typescript-generator库根据模式和连接自动创建实体和模型。

从描述:

如果你已经安装了这个包,你可以全局运行它

关于命令的使用

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

命令示例:

 stg \ -D mysql \ -h localhost \ -p 3306 \ -d myDatabase \ -u myUsername \ -x myPassword \ --indices \ --case camel \ --out-dir pathofthegeneratedfiletakeplace \ --clean \

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM