我来自Java背景,我们有直接从数据库生成Java实体(DTOs
、DAOs
、JPAs
等(文件的工具,现在正在进行NestJS
项目,使用TypeORM
,我有我的表~30表在MySQL
上实现,有没有生成实体文件
您所要求的通常是ORM的数据库优先方法,TypeORM不支持本机AFAIK。
我最好的建议是实现一个JSON到TS的模型,然后用TypeORM装饰器映射所需的列。这将是你最好的选择。
目前,您可以实现独立的表,然后与其他表一起使用。我知道这是一个漫长的过程。但现代ORM是用实体优先的方法工作的。
您可以使用sequelize typescript生成器库根据模式和连接自动创建实体和模型。
来自描述:
<blockquote\如果您已经安装了包,则可以全局运行
用于命令的使用
-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
>//app.module.ts
import { Module } from '@nestjs/common';
import { UserModule } from './user.resources/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from './user/entity/user.entity';
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306, // By default
username: 'YOUR_DB_USER',
password: 'YOUR_DB_PASSWORD',
database: 'YOUR_DB_NAME',
entities: [UserEntity],
synchronize: true, // In production use false
}),
],
controllers: [],
providers: [],
exports: [],
})
export class AppModule {}
// Create entity manually like this...
// user/entity/user.entity'
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ name: 'User' })
export class UserEntity {
@PrimaryGeneratedColumn({ name: 'id', unsigned: true })
id: number;
@Column({ unique: true, nullable: false })
name: string;
@Column({ unique: true, nullable: false, length: 50 })
email: string;
@Column({ enum: ['USER', 'ADMIN'], default: 'USER' })
role: 'USER' | 'ADMIN';
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
createAt: Date;
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updateAt: Date;
@Column({ unique: true, nullable: false, length: 255 })
password: string;
}