Multiple database connections
See original GitHub issueWe currently have two MikroORM instances, one connecting to a MongoDB and another connecting to a PostgreSQL, but we are facing troubles as the configs are being confused.
We implemented it like this.
This is the module that allows us to choose if we want to use real or fake repositories on the tests suites
@Module({})
export class MikroOrmSwitcherModule {
static init({ disable }: { disable: boolean }): DynamicModule {
if (disable) {
return {
module: MikroOrmSwitcherModule,
};
}
return {
module: MikroOrmSwitcherModule,
imports: [
MikroOrmModule.forRoot(mikroOrmPostgresConfig),
MikroOrmModule.forRoot(mikroOrmMongoConfig),
],
};
}
}
This is the mikro-orm-mongo.config.ts
:
const mikroOrmMongoConfig: Options = {
type: 'mongo',
contextName: 'mongoConfig',
name: 'mongoDB',
host: config.transactionsDatabase.host,
port: config.transactionsDatabase.port,
user: config.transactionsDatabase.username,
password: config.transactionsDatabase.password,
dbName: config.transactionsDatabase.database,
entities: [CustomerOrderEntity, CancelationEntity, OrderLineEntity, LocationEntity],
debug: true,
metadataProvider: TsMorphMetadataProvider,
};
export default mikroOrmMongoConfig;
This is the mikro-orm-postgres.config.ts
:
const mikroOrmPostgresConfig: Options = {
type: 'postgresql',
contextName: 'postgreSQLConfig',
name: 'postgreSQL',
host: config.ledgersDatabase.host,
port: config.ledgersDatabase.port,
user: config.ledgersDatabase.username,
password: config.ledgersDatabase.password,
dbName: config.ledgersDatabase.database,
entities: [LedgerEntity],
migrations: {
path: 'src/migrations',
},
debug: true,
metadataProvider: TsMorphMetadataProvider,
};
export default mikroOrmPostgresConfig;
The failures we are getting are that sometimes our orm instances trys to use the PostgreSQL driver for the MongoDB repository and sometimes the opposite.
At the config files we tried to add only the entities that each instance is going to use, but that gives us the following error also:
[Nest] 24318 - 25/03/2021 11:17:27 [ExceptionHandler] Metadata for entity CustomerOrderEntity not found +0ms
We haven’t found a proper way to achieve this in the documentation. Thanks in advance!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:13 (7 by maintainers)
I tried my hand at implementing multiple database connections as this is something that would benefit us
I’m still lost on how to connect to multiple databases (In my case, I want to connect to two different postgres databases).
Is there any way to do this without any problems or it still needs a PR?