You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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';
In attempting to do this, I ran into a couple roadblocks so far:
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).
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:
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:
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]
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.
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.
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: