【发布时间】:2020-09-22 17:48:22
【问题描述】:
我想用 .env 文件设置 TypeORM 的配置,但是当我尝试运行迁移脚本时遇到问题。
我做了什么:
1.在 package.json 中添加脚本
"migration:generate": "node_modules/.bin/typeorm migration:generate -n",
"migration:run": "node_modules/.bin/typeorm migration:run",
"migration:revert": "node_modules/.bin/typeorm migration:revert",
2.在app.module*中导入TypeOrmModule*
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('HOST'),
port: +configService.get<number>('PORT'),
username: configService.get('DATABASE_USERNAME'),
password: configService.get('DATABASE_PASSWORD'),
database: configService.get('DATABASE'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
})
}
)],
3.在根文件夹中创建 .env 文件
HOST=localhost
PORT=5432
DATABASE_USER=dbuser
DATABASE_PASSWORD=dbpassword
DATABASE=dbname
现在我正在尝试像这样运行迁移脚本:
npm run migration:generate -n AddUserTable
我收到这样的错误:
Error during migration generation:
Error: No connection options were found in any orm configuration files.
根据文档,它应该是一些 ormconfig.json,但它也应该与 .env 一起使用。请告诉我,我的情况有什么问题?
【问题讨论】:
标签: javascript node.js nestjs typeorm