@mikro-orm/nestjs 7.0.2-dev.2 → 7.0.2-dev.21

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.
package/README.md CHANGED
@@ -55,17 +55,28 @@ export class AppModule {}
55
55
  ```
56
56
 
57
57
  The `forRoot()` method accepts the same configuration object as `init()` from the MikroORM package.
58
- You can also omit the parameter to use the CLI config.
58
+ Since v7, the configuration must be provided explicitly — passing the options is required
59
+ (see the [v6 to v7 upgrading guide](https://mikro-orm.io/docs/upgrading-v6-to-v7#mikroorminit-requires-options-parameter)).
60
+ If you keep your config in a separate file (e.g. `mikro-orm.config.ts`), import it and pass it in:
61
+
62
+ ```typescript
63
+ import config from './mikro-orm.config';
64
+
65
+ @Module({
66
+ imports: [MikroOrmModule.forRoot(config)],
67
+ })
68
+ export class AppModule {}
69
+ ```
59
70
 
60
71
  Afterward, the `EntityManager` will be available to inject across entire project (without importing any module elsewhere).
61
72
 
62
73
  ```ts
63
74
  @Injectable()
64
75
  export class MyService {
65
-
66
- constructor(private readonly orm: MikroORM,
67
- private readonly em: EntityManager) { }
68
-
76
+ constructor(
77
+ private readonly orm: MikroORM,
78
+ private readonly em: EntityManager,
79
+ ) {}
69
80
  }
70
81
  ```
71
82
 
@@ -103,11 +114,10 @@ In this way we can inject the `PhotoRepository` to the `PhotoService` using the
103
114
  export class PhotoService {
104
115
  constructor(
105
116
  @InjectRepository(Photo)
106
- private readonly photoRepository: EntityRepository<Photo>
117
+ private readonly photoRepository: EntityRepository<Photo>,
107
118
  ) {}
108
119
 
109
120
  // ...
110
-
111
121
  }
112
122
  ```
113
123
 
@@ -186,14 +196,12 @@ method and execute it inside the context.
186
196
  ```ts
187
197
  @Injectable()
188
198
  export class MyService {
189
-
190
- constructor(private readonly orm: MikroORM) { }
199
+ constructor(private readonly orm: MikroORM) {}
191
200
 
192
201
  @CreateRequestContext()
193
202
  async doSomething() {
194
203
  // this will be executed in a separate context
195
204
  }
196
-
197
205
  }
198
206
  ```
199
207
 
@@ -206,18 +214,15 @@ Luckily, MikroORM provides a [serialization API](https://mikro-orm.io/docs/seria
206
214
  ```typescript
207
215
  @Entity()
208
216
  export class Book {
209
-
210
- @Property({ hidden: true }) // --> Equivalent of class-transformer's `@Exclude`
217
+ @Property({ hidden: true }) // --> Equivalent of class-transformer's `@Exclude`
211
218
  hiddenField: number = Date.now();
212
219
 
213
220
  @Property({ persist: false }) // --> Will only exist in memory (and will be serialized). Similar to class-transformer's `@Expose()`
214
221
  count?: number;
215
222
 
216
- @ManyToOne({ serializer: value => value.name, serializedName: 'authorName' }) // Equivalent of class-transformer's `@Transform()`
223
+ @ManyToOne({ serializer: value => value.name, serializedName: 'authorName' }) // Equivalent of class-transformer's `@Transform()`
217
224
  author: Author;
218
-
219
225
  }
220
-
221
226
  ```
222
227
 
223
228
  ## Using `AsyncLocalStorage` for request context
@@ -264,11 +269,11 @@ import { Scope } from '@nestjs/common';
264
269
  MikroOrmModule.forRoot({
265
270
  // ...
266
271
  registerRequestContext: false, // disable automatatic middleware
267
- scope: Scope.REQUEST
272
+ scope: Scope.REQUEST,
268
273
  }),
269
274
  ],
270
275
  controllers: [AppController],
271
- providers: [AppService]
276
+ providers: [AppService],
272
277
  })
273
278
  export class AppModule {}
274
279
  ```
@@ -287,11 +292,11 @@ import { PostgreSqlDriver } from '@mikro-orm/postgresql';
287
292
  // ...
288
293
  registerRequestContext: false, // disable automatatic middleware
289
294
  }),
290
- scope: Scope.REQUEST
291
- })
295
+ scope: Scope.REQUEST,
296
+ }),
292
297
  ],
293
298
  controllers: [AppController],
294
- providers: [AppService]
299
+ providers: [AppService],
295
300
  })
296
301
  export class AppModule {}
297
302
  ```
@@ -305,7 +310,7 @@ When using custom repositories, we can get around the need for `@InjectRepositor
305
310
  decorator by naming our repositories the same way as `getRepositoryToken()` method do:
306
311
 
307
312
  ```ts
308
- export const getRepositoryToken = <T> (entity: EntityName<T>) => `${Utils.className(entity)}Repository`;
313
+ export const getRepositoryToken = <T>(entity: EntityName<T>) => `${Utils.className(entity)}Repository`;
309
314
  ```
310
315
 
311
316
  In other words, as long as we name the repository same was as the entity is called,
@@ -317,10 +322,8 @@ the Nest.js DI container.
317
322
  ```ts
318
323
  @Entity({ customRepository: () => AuthorRepository })
319
324
  export class Author {
320
-
321
325
  // to allow inference in `em.getRepository()`
322
326
  [EntityRepositoryType]?: AuthorRepository;
323
-
324
327
  }
325
328
  ```
326
329
 
@@ -328,9 +331,7 @@ export class Author {
328
331
 
329
332
  ```ts
330
333
  export class AuthorRepository extends EntityRepository<Author> {
331
-
332
334
  // your custom methods...
333
-
334
335
  }
335
336
  ```
336
337
 
@@ -340,9 +341,7 @@ return, we do not need the `@InjectRepository()` decorator anymore:
340
341
  ```ts
341
342
  @Injectable()
342
343
  export class MyService {
343
-
344
- constructor(private readonly repo: AuthorRepository) { }
345
-
344
+ constructor(private readonly repo: AuthorRepository) {}
346
345
  }
347
346
  ```
348
347
 
@@ -395,7 +394,7 @@ Since MikroORM v6.4, you can also define [multiple configurations](https://mikro
395
394
  imports: [
396
395
  // `config` exports an array of configs
397
396
  ...MikroOrmModule.forRoot(config),
398
- MikroOrmModule.forMiddleware()
397
+ MikroOrmModule.forMiddleware(),
399
398
  ],
400
399
  controllers: [AppController],
401
400
  providers: [AppService],
@@ -408,12 +407,12 @@ To access different `MikroORM`/`EntityManager` connections you have to use the n
408
407
  ```ts
409
408
  @Injectable()
410
409
  export class MyService {
411
-
412
- constructor(@InjectMikroORM('db1') private readonly orm1: MikroORM,
413
- @InjectMikroORM('db2') private readonly orm2: MikroORM,
414
- @InjectEntityManager('db1') private readonly em1: EntityManager,
415
- @InjectEntityManager('db2') private readonly em2: EntityManager) { }
416
-
410
+ constructor(
411
+ @InjectMikroORM('db1') private readonly orm1: MikroORM,
412
+ @InjectMikroORM('db2') private readonly orm2: MikroORM,
413
+ @InjectEntityManager('db1') private readonly em1: EntityManager,
414
+ @InjectEntityManager('db2') private readonly em2: EntityManager,
415
+ ) {}
417
416
  }
418
417
  ```
419
418
 
@@ -437,11 +436,10 @@ When using the `@InjectRepository` decorator you will also need to pass the `con
437
436
  export class PhotoService {
438
437
  constructor(
439
438
  @InjectRepository(Photo, 'db1')
440
- private readonly photoRepository: EntityRepository<Photo>
439
+ private readonly photoRepository: EntityRepository<Photo>,
441
440
  ) {}
442
441
 
443
442
  // ...
444
-
445
443
  }
446
444
  ```
447
445
 
@@ -450,9 +448,7 @@ You can use the `@InjectMikroORMs` decorator to get all registered MikroORM inst
450
448
  ```typescript
451
449
  @Injectable()
452
450
  export class MyService {
453
-
454
- constructor(@InjectMikroORMs() private readonly orms: MikroORM[]) { }
455
-
451
+ constructor(@InjectMikroORMs() private readonly orms: MikroORM[]) {}
456
452
  }
457
453
  ```
458
454
 
@@ -62,7 +62,9 @@ let MikroOrmCoreModule = MikroOrmCoreModule_1 = class MikroOrmCoreModule {
62
62
  createMikroOrmProvider(contextName),
63
63
  ...(em ? [createMikroOrmProvider(contextName, em.getDriver().getORMClass())] : []),
64
64
  createEntityManagerProvider(options.scope, EntityManager, contextName, options.forkOptions),
65
- ...(em ? [createEntityManagerProvider(options.scope, em.constructor, contextName, options.forkOptions)] : []),
65
+ ...(em
66
+ ? [createEntityManagerProvider(options.scope, em.constructor, contextName, options.forkOptions)]
67
+ : []),
66
68
  ],
67
69
  exports: [
68
70
  contextName ? getMikroORMToken(contextName) : MikroORM,
@@ -1,4 +1,4 @@
1
- import { EntityManager, EntitySchema, MetadataStorage, MikroORM } from '@mikro-orm/core';
1
+ import { EntityManager, EntitySchema, MetadataStorage, MikroORM, } from '@mikro-orm/core';
2
2
  import { Scope } from '@nestjs/common';
3
3
  import { MIKRO_ORM_MODULE_OPTIONS, getEntityManagerToken, getMikroORMToken, getRepositoryToken, logger, } from './mikro-orm.common.js';
4
4
  import { MikroOrmEntitiesStorage } from './mikro-orm.entities.storage.js';
@@ -42,7 +42,7 @@ export function createEntityManagerProvider(scope = Scope.DEFAULT, entityManager
42
42
  return {
43
43
  provide: contextName ? getEntityManagerToken(contextName) : entityManager,
44
44
  scope,
45
- useFactory: (orm) => (scope === Scope.DEFAULT ? orm.em : orm.em.fork({ useContext: true, ...forkOptions })),
45
+ useFactory: (orm) => scope === Scope.DEFAULT ? orm.em : orm.em.fork({ useContext: true, ...forkOptions }),
46
46
  inject: [contextName ? getMikroORMToken(contextName) : MikroORM],
47
47
  };
48
48
  }
@@ -52,7 +52,11 @@ export function createMikroOrmAsyncOptionsProvider(options) {
52
52
  provide: MIKRO_ORM_MODULE_OPTIONS,
53
53
  useFactory: async (...args) => {
54
54
  const factoryOptions = await options.useFactory(...args);
55
- return options.contextName ? { contextName: options.contextName, ...factoryOptions } : factoryOptions;
55
+ return {
56
+ ...(options.contextName ? { contextName: options.contextName } : {}),
57
+ ...(options.driver ? { driver: options.driver } : {}),
58
+ ...factoryOptions,
59
+ };
56
60
  },
57
61
  inject: options.inject || [],
58
62
  };
@@ -63,7 +67,13 @@ export function createMikroOrmAsyncOptionsProvider(options) {
63
67
  }
64
68
  return {
65
69
  provide: MIKRO_ORM_MODULE_OPTIONS,
66
- useFactory: async (optionsFactory) => await optionsFactory.createMikroOrmOptions(options.contextName),
70
+ useFactory: async (optionsFactory) => {
71
+ const factoryOptions = await optionsFactory.createMikroOrmOptions(options.contextName);
72
+ return {
73
+ ...(options.driver ? { driver: options.driver } : {}),
74
+ ...factoryOptions,
75
+ };
76
+ },
67
77
  inject,
68
78
  };
69
79
  }
@@ -83,7 +93,8 @@ export function createMikroOrmRepositoryProviders(entities, contextName) {
83
93
  (entities || []).forEach(entity => {
84
94
  const meta = entity instanceof EntitySchema
85
95
  ? entity.meta
86
- : (typeof entity === 'function' ? EntitySchema.REGISTRY.get(entity)?.meta : undefined) ?? metadata.find(meta => meta.class === entity);
96
+ : ((typeof entity === 'function' ? EntitySchema.REGISTRY.get(entity)?.meta : undefined) ??
97
+ metadata.find(meta => meta.class === entity));
87
98
  const repository = meta?.repository;
88
99
  if (repository) {
89
100
  providers.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/nestjs",
3
- "version": "7.0.2-dev.2",
3
+ "version": "7.0.2-dev.21",
4
4
  "description": "NestJS module for MikroORM",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -19,15 +19,15 @@
19
19
  ],
20
20
  "homepage": "https://github.com/mikro-orm/nestjs#readme",
21
21
  "bugs": "https://github.com/mikro-orm/nestjs/issues",
22
- "repository": {
23
- "type": "git",
24
- "url": "https://github.com/mikro-orm/nestjs"
25
- },
22
+ "license": "MIT",
26
23
  "author": {
27
24
  "name": "Martin Adamek",
28
25
  "email": "banan23@gmail.com"
29
26
  },
30
- "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/mikro-orm/nestjs"
30
+ },
31
31
  "type": "module",
32
32
  "exports": {
33
33
  ".": "./index.js",
@@ -43,15 +43,8 @@
43
43
  "format": "oxfmt --write .",
44
44
  "lint": "oxlint --type-aware",
45
45
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
46
- "coverage": "yarn test --coverage"
47
- },
48
- "commitlint": {
49
- "extends": [
50
- "@commitlint/config-conventional"
51
- ]
52
- },
53
- "lint-staged": {
54
- "*": "oxfmt --no-error-on-unmatched-pattern"
46
+ "coverage": "yarn test --coverage",
47
+ "publish:jsr": "jsr publish"
55
48
  },
56
49
  "devDependencies": {
57
50
  "@mikro-orm/core": "^7.0.0-dev.109",
@@ -67,14 +60,15 @@
67
60
  "@vitest/coverage-v8": "^4.0.16",
68
61
  "conventional-changelog": "^7.1.1",
69
62
  "conventional-changelog-cli": "^5.0.0",
70
- "oxfmt": "^0.40.0",
63
+ "jsr": "0.14.3",
64
+ "oxfmt": "^0.47.0",
71
65
  "oxlint": "^1.33.0",
72
- "oxlint-tsgolint": "^0.17.0",
66
+ "oxlint-tsgolint": "^0.22.0",
73
67
  "reflect-metadata": "^0.2.2",
74
68
  "rimraf": "^6.1.2",
75
69
  "rxjs": "^7.8.2",
76
70
  "supertest": "^7.1.4",
77
- "typescript": "5.9.3",
71
+ "typescript": "6.0.3",
78
72
  "unplugin-swc": "^1.5.9",
79
73
  "vitest": "^4.0.16"
80
74
  },
@@ -84,10 +78,18 @@
84
78
  "@nestjs/core": "^11.0.5",
85
79
  "reflect-metadata": "^0.1.0 || ^0.2.0"
86
80
  },
87
- "packageManager": "yarn@4.12.0",
81
+ "lint-staged": {
82
+ "*": "oxfmt --no-error-on-unmatched-pattern"
83
+ },
84
+ "commitlint": {
85
+ "extends": [
86
+ "@commitlint/config-conventional"
87
+ ]
88
+ },
88
89
  "engines": {
89
90
  "node": ">= 22.17.0"
90
91
  },
92
+ "packageManager": "yarn@4.14.1",
91
93
  "readme": "./README.md",
92
94
  "renovate": {
93
95
  "extends": [
package/typings.d.ts CHANGED
@@ -43,7 +43,7 @@ export type MikroOrmModuleOptions<D extends IDatabaseDriver = IDatabaseDriver> =
43
43
  * @default false
44
44
  */
45
45
  autoLoadEntities?: boolean;
46
- } & Partial<Options<D>> & MikroOrmMiddlewareModuleOptions;
46
+ } & Partial<Options<D, any, any>> & MikroOrmMiddlewareModuleOptions;
47
47
  export interface MikroOrmModuleFeatureOptions {
48
48
  /**
49
49
  * The entities to provide an EntityRepository for.