TypeOrmModule
如果我們安裝了@nestjs/typeorm
,那麼使用TypeOrmModule.forRoot():
TypeOrmModule.forRoot({
type:'mysql',
host:'localhost',
port:3306,
username:'root',
password:'123456',
database:'test',
"entities": [],
synchronize:true
}),
如果這樣子手動導入entity會很繁瑣,所以可以在Nest根目錄設置ormconfig.json文件(使用這種方式不能使用@nestjs/typeorm
配置的autoLoadEntities和retryDelay配置項):
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "test",
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true
但是,請注意,webpack不支持glob路徑,因此如果你在monorepo中構建應用程序,就不能使用它們。
因此,nest/typeorm給我們增加了autoLoadEntities配置:
TypeOrmModule.forRoot({
type:'mysql',
host:'localhost',
port:3306,
username:'root',
password:'123456',
database:'test',
autoLoadEntities: true,
synchronize:true
}),
指定了該選項後,通過forFeature()方法註冊的每個實體都將自動添加到配置對象的實體數組中。