@mikro-orm/nestjs 5.1.3 → 5.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,11 @@ export declare class MikroOrmCoreModule implements OnApplicationShutdown {
8
8
  constructor(options: MikroOrmModuleOptions, moduleRef: ModuleRef);
9
9
  static forRoot(options?: MikroOrmModuleSyncOptions): Promise<DynamicModule>;
10
10
  static forRootAsync(options: MikroOrmModuleAsyncOptions): Promise<DynamicModule>;
11
+ /**
12
+ * Tries to create the driver instance to use the actual entity manager implementation for DI symbol.
13
+ * This helps with dependency resolution issues when importing the EM from driver package (e.g. `SqlEntityManager`).
14
+ */
15
+ private static createEntityManager;
11
16
  onApplicationShutdown(): Promise<void>;
12
17
  configure(consumer: MiddlewareConsumer): void;
13
18
  private static setContextName;
@@ -59,40 +59,34 @@ let MikroOrmCoreModule = MikroOrmCoreModule_1 = class MikroOrmCoreModule {
59
59
  this.moduleRef = moduleRef;
60
60
  }
61
61
  static async forRoot(options) {
62
- const config = (!options || Object.keys(options).length === 0)
63
- ? await core_1.ConfigurationLoader.getConfiguration()
64
- : new core_1.Configuration(options);
65
- const em = config.getDriver().createEntityManager();
66
62
  const contextName = this.setContextName(options?.contextName);
67
63
  const knex = await tryRequire('@mikro-orm/knex');
68
64
  const mongo = await tryRequire('@mikro-orm/mongodb');
65
+ const em = await this.createEntityManager(options);
69
66
  return {
70
67
  module: MikroOrmCoreModule_1,
71
68
  providers: [
72
69
  { provide: mikro_orm_common_1.MIKRO_ORM_MODULE_OPTIONS, useValue: options || {} },
73
70
  (0, mikro_orm_providers_1.createMikroOrmProvider)(contextName),
74
71
  (0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, core_1.EntityManager, contextName),
75
- (0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, em.constructor, contextName),
72
+ ...(em ? [(0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, em.constructor, contextName)] : []),
76
73
  ...(knex ? [(0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, knex.EntityManager, contextName)] : []),
77
74
  ...(mongo ? [(0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, mongo.EntityManager, contextName)] : []),
78
75
  ],
79
76
  exports: [
80
77
  contextName ? (0, mikro_orm_common_1.getMikroORMToken)(contextName) : core_1.MikroORM,
81
78
  contextName ? (0, mikro_orm_common_1.getEntityManagerToken)(contextName) : core_1.EntityManager,
82
- ...(contextName ? [] : [em.constructor]),
83
- ...(knex ? (contextName ? [] : [knex.EntityManager]) : []),
84
- ...(mongo ? (contextName ? [] : [mongo.EntityManager]) : []),
79
+ ...(em && !contextName ? [em.constructor] : []),
80
+ ...(knex && !contextName ? [knex.EntityManager] : []),
81
+ ...(mongo && !contextName ? [mongo.EntityManager] : []),
85
82
  ],
86
83
  };
87
84
  }
88
85
  static async forRootAsync(options) {
89
- const config = (!options || Object.keys(options).length === 0)
90
- ? await core_1.ConfigurationLoader.getConfiguration()
91
- : new core_1.Configuration(options);
92
- const em = config.getDriver().createEntityManager();
93
86
  const contextName = this.setContextName(options?.contextName);
94
87
  const knex = await tryRequire('@mikro-orm/knex');
95
88
  const mongo = await tryRequire('@mikro-orm/mongodb');
89
+ const em = await this.createEntityManager(options);
96
90
  return {
97
91
  module: MikroOrmCoreModule_1,
98
92
  imports: options.imports || [],
@@ -101,19 +95,41 @@ let MikroOrmCoreModule = MikroOrmCoreModule_1 = class MikroOrmCoreModule {
101
95
  ...(0, mikro_orm_providers_1.createAsyncProviders)({ ...options, contextName: options.contextName }),
102
96
  (0, mikro_orm_providers_1.createMikroOrmProvider)(contextName),
103
97
  (0, mikro_orm_providers_1.createEntityManagerProvider)(options.scope, core_1.EntityManager, contextName),
104
- (0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, em.constructor, contextName),
98
+ ...(em ? [(0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, em.constructor, contextName)] : []),
105
99
  ...(knex ? [(0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, knex.EntityManager, contextName)] : []),
106
100
  ...(mongo ? [(0, mikro_orm_providers_1.createEntityManagerProvider)(options?.scope, mongo.EntityManager, contextName)] : []),
107
101
  ],
108
102
  exports: [
109
103
  contextName ? (0, mikro_orm_common_1.getMikroORMToken)(contextName) : core_1.MikroORM,
110
104
  contextName ? (0, mikro_orm_common_1.getEntityManagerToken)(contextName) : core_1.EntityManager,
111
- ...(contextName ? [] : [em.constructor]),
112
- ...(knex ? (contextName ? [] : [knex.EntityManager]) : []),
113
- ...(mongo ? (contextName ? [] : [mongo.EntityManager]) : []),
105
+ ...(em && !contextName ? [em.constructor] : []),
106
+ ...(knex && !contextName ? [knex.EntityManager] : []),
107
+ ...(mongo && !contextName ? [mongo.EntityManager] : []),
114
108
  ],
115
109
  };
116
110
  }
111
+ /**
112
+ * Tries to create the driver instance to use the actual entity manager implementation for DI symbol.
113
+ * This helps with dependency resolution issues when importing the EM from driver package (e.g. `SqlEntityManager`).
114
+ */
115
+ static async createEntityManager(options) {
116
+ if (options?.contextName) {
117
+ return undefined;
118
+ }
119
+ try {
120
+ if (!options || Object.keys(options).length === 0) {
121
+ const config = await core_1.ConfigurationLoader.getConfiguration(false);
122
+ return config.getDriver().createEntityManager();
123
+ }
124
+ if ('useFactory' in options) {
125
+ const config = new core_1.Configuration(options.useFactory(), false);
126
+ return config.getDriver().createEntityManager();
127
+ }
128
+ }
129
+ catch {
130
+ // ignore
131
+ }
132
+ }
117
133
  async onApplicationShutdown() {
118
134
  const token = this.options.contextName ? (0, mikro_orm_common_1.getMikroORMToken)(this.options.contextName) : core_1.MikroORM;
119
135
  const orm = this.moduleRef.get(token);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/nestjs",
3
- "version": "5.1.3",
3
+ "version": "5.1.5",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Martin Adamek",
@@ -40,7 +40,7 @@
40
40
  "lint": "eslint src/**/*.ts"
41
41
  },
42
42
  "peerDependencies": {
43
- "@mikro-orm/core": "^5.0.0||^6.0.0-dev.0",
43
+ "@mikro-orm/core": "^5.0.0 || ^6.0.0-dev.0",
44
44
  "@nestjs/common": "^8.0.0 || ^9.0.0",
45
45
  "@nestjs/core": "^8.0.0 || ^9.0.0"
46
46
  },
@@ -102,8 +102,7 @@
102
102
  ]
103
103
  },
104
104
  "engines": {
105
- "node": ">= 14.0.0",
106
- "yarn": ">=3.2.0"
105
+ "node": ">= 14.0.0"
107
106
  },
108
107
  "packageManager": "yarn@3.3.0"
109
108
  }
package/typings.d.ts CHANGED
@@ -37,5 +37,5 @@ export interface MikroOrmModuleAsyncOptions<D extends IDatabaseDriver = IDatabas
37
37
  useFactory?: (...args: any[]) => Promise<Omit<MikroOrmModuleOptions<D>, 'contextName'>> | Omit<MikroOrmModuleOptions<D>, 'contextName'>;
38
38
  inject?: any[];
39
39
  }
40
- export declare type EntityName<T extends AnyEntity<T>> = CoreEntityName<T> | EntitySchema<any>;
40
+ export declare type EntityName<T extends AnyEntity<T>> = CoreEntityName<T> | EntitySchema;
41
41
  export {};