* Returns new EntityManager instance with its own identity map
(options: ForkOptions = {})
| 2521 | * Returns new EntityManager instance with its own identity map |
| 2522 | */ |
| 2523 | fork(options: ForkOptions = {}): this { |
| 2524 | const em = options.disableContextResolution ? this : this.getContext(false); |
| 2525 | options.clear ??= true; |
| 2526 | options.useContext ??= false; |
| 2527 | options.freshEventManager ??= false; |
| 2528 | options.cloneEventManager ??= false; |
| 2529 | |
| 2530 | const eventManager = options.freshEventManager |
| 2531 | ? new EventManager(em.config.get('subscribers')) |
| 2532 | : options.cloneEventManager |
| 2533 | ? em.eventManager.clone() |
| 2534 | : em.eventManager; |
| 2535 | |
| 2536 | // we need to allow global context here as forking from global EM is fine |
| 2537 | const allowGlobalContext = em.config.get('allowGlobalContext'); |
| 2538 | em.config.set('allowGlobalContext', true); |
| 2539 | const fork = new (em.constructor as typeof EntityManager)( |
| 2540 | em.config, |
| 2541 | em.driver, |
| 2542 | em.metadata, |
| 2543 | options.useContext, |
| 2544 | eventManager, |
| 2545 | ); |
| 2546 | fork.setFlushMode(options.flushMode ?? em.#flushMode); |
| 2547 | fork.#disableTransactions = |
| 2548 | options.disableTransactions ?? this.#disableTransactions ?? this.config.get('disableTransactions'); |
| 2549 | em.config.set('allowGlobalContext', allowGlobalContext); |
| 2550 | |
| 2551 | if (options.keepTransactionContext) { |
| 2552 | fork.#transactionContext = em.#transactionContext; |
| 2553 | } |
| 2554 | |
| 2555 | fork.#filters = { ...em.#filters }; |
| 2556 | fork.#filterParams = Utils.copy(em.#filterParams); |
| 2557 | fork.loggerContext = Utils.merge({}, em.loggerContext, options.loggerContext); |
| 2558 | fork.#schema = options.schema ?? em.#schema; |
| 2559 | fork.signal = options.signal ?? em.signal; |
| 2560 | fork.inflightQueryAbortStrategy = options.inflightQueryAbortStrategy ?? em.inflightQueryAbortStrategy; |
| 2561 | |
| 2562 | if (!options.clear) { |
| 2563 | for (const entity of em.#unitOfWork.getIdentityMap()) { |
| 2564 | fork.#unitOfWork.register(entity); |
| 2565 | } |
| 2566 | |
| 2567 | for (const entity of em.#unitOfWork.getPersistStack()) { |
| 2568 | fork.#unitOfWork.persist(entity); |
| 2569 | } |
| 2570 | |
| 2571 | for (const entity of em.#unitOfWork.getOrphanRemoveStack()) { |
| 2572 | fork.#unitOfWork.getOrphanRemoveStack().add(entity); |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | return fork as this; |
| 2577 | } |
| 2578 | |
| 2579 | /** |
| 2580 | * Gets the UnitOfWork used by the EntityManager to coordinate operations. |