nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

Cannot get two database connections working at the same time using ormconfig.json #2692

Closed pxr64 closed 5 years ago

pxr64 commented 5 years ago

Bug Report

Current behaviour

I have the following ormconfig.json:

[
    {
        "name": "default",
        "type": "mariadb",
        "port": 3306,
        "username": "root",
        "password": "",
        "database": "db1",
        "host": "localhost",
        "bigNumberStrings": false,
        "supportBigNumbers": true,
        "logging": [
            "error"
        ],
        "entities": [
            "src/models/entities/*{.ts,.js}"
        ],
        "migrationsTableName": "migrations",
        "migrations": [
            "src/migrations/.ts"
        ],
        "cli": {
            "migrationsDir": "src/migrations",
            "entitiesDir": "src/models/entities"
        }
    },
    {
        "name": "employees",
        "type": "postgres",
        "port": 5432,
        "username": "postgres",
        "password": "",
        "database": "db1",
        "host": "localhost",
        "logging": [
            "error"
        ],
        "entities": [
            "src/workers/crypto/models/entities/*{.ts,.js}"
        ],
        "migrationsTableName": "migrations",
        "migrations": [
            "src/workers/crypto/migrations/.ts"
        ],
        "cli": {
            "migrationsDir": "src/workers/employee/migrations",
            "entitiesDir": "src/workers/employee/models/models/entities"
        }
    }
].

And the following configuration iside my modules.

@Module({
  controllers: [StaffController],
  imports: [TypeOrmModule.forRoot(), EmployeeModule]
})
export class StaffModule { }

@Module({
  imports: [ConfigsModule, TypeOrmModule.forFeature([Address,], 'employees')]
})
export class EmployeeModule { }

On startupt I get the following error: Nest can't resolve dependencies of the employees_AddressRepository (?).

Expected behavior

Nest should initialise without any issues and should be able to access the databases.

Update

I think it has something to do with the name property of the connection.

I've tried to setup my connection via forRootAsync and I get the same error when I set the connection name in the async block. My code below.


  imports: [TypeOrmModule.forRootAsync({
    imports: [ConfigsModule],
    name: 'employees',//  <-- Connection work if name is here
    useFactory: async (configService: AppConfigService) =>  (console.log(__dirname) as any) || ({
      host: configService.get(DB_HOST'),
      //name: 'crypto',  <-- Does not work  if name is here
      port: +configService.get('DB_PORT'),
      username: configService.get('DB_USERNAME'),
      password: configService.get('DB_PASSWORD'),
      entities: [__dirname + '/models/entities/*{.ts,.js}'],
      database: configService.get('DB_DATABASE'),
      migrationsTableName: 'migrations',
      migrations: [__dirname + '/models/migrations/*.ts'],
      migrationsRun: process.env.NODE_ENV === 'production',
      type: 'postgres'
    } as TypeOrmModuleOptions),
    inject: [AppConfigService],
  }) ]
sanderboom commented 5 years ago

I'm having a similar error. Using multiple connections work fine until I want to use TypeOrm entity-features (so anything other than raw .query's). As soon as I include:

  imports:     [
    TypeOrmModule.forFeature([MyEntity], 'connection2'),
  ],

in the relevant module, Nest returns the error 'Nest can't resolve dependencies of the connection2Connection_MyEntityRepository (?)'.

Changing the positon of the name property does not work. BTW: I'm not using ormconfig.json, but:

TypeOrmModule.forRootAsync({
      imports:    [ConfigModule],
      useFactory: async (configService: ConfigService) => ({...})
});

TIA.

Edit:

Using the second connection works as intended when using TypeOrmModule.forRoot() and supplying the connection values hardcoded.

kamilmysliwiec commented 5 years ago

Duplicate of https://github.com/nestjs/typeorm/issues/66

saarw commented 5 years ago

Came up with this workaround to create two connections from ormconfig.json

TypeOrmModule.forRootAsync({name: 'secondConnection', useFactory: () => {
        const options = new ConnectionOptionsReader().all();
        return options.then(opts => {
          return opts[0]
        });
    }})
lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.