简体   繁体   中英

NestJs TypeORM async configuration

I am trying to use the @nestjs/typeorm module and using the async configuration. In my app.module.ts I have the following:

@Module({
  controllers: [
    AppController,
  ],
  exports: [ConfigModule],
  imports: [
    ConfigModule,
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        console.log("config.getPostgresConfig(): ", config.getPostgresConfig());
        return {
          ...config.getPostgresConfig(),
          entities,
          synchronize: true,
          type: "postgres",
        } as PostgresConnectionOptions;
      },
    }),
  ],
  providers: [AppService],
})
export class AppModule {}

The console.log prints the correct stuff, but I constantly get this error:

2019-06-17T14:41:35.569358700Z [Nest] 45   - 06/17/2019, 2:41 PM   [NestFactory] Starting Nest application...
2019-06-17T14:39:31.277686600Z     at Object.next (/root/node_modules/tslib/tslib.js:114:57)
2019-06-17T14:41:35.740192700Z [Nest] 45   - 06/17/2019, 2:41 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +170ms
2019-06-17T14:41:35.740666500Z Error: No connection options were found in any of configurations file.
2019-06-17T14:41:35.740704100Z     at ConnectionOptionsReader.<anonymous> (/root/src/connection/ConnectionOptionsReader.ts:41:19)

The config should be done all dynamically through the config service, I don't understand why it is asking for a config file, and why it can't connect to the database.

这是我的坏,我也在另一个模块中导入TypeOrmModule.forRoot() ,这导致了很多问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM