MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / fork

Method fork

packages/core/src/EntityManager.ts:2523–2577  ·  view source on GitHub ↗

* Returns new EntityManager instance with its own identity map

(options: ForkOptions = {})

Source from the content-addressed store, hash-verified

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.

Callers 15

refreshMethod · 0.95
callFunction · 0.80
constructorMethod · 0.80
findMethod · 0.80
streamMethod · 0.80
findOneMethod · 0.80
upsertMethod · 0.80
upsertManyMethod · 0.80
createContextMethod · 0.80
createForkMethod · 0.80
decorateMethod · 0.80
CreateRequestContextFunction · 0.80

Calls 13

getContextMethod · 0.95
copyMethod · 0.80
getIdentityMapMethod · 0.80
getPersistStackMethod · 0.80
getOrphanRemoveStackMethod · 0.80
addMethod · 0.80
getMethod · 0.65
cloneMethod · 0.65
setMethod · 0.65
persistMethod · 0.65
setFlushModeMethod · 0.45
mergeMethod · 0.45

Tested by 15

createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createTestDataFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
getReferencesFunction · 0.64
getColsFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64