Comments (14)
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.
I tried my hand at implementing multiple database connections as this is something that would benefit us
from nestjs.
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.
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 inpackage.json
. You can also useMIKRO_ORM_CLI
environment variable with the path to overrideconfigPaths
value.
https://mikro-orm.io/docs/installation/#setting-up-the-commandline-tool
from nestjs.
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.
For reference, v6 also adds a --config
CLI option:
from nestjs.
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.
@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.
You could override most of the config option via env vars.
https://mikro-orm.io/docs/configuration/#using-environment-variables
from nestjs.
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.
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.
I'm happy to open a PR for specifying orm config via env variable if you'll accept it.
from nestjs.
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.
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)
- Error when trying to query an entity from the repository HOT 3
- How can I take some actions after it successfully connects or fails? HOT 1
- Cannot persist an entity with a 1-to-1 reference to another entity HOT 5
- Cannot persist Read-Only association HOT 3
- Cannot get `@UseRequestContext` to work in a REQUEST scoped BullMQ processor HOT 4
- Populating Nested Field returns the wrong number of associated entities HOT 5
- Relations not working in nestjs HOT 6
- Issue when running tests after upgrading to 5.2.2
- Bump version, compatible with MikroORM V6 HOT 6
- Nestjs, mikro-orm and postgres can't generate table automatically HOT 2
- vitest + mikroorm collection error HOT 4
- findByCursor method not working on sqlite HOT 1
- Testing with multiple testing modules in NestJS and Jest leads to an error during metadata discovery HOT 2
- Issues running Mikro-ORM with NestJS on Vercel platform HOT 1
- MikroOrmMiddleware not found in e2e tests
- feat: add Transactional Decorator HOT 1
- How to listen to connection status?
- Nest cannot export a provider/module that is not a part of the currently processed module (MikroOrmCoreModule). Please verify whether the exported MongoMikroORM is available in this particular context.
- Allow specifying .fork() options for Request-Scoped EntityManager in the MikroOrmModule
- Using multiple databases hustle
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs.