-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Documentation Request] Using MikroOrm within a dynamic NestJS module #92
Comments
Okay, so I was able to solve the:
|
I will have a closer look later, but one thing I can say right ahead: the |
Whoops, you're right that screenshot is incorrect - but yes I included the references there as well. From the docs I wasn't sure since it accepts both, but makes sense since it's working when I supplied just entity references directly. |
In the interest of code reuse I'd like to create a NestJS module that I can put on NPM and import into future projects. Within the module I'd like to have things like entity definitions, as well as services that interact with the database.
While I use NestJS + MikroOrm quite a bit, definitely sure part of this is due to a lack of lower level understanding here and there... but regardless would be cool to get more information on how to reuse entities and business logic that interacts with the database via MikroOrm!
So for examples sake we have:
-A newly scaffolded NestJS project
-The custom module that we'd like to reuse as a dependency hosted on NPM like this one https://github.com/conspireagency/nestm-shopify that's a dependency of the newly scaffolded project.
So if you were to look into the project's AppModule, it might look something like this:
`
import {MikroOrmModule } from '@mikro-orm/nestjs';
import { ShopifyModule } from '@conspireagency/nestm-shopify';
@module({
imports: [
MikroOrmModule.forRoot(),
ShopifyModule
],
controllers: [],
providers: [AppService],
})
export class AppModule {}`
In attempting to do this, I ran into a couple roadblocks so far:
It looks like by importing the entities from the node module in the mikro-orm.config.ts that it sees the two entities (Product, Variant), but in trying to do something common like create a new migration after adding these entities it fails to detect that changes are required (assuming it is missing the entities).
I tried importing + injecting it like so in the main project to no avail:
AppModule.ts
MikroOrmModule.forRoot(), ShopifyModule.registerAsync({ imports: [MikroOrmModule], inject: [MikroOrmModule], })
If I change it back to below
AppModule.ts
MikroOrmModule.forRoot(), ShopifyModule
and then in the npm imported module initialize MikroOrm again the errors go away (assuming no version mismatches of the mikro orm packages in the main project and imported module), but guessing this doesn't actually work since both the main app and custom module have separate instances of MikroOrm.
The text was updated successfully, but these errors were encountered: