Skip to content

Commit

Permalink
feat: expose fork options in the MikroOrmModule options configurati…
Browse files Browse the repository at this point in the history
…on object (#182)

Closes #181
  • Loading branch information
AlexBksiad authored Dec 8, 2024
1 parent e7af71a commit 87fd6b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/mikro-orm.providers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigurationLoader, EntityManager, MetadataStorage, MikroORM, type AnyEntity, type EntityClass, type EntityClassGroup, type EntitySchema } from '@mikro-orm/core';
import { ConfigurationLoader, EntityManager, MetadataStorage, MikroORM, type AnyEntity, type EntityClass, type EntityClassGroup, type EntitySchema, type ForkOptions } from '@mikro-orm/core';
import { MIKRO_ORM_MODULE_OPTIONS, getEntityManagerToken, getMikroORMToken, getRepositoryToken, logger } from './mikro-orm.common';

import { Scope, type InjectionToken, type Provider, type Type } from '@nestjs/common';
Expand Down Expand Up @@ -44,6 +44,7 @@ export function createEntityManagerProvider(
scope = Scope.DEFAULT,
entityManager: Type = EntityManager,
contextName?: string,
forkOptions?: ForkOptions,
): Provider<EntityManager> {
if (!contextName && entityManager !== EntityManager) {
return {
Expand All @@ -57,7 +58,7 @@ export function createEntityManagerProvider(
return {
provide: contextName ? getEntityManagerToken(contextName) : entityManager,
scope,
useFactory: (orm: MikroORM) => scope === Scope.DEFAULT ? orm.em : orm.em.fork(),
useFactory: (orm: MikroORM) => scope === Scope.DEFAULT ? orm.em : orm.em.fork(forkOptions),
inject: [contextName ? getMikroORMToken(contextName) : MikroORM],
};
}
Expand Down
10 changes: 9 additions & 1 deletion src/typings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnyEntity, EntityName as CoreEntityName, EntitySchema, IDatabaseDriver, Options } from '@mikro-orm/core';
import type { AnyEntity, EntityName as CoreEntityName, EntitySchema, ForkOptions, IDatabaseDriver, Options } from '@mikro-orm/core';
import type { MiddlewareConsumer, ModuleMetadata, Scope, Type } from '@nestjs/common';
import type { AbstractHttpAdapter } from '@nestjs/core';

Expand All @@ -15,6 +15,14 @@ type MikroOrmNestScopeOptions = {
* @see [NestJS Scope Hierarchy](https://docs.nestjs.com/fundamentals/injection-scopes#scope-hierarchy)
*/
scope?: Scope;
/**
* An optional configuration object to use when forking the Event Manager if it is configured with a scope other than Scope.DEFAULT
*
* This configuration option has no effect when the scope is set to Scope.DEFAULT.
*
* https://mikro-orm.io/api/core/interface/ForkOptions
*/
forkOptions?: ForkOptions;
};

export type MikroOrmMiddlewareModuleOptions = {
Expand Down

0 comments on commit 87fd6b8

Please sign in to comment.