Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pxr64 opened this issue Aug 1, 2019 · 4 comments
Labels
needs triage This issue has not been looked into

Comments

@pxr64
Copy link

pxr64 commented Aug 1, 2019

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],
  }) ]
@pxr64 pxr64 added the needs triage This issue has not been looked into label Aug 1, 2019
@pxr64 pxr64 changed the title Cannot get 2 typeorm coneections working at the same time using ormconfig Cannot get two database connections working at the same time using ormconfig.json Aug 1, 2019
@sanderboom
Copy link

sanderboom commented Aug 1, 2019

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
Copy link
Member

Duplicate of nestjs/typeorm#66

@kamilmysliwiec kamilmysliwiec marked this as a duplicate of nestjs/typeorm#66 Aug 13, 2019
@saarw
Copy link
Contributor

saarw commented Sep 14, 2019

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
Copy link

lock bot commented Dec 13, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

4 participants