Could not use @InjectDataSource with named databases if one connection is not named "default"
Proxxx23 opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
Seems there's a problem getting named connection if there are two connections to DB established.
In my project, I've made two separate named connections, as stands in documentation.
This is main connection:
const TypeOrmCoreModule = TypeOrmModule.forRootAsync({
imports: [ConfigModule],
name: 'MainDatabase',
inject: [DatabaseConfigToken],
useFactory(databaseConfig: DatabaseConfig): TypeOrmModuleOptions {
const ormOptions =
process.env.DEPLOY_ENV !== 'test-local' && process.env.DEPLOY_ENV !== 'test'
? coreDatabaseTypeOrmOptions
: testDatabaseTypeOrmOptions
return {
type: 'postgres',
...ormOptions,
...databaseConfig,
}
},
dataSourceFactory: async (options) => {
if (!options) {
throw new Error('Could not connect to DB - empty options')
}
return await new DataSource(options).initialize()
},
})
@Module({
imports: [TypeOrmCoreModule],
})
export class DatabaseModule {}
And this is second connection - DB used for authorization.
export const TypeOrmAuthorizationModule = TypeOrmModule.forRootAsync({
imports: [ConfigModule],
name: 'AuthorizationDatabase',
inject: [AuthorizationDatabaseConfigToken],
useFactory(databaseConfig: DatabaseConfig): TypeOrmModuleOptions {
const ormOptions =
process.env.DEPLOY_ENV !== 'test-local' && process.env.DEPLOY_ENV !== 'test'
? authorizationDatabaseStaticTypeOrmOptions
: authorizationTestDatabaseStaticTypeOrmOptions
return {
type: 'postgres',
...ormOptions,
...databaseConfig,
}
},
dataSourceFactory: async (options) => {
if (!options) {
throw new Error('Could not connect to authorization DB - empty options')
}
return await new DataSource(options).initialize()
},
})
@Module({
imports: [TypeOrmAuthorizationModule],
})
export class AuthorizationDatabaseModule {}
Then, if I use first connection this way in some module's service:
@InjectDataSource('MainDatabase') private readonly db: DataSource
It doesn't work. There will be an error saying this property could not haven been bound:
"Nest can't resolve dependencies of the ..."
Second connection works properly. And there's a bug I guess. If I remove this line from first connection:
name: 'MainDatabase'
which means it'll be named default
, and I provide NO NAME here:
@InjectDataSource() private readonly db: DataSource
It works properly. Seems one non-named connection (default) is always needed?
Minimum reproduction code
https://github.com/nestjs/typeorm/
Steps to reproduce
No response
Expected behavior
All named connections should work properly, connecting to defined database with given name.
Package version
0.3.10
NestJS version
8.4.7
Node.js version
14.16.1
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
I'd be really hard to reproduce this problem in a quick fashion with minimum. The case is simple - if you have more than one connection via TypeOrmModule.forRootAsync()
, both named (so no default
name in connections), there's something weird going on. If one connection has no name, then everything works fine...
Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project) - it shouldn't take longer than a few minutes