Skip to content
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

Open
conspireagency opened this issue Dec 1, 2022 · 3 comments

Comments

@conspireagency
Copy link

conspireagency commented Dec 1, 2022

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:

  1. How to have the MikroOrm instance in the main project properly work with entities imported from the Custom Module

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).

Screen Shot 2022-12-01 at 12 35 27 AM
Screen Shot 2022-12-01 at 12 48 00 AM
Screen Shot 2022-12-01 at 12 39 04 AM

  1. How to share / inject the MikroOrm instance from the main project into the Custom Module so the services have access to em/orm instance?

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.

Screen Shot 2022-12-01 at 12 42 07 AM

@conspireagency
Copy link
Author

Okay, so I was able to solve the:

  1. Entities/Migration issue by using all class references instead of mixing globs with class references. So instead of the config looking like below it's all class references. Kind of odd because on bootstrap below finds all the entities properly, both those in the glob and class reference.... but migration doesn't. Looked into the packages a bit, but couldn't figure it out why this odd behavior.
    entities: ['dist/src/entities/*.entity.{js,ts}', Product, Variant]

  2. Using a module imported via node_modules/npm, by treating it as any old module you'd have in your project and removing the MikroOrmModule instantiation. For whatever it was throwing errors for me not being able to find orm/em before, but seems OK now.

@B4nan
Copy link
Member

B4nan commented Dec 2, 2022

I will have a closer look later, but one thing I can say right ahead: the entities and entitiesTs needs to match, you can mix paths and references, but in the OP its not matching, you have the references only in entities, not entitiesTs, hence they won't be discovered in the TS mode. You can still discover the entities by reference in TS mode too. Those two options are used exclusively, they are never combined.

https://github.com/mikro-orm/mikro-orm/blob/master/packages/core/src/metadata/MetadataDiscovery.ts#L97

@conspireagency
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants