我正在使用nestjs typeorm和nx workspace,并使用一个webpack编译器
db.config.ts
export const dataSourceOptions: DataSourceOptions = {
type: 'postgres',
host: process.env.DATABASE_HOST,
port: +process.env.DATABASE_PORT,
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
entities: [
__dirname + 'src/**/*.entity{.ts,.js}',
],
migrations: [
__dirname + 'src/migrations/*.migration{.ts,.js}',
],
extra: {
charset: 'utf8mb4_unicode_ci',
},
migrationsTableName: 'migrations_typeorm',
migrationsRun: Boolean(process.env.DATABASE_MIGRATION_SYNC),
synchronize: Boolean(process.env.DATABASE_SYNC),
logging: Boolean(process.env.DATABASE_LOGGING)
}
const dataSource = new DataSource(dataSourceOptions);
export default dataSource;
Scripts
"typeorm": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"command": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"
}
},
"db-migration-gen": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"command": "nx run project:typeorm -- migration:generate -d apps/project/src/config/db.config.ts apps/project/src/migrations/{args.name}"
}
},
"db-migration-create": {
"dependsOn": [
{
"target": "build",
"projects": "self"
}
],
"executor": "nx:run-commands",
"outputs": [],
"options": {
"command": "nx run project:typeorm -- migration:create apps/project/src/migrations/{args.name}"
}
},
"db-migration-run": {
"executor": "nx:run-commands",
"outputs": [],
"options": {
"commands": ["rimraf dist", "nx run project:typeorm -- -d apps/project/src/config/db.config.ts migration:run"]
}
},
在这里,db-migration-create和db-migration-run运行正常,但在db-migration-gen的情况下,我得到下面提到的错误
enter image description here
我尝试了各种修复与typeorm文档,但我只能修复创建和运行,但生成仍然抛出错误
1条答案
按热度按时间k4emjkb11#
我认为你的问题来自regex
'src/**/*.entity{.ts,.js}'
,因为webpack你应该构建到“dist”文件夹。你必须考虑两种配置,一种用于开发模式,另一种用于生产模式。我为这个问题提出了两个解决方案:
entities: [User, Post, ...]
,而不是使用正则表达式。(我更喜欢这个)我认为你的命令
db-migration-run
正在运行,但不要运行你的迁移,并认为它是空的,或者它在src/
中寻找迁移,这在很多情况下不是你想要的。你应该遵循常见问题解答。根据你的配置,使用typeorm和webpack可能会出现错误“Metadata not found”。但这将是另一个问题。希望能有所帮助。