Giter Site home page Giter Site logo

Multiple database connections about nestjs HOT 14 CLOSED

mikro-orm avatar mikro-orm commented on September 2, 2024 1
Multiple database connections

from nestjs.

Comments (14)

EduVencovsky avatar EduVencovsky commented on September 2, 2024 9

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?

from nestjs.

tsangste avatar tsangste commented on September 2, 2024 7

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

from nestjs.

B4nan avatar B4nan commented on September 2, 2024 3

This will be caused by how the nest adapter registers the EM and repositories - both of your ORM instances will use the same tokens in the DI. You should be able to register things yourself manually.

Also you will need to handle the request context manually and give your ORM instances distinct names. See the tests:

https://github.com/mikro-orm/mikro-orm/blob/master/tests/RequestContext.test.ts#L64

We can definitly support this out of box, there will have to be some @InjectEntityManager(name) decorator, and the repository decorator will also need to have a name parameter. The adapter will then register all instances prefixed with the ORM instance name.

from nestjs.

B4nan avatar B4nan commented on September 2, 2024 2

Or actually this is already implemented, almost forgot about that - you can use MIKRO_ORM_CLI env var which will add the CLI config path.

You can also use different names for this file, simply rename it in the configPaths array your in package.json. You can also use MIKRO_ORM_CLI environment variable with the path to override configPaths value.

https://mikro-orm.io/docs/installation/#setting-up-the-commandline-tool

from nestjs.

AaronPerezPerez avatar AaronPerezPerez commented on September 2, 2024 1

Hi again, what we finally did was to provide two different MikroORM instances instead of using the MikroOrmModule.forRoot() and then inject them in our repositories.
It looks like this:

@Global()
@Module({})
export class MikroOrmSwitcherModule {
  static init({ disable }: { disable: boolean }): DynamicModule {
    if (disable) {
      return {
        module: MikroOrmSwitcherModule,
      };
    }

    return {
      module: MikroOrmSwitcherModule,
      providers: [
        {
          provide: MIKRO_ORM_MONGODB_TOKEN,
          useValue: MikroORM.init(mikroOrmMongoConfig),
        },
        {
          provide: MIKRO_ORM_POSTGRESQL_TOKEN,
          useValue: MikroORM.init(mikroOrmPostgresConfig),
        },
      ],
      exports: [MIKRO_ORM_MONGODB_TOKEN, MIKRO_ORM_POSTGRESQL_TOKEN],
    };
  }
}

And this is the repository contructor:

  constructor(
    @Inject(MIKRO_ORM_POSTGRESQL_TOKEN) private readonly orm: MikroORM<PostgreSqlDriver>,
  ) {
    this.ledgerRepository = this.orm.em.getRepository(LedgerEntity);
  }

So we didn't need to finally use RequestContext at all.
We thought that this may be useful for you, so we wanted to share it!

from nestjs.

B4nan avatar B4nan commented on September 2, 2024 1

For reference, v6 also adds a --config CLI option:

mikro-orm/mikro-orm#3924

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

You still need to register the request context yourself, as it was done automatically for you via MikroOrmModule.forRoot() that is no longer used.

Each request needs to have its own context, using global one will cause side effects and memory leaks.

https://mikro-orm.io/docs/identity-map/#why-is-request-context-needed

from nestjs.

jasonwilson avatar jasonwilson commented on September 2, 2024

@B4nan thanks for referencing this issue was able to setup a similar environment splitting out each database as a separate orm instance.

We rely in the mikro-orm cli which looks for mikro-orm.config.ts. Is there a a way to dynamically specify the configuration or connection in the cli? If not might be something to consider for v5.

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

You could override most of the config option via env vars.

https://mikro-orm.io/docs/configuration/#using-environment-variables

from nestjs.

jasonwilson avatar jasonwilson commented on September 2, 2024

This would work as a "one off" for running commands but there's no way to inject or share .env configuration into the app when using multiple connections.

Another option would be to specify the mikro-orm.config.ts path via environment variable. I didn't see that option in the list, let me know if I missed it.

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

Nope, that's not possible.

You can either wait for #545 or of course you could send a PR right ahead :]

If nobody contributes, will probably do it before v5 is out myself.

from nestjs.

jasonwilson avatar jasonwilson commented on September 2, 2024

I'm happy to open a PR for specifying orm config via env variable if you'll accept it.

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

Sure we can start with that, I'd also add a CLI option for this ideally.

Just keep in mind that master is already v5 and I am not planning to backport features to v4, so if you will want to benefit from that, you will need to switch to v5 dev version.

from nestjs.

gianlazz avatar gianlazz commented on September 2, 2024

I also have this as a required use case in the process of migrating from typeorm on an existing project where I had two databases supported. Using MIKRO_ORM_CLI appears to be working for me 🎉

For example, I have a mikro-orm.sqlite.config.js & mikro-orm.postgres.config.js file respectively for each. Then use commands as such:

$ MIKRO_ORM_CLI=$PWD/mikro-orm.sqlite.config.js mikro-orm debug
# or
$ MIKRO_ORM_CLI=$PWD/mikro-orm.postgres.config.js mikro-orm debug

from nestjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.