OP可能找到了解决方案。这适用于遇到此问题的其他人。
查看https://github.com/nestjs/typeorm/issues/150#issuecomment-510716686和下一条评论以获取问题的解决方案。
您无法.ts
在转译过程后加载文件。根据环境更改传递给实体的路径或将实体直接放入此数组。
我不想在每次创建新实体时都更新实体数组,所以我选择更新文件 glob 模式。
解决方案的关键:
/* replaced original two lines */
// entities: ['**/*.entity{.ts,.js}'],
// migrations: ['src/migration/*.ts'],
/* with these two lines */
entities: [path.join(__dirname, '../**/*.entity{.ts,.js}')],
migrations: [path.join(__dirname, '../migration/*{.ts,.js')],
现在npm run start:dev
还是npm run start:debug
不抛出错误。
这是完整的configuration.ts
和app.module.ts
// src/config/configuration.ts
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import path = require('path');
export default () => {
const customConfigService = new CustomConfigService(
process.env,
).ensureValues([
'DATABASE_HOST',
'DATABASE_PORT',
'DATABASE_USER',
'DATABASE_PASSWORD',
'DATABASE_NAME',
]);
return {
port: parseInt(process.env.PORT, 10) || 4000,
database: {
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT, 10) || 5432,
},
typeOrmModuleOptions: customConfigService.getTypeOrmConfig(),
};
};
// see https://medium.com/@gausmann.simon/nestjs-typeorm-and-postgresql-full-example-development-and-project-setup-working-with-database-c1a2b1b11b8f
class CustomConfigService {
constructor(private env: { [k: string]: string | undefined }) {}
private getValue(key: string, throwOnMissing = true): string {
const value = this.env[key];
if ((value === null || value === undefined) && throwOnMissing) {
throw new Error(`config error - missing env.${key}`);
}
return value;
}
public ensureValues(keys: string[]) {
keys.forEach(k => this.getValue(k, true));
return this;
}
public getPort() {
return this.getValue('PORT', true);
}
public isProduction() {
const mode = this.getValue('NODE_ENV', false);
return mode === 'production';
}
public getTypeOrmConfig(): TypeOrmModuleOptions {
return {
type: 'postgres',
host: this.getValue('DATABASE_HOST'),
port: parseInt(this.getValue('DATABASE_PORT')),
username: this.getValue('DATABASE_USER'),
password: this.getValue('DATABASE_PASSWORD'),
database: this.getValue('DATABASE_NAME'),
/* replaced original two lines */
// entities: ['**/*.entity{.ts,.js}'],
// migrations: ['src/migration/*.ts'],
/* with these two lines */
entities: [path.join(__dirname, '../**/*.entity{.ts,.js}')],
migrations: [path.join(__dirname, '../migration/*{.ts,.js')],
migrationsTableName: 'migration',
cli: {
migrationsDir: 'src/migration',
},
ssl: this.isProduction(),
};
}
}
// src/app.module.ts
// lib imports
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
// local imports
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ItemsModule } from './items/items.module';
import configuration from './config/configuration';
const envFilePath =
process.env.NODE_ENV === 'production'
? 'envs/.production.env'
: 'envs/.development.env';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath,
load: [configuration],
}),
// see https://stackoverflow.com/questions/53426486/best-practice-to-use-config-service-in-nestjs-module
// see https://github.com/nestjs/nest/issues/530#issuecomment-415690676
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const typeOrmModuleOptions = configService.get<TypeOrmModuleOptions>(
'typeOrmModuleOptions',
);
console.log(`typeOrmModuleOptions= `, typeOrmModuleOptions);
return typeOrmModuleOptions;
},
inject: [ConfigService],
}),
ItemsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}