diff --git a/src/mikro-orm.providers.ts b/src/mikro-orm.providers.ts index c65a78b..088ac99 100644 --- a/src/mikro-orm.providers.ts +++ b/src/mikro-orm.providers.ts @@ -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'; @@ -44,6 +44,7 @@ export function createEntityManagerProvider( scope = Scope.DEFAULT, entityManager: Type = EntityManager, contextName?: string, + forkOptions?: ForkOptions, ): Provider { if (!contextName && entityManager !== EntityManager) { return { @@ -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], }; } diff --git a/src/typings.ts b/src/typings.ts index 5678ad2..ccfc6e0 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -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'; @@ -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 = {