question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple database connections

See original GitHub issue

We 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
tsangstecommented, Feb 19, 2022

I tried my hand at implementing multiple database connections as this is something that would benefit us

6reactions
EduVencovskycommented, Nov 17, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple Database Connections - IBM
To enable multiple database connections for an archive, extract, or delete process, set Maximum Database Connections in Product Options to a value greater ......
Read more >
Why is it bad practice to make multiple database connections ...
Database connections are a limited resource. Some DBs have a very low connection limit, and wasting connections is a major problem.
Read more >
How to Integrate Multiple Databases in a Web App - Budibase
The first step is to connect your databases to the Budibase builder. Of course, you might have several data sources, but for simplicity's...
Read more >
Connecting to Multiple Databases from a Single Tool - Navicat
Many database management and development tools support multiple connections to homogeneous databases, i.e., where they are all of the same type ...
Read more >
Multiple DB Connections in Laravel
Here is how to run more than one database connection. This article is about using databases with different data, not necessary for load...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found