Skip to main content
Version: 7.0

UnitOfWork

Implements the Unit of Work pattern: tracks entity changes, computes change sets, and flushes them to the database.

Index

Constructors

constructor

Methods

cancelOrphanRemoval

  • cancelOrphanRemoval(entity, visited): void
  • Parameters

    • entity: Partial<any>
    • optionalvisited: Set<Partial<any>>

    Returns void

clear

  • clear(): void
  • Returns void

clearActionsQueue

  • clearActionsQueue(): void
  • Clears the queue of entity types that triggered auto-flush detection.


    Returns void

commit

  • commit(): Promise<void>
  • Flushes all pending changes to the database within a transaction.


    Returns Promise<void>

computeChangeSet

  • computeChangeSet<T>(entity, type): void
  • Computes and registers a change set for the given entity.


    Parameters

    Returns void

computeChangeSets

  • computeChangeSets(): void
  • Returns void

getById

  • getById<T>(entityName, id, schema, convertCustomTypes): undefined | T
  • Returns entity from the identity map. For composite keys, you need to pass an array of PKs in the same order as they are defined in meta.primaryKeys.


    Parameters

    • entityName: EntityName<T>
    • id: (T extends { [PrimaryKeyProp]?: PK } ? PK extends undefined ? Omit<T<T>, typeof PrimaryKeyProp> : PK extends keyof T<T> ? ReadonlyPrimary<UnwrapPrimary<T<T>[PK<PK>]>> : PK extends keyof T<T>[] ? ReadonlyPrimary<PrimaryPropToType<T<T>, PK<PK>>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T) | (T extends { [PrimaryKeyProp]?: PK } ? PK extends undefined ? Omit<T<T>, typeof PrimaryKeyProp> : PK extends keyof T<T> ? ReadonlyPrimary<UnwrapPrimary<T<T>[PK<PK>]>> : PK extends keyof T<T>[] ? ReadonlyPrimary<PrimaryPropToType<T<T>, PK<PK>>> : PK : T extends { _id?: PK } ? string | ReadonlyPrimary<PK> : T extends { id?: PK } ? ReadonlyPrimary<PK> : T extends { uuid?: PK } ? ReadonlyPrimary<PK> : T)[]
    • optionalschema: string
    • optionalconvertCustomTypes: boolean

    Returns undefined | T

getByKey

  • getByKey<T>(entityName, key, value, schema, convertCustomTypes): undefined | T
  • Returns entity from the identity map by an alternate key (non-PK property).


    Parameters

    • entityName: EntityName<T>
    • key: string
    • value: unknown
    • optionalschema: string
    • optionalconvertCustomTypes: boolean

      If true, the value is in database format and will be converted to JS format for lookup. If false (default), the value is assumed to be in JS format already.

    Returns undefined | T

getChangeSetPersister

  • getChangeSetPersister(): ChangeSetPersister
  • Returns ChangeSetPersister

getChangeSets

  • Returns all computed change sets for the current flush.


    Returns ChangeSet<Partial<any>>[]

getCollectionUpdates

  • getCollectionUpdates(): Collection<Partial<any>, object>[]
  • Returns all M:N collections that need synchronization.


    Returns Collection<Partial<any>, object>[]

getExtraUpdates

  • Returns extra updates needed for relations that could not be resolved in the initial pass.


    Returns Set<[Partial<any>, string | string[], Partial<any> | Collection<any, object> | Partial<any>[] | Reference<any>, undefined | ChangeSet<any>, ChangeSetType]>

getIdentityMap

  • getIdentityMap(): IdentityMap
  • Returns map of all managed entities.


    Returns IdentityMap

getOriginalEntityData

  • getOriginalEntityData<T>(entity): undefined | EntityData<T>
  • Returns stored snapshot of entity state that is used for change set computation.


    Parameters

    • entity: T

    Returns undefined | EntityData<T>

getOrphanRemoveStack

  • getOrphanRemoveStack(): Set<Partial<any>>
  • Returns Set<Partial<any>>

getPersistStack

  • getPersistStack(): Set<Partial<any>>
  • Returns the set of entities scheduled for persistence.


    Returns Set<Partial<any>>

getRemoveStack

  • getRemoveStack(): Set<Partial<any>>
  • Returns the set of entities scheduled for removal.


    Returns Set<Partial<any>>

lock

  • lock<T>(entity, options): Promise<void>
  • Parameters

    Returns Promise<void>

merge

  • merge<T>(entity, visited): void
  • Merges an entity into the identity map, taking a snapshot of its current state.


    Parameters

    • entity: T
    • optionalvisited: Set<Partial<any>>

    Returns void

persist

  • persist<T>(entity, visited, options): void
  • Marks an entity for persistence, cascading to related entities.


    Parameters

    • entity: T
    • optionalvisited: Set<Partial<any>>
    • options: { cascade?: boolean; checkRemoveStack?: boolean } = {}
      • optionalcascade: boolean
      • optionalcheckRemoveStack: boolean

    Returns void

recomputeSingleChangeSet

  • recomputeSingleChangeSet<T>(entity): void
  • Recomputes and merges the change set for an already-tracked entity.


    Parameters

    • entity: T

    Returns void

remove

  • remove<T>(entity, visited, options): void
  • Marks an entity for removal, cascading to related entities.


    Parameters

    • entity: T
    • optionalvisited: Set<Partial<any>>
    • options: { cascade?: boolean } = {}
      • optionalcascade: boolean

    Returns void

scheduleExtraUpdate

  • scheduleExtraUpdate<T>(changeSet, props): void

scheduleOrphanRemoval

  • scheduleOrphanRemoval(entity, visited): void
  • Parameters

    • optionalentity: Partial<any>
    • optionalvisited: Set<Partial<any>>

    Returns void

shouldAutoFlush

  • shouldAutoFlush<T>(meta): boolean
  • Checks whether an auto-flush is needed before querying the given entity type.


    Parameters

    Returns boolean

storeByKey

  • storeByKey<T>(entity, key, value, schema, convertCustomTypes): void
  • Stores an entity in the identity map under an alternate key (non-PK property). Also sets the property value on the entity.


    Parameters

    • entity: T
    • key: string
    • value: unknown
    • optionalschema: string
    • optionalconvertCustomTypes: boolean

      If true, the value is in database format and will be converted to JS format. If false (default), the value is assumed to be in JS format already.

    Returns void

tryGetById

  • tryGetById<T>(entityName, where, schema, strict): null | T
  • Attempts to extract a primary key from the where condition and look up the entity in the identity map.


    Parameters

    Returns null | T

unsetIdentity

  • unsetIdentity(entity): void
  • Parameters

    • entity: Partial<any>

    Returns void