Skip to main content
Version: 5.9

EntityManager <D>

The EntityManager is the central access point to ORM functionality. It is a facade to all different ORM subsystems such as UnitOfWork, Query Language and Repository API.

@template

driver type

Hierarchy

  • EntityManager<D>
    • EntityManager

Index

Properties

readonly_id

_id: number = ...

readonlyconfig

config: Configuration<IDatabaseDriver<Connection>>

readonlyglobal

global: false = false

readonlyname

name: string = ...

Accessors

id

  • get id(): number
  • Returns the ID of this EntityManager. Respects the context, so global EM will give you the contextual ID if executed inside request context handler.


    Returns number

schema

  • get schema(): undefined | string
  • set schema(schema: undefined | null | string): void
  • Returns the default schema of this EntityManager. Respects the context, so global EM will give you the contextual schema if executed inside request context handler.


    Returns undefined | string

  • Sets the default schema of this EntityManager. Respects the context, so global EM will set the contextual schema if executed inside request context handler.


    Parameters

    • schema: undefined | null | string

    Returns void

Methods

addFilter

  • addFilter<T1>(name: string, cond: FilterQuery<T1> | (args: Dictionary) => MaybePromise<FilterQuery<T1>>, entityName?: EntityName<T1> | [EntityName<T1>], enabled?: boolean): void
  • addFilter<T1, T2>(name: string, cond: FilterQuery<T1 | T2> | (args: Dictionary) => MaybePromise<FilterQuery<T1 | T2>>, entityName?: [EntityName<T1>, EntityName<T2>], enabled?: boolean): void
  • addFilter<T1, T2, T3>(name: string, cond: FilterQuery<T1 | T2 | T3> | (args: Dictionary) => MaybePromise<FilterQuery<T1 | T2 | T3>>, entityName?: [EntityName<T1>, EntityName<T2>, EntityName<T3>], enabled?: boolean): void
  • Registers global filter to this entity manager. Global filters are enabled by default (unless disabled via last parameter).


    Type parameters

    • T1

    Parameters

    • name: string
    • cond: FilterQuery<T1> | (args: Dictionary) => MaybePromise<FilterQuery<T1>>
      • optionalentityName: EntityName<T1> | [EntityName<T1>]
      • optionalenabled: boolean

      Returns void

    aggregate

    • aggregate(entityName: EntityName<any>, pipeline: any[]): Promise<any[]>
    • Shortcut to driver's aggregate method. Available in MongoDriver only.


      Parameters

      • entityName: EntityName<any>
      • pipeline: any[]

      Returns Promise<any[]>

    assign

    • assign<Entity>(entity: Entity, data: EntityData<Entity> | Partial<EntityDTO<Entity>>, options?: AssignOptions): Entity
    • Shortcut for wrap(entity).assign(data, { em })


      Type parameters

      • Entity: object

      Parameters

      • entity: Entity
      • data: EntityData<Entity> | Partial<EntityDTO<Entity>>
      • options: AssignOptions = {}

      Returns Entity

    begin

    • begin(options?: Omit<TransactionOptions, ignoreNestedTransactions> & TransactionOptions): Promise<void>
    • Starts new transaction bound to this EntityManager. Use ctx parameter to provide the parent when nesting transactions.


      Parameters

      • options: Omit<TransactionOptions, ignoreNestedTransactions> & TransactionOptions = {}

      Returns Promise<void>

    canPopulate

    • canPopulate<Entity>(entityName: EntityName<Entity>, property: string): boolean
    • Checks whether given property can be populated on the entity.


      Type parameters

      • Entity: object

      Parameters

      • entityName: EntityName<Entity>
      • property: string

      Returns boolean

    clear

    • clear(): void
    • Clears the EntityManager. All entities that are currently managed by this EntityManager become detached.


      Returns void

    clearCache

    • clearCache(cacheKey: string): Promise<void>
    • Clears result cache for given cache key. If we want to be able to call this method, we need to set the cache key explicitly when storing the cache.

      // set the cache key to 'book-cache-key', with expiration of 60s
      const res = await em.find(Book, { ... }, { cache: ['book-cache-key', 60_000] });

      // clear the cache key by name
      await em.clearCache('book-cache-key');

      Parameters

      • cacheKey: string

      Returns Promise<void>

    commit

    • commit(): Promise<void>
    • Commits the transaction bound to this EntityManager. Flushes before doing the actual commit query.


      Returns Promise<void>

    count

    • count<Entity, Hint>(entityName: EntityName<Entity>, where?: FilterQuery<Entity>, options?: CountOptions<Entity, Hint>): Promise<number>
    • Returns total number of entities matching your where query.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity> = ...
      • options: CountOptions<Entity, Hint> = {}

      Returns Promise<number>

    create

    • create<Entity>(entityName: EntityName<Entity>, data: RequiredEntityData<Entity>, options?: CreateOptions): Entity
    • Creates new instance of given entity and populates it with given data. The entity constructor will be used unless you provide { managed: true } in the options parameter. The constructor will be given parameters based on the defined constructor of the entity. If the constructor parameter matches a property name, its value will be extracted from data. If no matching property exists, the whole data parameter will be passed. This means we can also define constructor(data: Partial<T>) and em.create() will pass the data into it (unless we have a property named data too).


      Type parameters

      • Entity: object

      Parameters

      • entityName: EntityName<Entity>
      • data: RequiredEntityData<Entity>
      • options: CreateOptions = {}

      Returns Entity

    find

    • find<Entity, Hint>(entityName: EntityName<Entity>, where: FilterQuery<Entity>, options?: FindOptions<Entity, Hint>): Promise<Loaded<Entity, Hint>[]>
    • Finds all entities matching your where query. You can pass additional options via the options parameter.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity>
      • options: FindOptions<Entity, Hint> = {}

      Returns Promise<Loaded<Entity, Hint>[]>

    findAndCount

    • findAndCount<Entity, Hint>(entityName: EntityName<Entity>, where: FilterQuery<Entity>, options?: FindOptions<Entity, Hint>): Promise<[Loaded<Entity, Hint>[], number]>
    • Calls em.find() and em.count() with the same arguments (where applicable) and returns the results as tuple where first element is the array of entities and the second is the count.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity>
      • options: FindOptions<Entity, Hint> = {}

      Returns Promise<[Loaded<Entity, Hint>[], number]>

    findOne

    • findOne<Entity, Hint>(entityName: EntityName<Entity>, where: FilterQuery<Entity>, options?: FindOneOptions<Entity, Hint>): Promise<null | Loaded<Entity, Hint>>
    • Finds first entity matching your where query.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity>
      • options: FindOneOptions<Entity, Hint> = {}

      Returns Promise<null | Loaded<Entity, Hint>>

    findOneOrFail

    • findOneOrFail<Entity, Hint>(entityName: EntityName<Entity>, where: FilterQuery<Entity>, options?: FindOneOrFailOptions<Entity, Hint>): Promise<Loaded<Entity, Hint>>
    • Finds first entity matching your where query. If nothing found, it will throw an error. If the strict option is specified and nothing is found or more than one matching entity is found, it will throw an error. You can override the factory for creating this method via options.failHandler locally or via Configuration.findOneOrFailHandler (findExactlyOneOrFailHandler when specifying strict) globally.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity>
      • options: FindOneOrFailOptions<Entity, Hint> = {}

      Returns Promise<Loaded<Entity, Hint>>

    flush

    • flush(): Promise<void>
    • Flushes all changes to objects that have been queued up to now to the database. This effectively synchronizes the in-memory state of managed objects with the database.


      Returns Promise<void>

    fork

    • fork(options?: ForkOptions): D[typeof EntityManagerType]
    • Returns new EntityManager instance with its own identity map


      Parameters

      • options: ForkOptions = {}

      Returns D[typeof EntityManagerType]

    getCollection

    • getCollection<T>(entityName: EntityName<T>): Collection<T>
    • Type parameters

      • T: Document

      Parameters

      • entityName: EntityName<T>

      Returns Collection<T>

    getComparator

    • getComparator(): EntityComparator
    • Gets the EntityComparator.


      Returns EntityComparator

    getConnection

    • getConnection(type?: ConnectionType): ReturnType<D[getConnection]>
    • Gets the Connection instance, by default returns write connection


      Parameters

      • optionaltype: ConnectionType

      Returns ReturnType<D[getConnection]>

    getDriver

    • getDriver(): D
    • Gets the Driver instance used by this EntityManager. Driver is singleton, for one MikroORM instance, only one driver is created.


      Returns D

    getEntityFactory

    • getEntityFactory(): EntityFactory
    • Gets the EntityFactory used by the EntityManager.


      Returns EntityFactory

    getEventManager

    • getEventManager(): EventManager
    • Returns EventManager

    getFilterParams

    • getFilterParams<T>(name: string): T
    • Returns filter parameters for given filter set in this context.


      Type parameters

      • T: Dictionary = Dictionary

      Parameters

      • name: string

      Returns T

    getHydrator

    • getHydrator(): IHydrator
    • Gets the Hydrator used by the EntityManager.


      Returns IHydrator

    getMetadata

    • getMetadata(): MetadataStorage
    • getMetadata<Entity>(entityName: EntityName<Entity>): EntityMetadata<Entity>
    • Gets the MetadataStorage.


      Returns MetadataStorage

    getPlatform

    • getPlatform(): ReturnType<D[getPlatform]>
    • Gets the platform instance. Just like the driver, platform is singleton, one for a MikroORM instance.


      Returns ReturnType<D[getPlatform]>

    getReference

    • getReference<Entity, PK>(entityName: EntityName<Entity>, id: Primary<Entity>, options: Omit<GetReferenceOptions, wrapped> & { wrapped: true }): Ref<Entity, PK>
    • getReference<Entity>(entityName: EntityName<Entity>, id: Primary<Entity> | Primary<Entity>[]): Entity
    • getReference<Entity>(entityName: EntityName<Entity>, id: Primary<Entity>, options: Omit<GetReferenceOptions, wrapped> & { wrapped: false }): Entity
    • getReference<Entity>(entityName: EntityName<Entity>, id: Primary<Entity>, options?: GetReferenceOptions): Entity | Reference<Entity>
    • Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded


      Type parameters

      • Entity: object
      • PK: string | number | symbol

      Parameters

      • entityName: EntityName<Entity>
      • id: Primary<Entity>
      • options: Omit<GetReferenceOptions, wrapped> & { wrapped: true }

      Returns Ref<Entity, PK>

    getRepository

    • getRepository<T, U>(entityName: EntityName<T>): GetRepository<T, U>
    • Gets repository for given entity. You can pass either string name or entity class reference.


      Type parameters

      • T: object
      • U: EntityRepository<T, U> = MongoEntityRepository<T>

      Parameters

      • entityName: EntityName<T>

      Returns GetRepository<T, U>

    getTransactionContext

    • getTransactionContext<T>(): undefined | T
    • Gets the transaction context (driver dependent object used to make sure queries are executed on same connection).


      Type parameters

      • T: unknown = any

      Returns undefined | T

    getUnitOfWork

    • getUnitOfWork(useContext?: boolean): UnitOfWork
    • Gets the UnitOfWork used by the EntityManager to coordinate operations.


      Parameters

      • useContext: boolean = true

      Returns UnitOfWork

    getValidator

    • getValidator(): EntityValidator
    • Gets EntityValidator instance


      Returns EntityValidator

    insert

    • insert<Entity>(entityNameOrEntity: Entity | EntityName<Entity>, data?: Entity | EntityData<Entity>, options?: NativeInsertUpdateOptions<Entity>): Promise<Primary<Entity>>
    • Fires native insert query. Calling this has no side effects on the context (identity map).


      Type parameters

      • Entity: object

      Parameters

      • entityNameOrEntity: Entity | EntityName<Entity>
      • optionaldata: Entity | EntityData<Entity>
      • options: NativeInsertUpdateOptions<Entity> = {}

      Returns Promise<Primary<Entity>>

    insertMany

    • insertMany<Entity>(entityNameOrEntities: EntityName<Entity> | Entity[], data?: Entity[] | EntityData<Entity>[], options?: NativeInsertUpdateOptions<Entity>): Promise<Primary<Entity>[]>
    • Fires native multi-insert query. Calling this has no side effects on the context (identity map).


      Type parameters

      • Entity: object

      Parameters

      • entityNameOrEntities: EntityName<Entity> | Entity[]
      • optionaldata: Entity[] | EntityData<Entity>[]
      • options: NativeInsertUpdateOptions<Entity> = {}

      Returns Promise<Primary<Entity>[]>

    isInTransaction

    • isInTransaction(): boolean
    • Checks whether this EntityManager is currently operating inside a database transaction.


      Returns boolean

    lock

    • lock<T>(entity: T, lockMode: LockMode, options?: number | Date | LockOptions): Promise<void>
    • Runs your callback wrapped inside a database transaction.


      Type parameters

      • T: object

      Parameters

      • entity: T
      • lockMode: LockMode
      • options: number | Date | LockOptions = {}

      Returns Promise<void>

    map

    • map<Entity>(entityName: EntityName<Entity>, result: EntityDictionary<Entity>, options?: { schema?: string }): Entity
    • Maps raw database result to an entity and merges it to this EntityManager.


      Type parameters

      • Entity: object

      Parameters

      • entityName: EntityName<Entity>
      • result: EntityDictionary<Entity>
      • options: { schema?: string } = {}
        • optionalschema: string

      Returns Entity

    merge

    • merge<Entity>(entity: Entity, options?: MergeOptions): Entity
    • merge<Entity>(entityName: EntityName<Entity>, data: EntityData<Entity> | EntityDTO<Entity>, options?: MergeOptions): Entity
    • Merges given entity to this EntityManager so it becomes managed. You can force refreshing of existing entities via second parameter. By default, it will return already loaded entities without modifying them.


      Type parameters

      • Entity: object

      Parameters

      • entity: Entity
      • optionaloptions: MergeOptions

      Returns Entity

    nativeDelete

    • nativeDelete<Entity>(entityName: EntityName<Entity>, where: FilterQuery<Entity>, options?: DeleteOptions<Entity>): Promise<number>
    • Fires native delete query. Calling this has no side effects on the context (identity map).


      Type parameters

      • Entity: object

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity>
      • options: DeleteOptions<Entity> = {}

      Returns Promise<number>

    nativeInsert

    • nativeInsert<Entity>(entityNameOrEntity: Entity | EntityName<Entity>, data?: Entity | EntityData<Entity>, options?: NativeInsertUpdateOptions<Entity>): Promise<Primary<Entity>>
    • alias for em.insert()

      @deprecated

      use em.insert() instead


      Type parameters

      • Entity: object

      Parameters

      • entityNameOrEntity: Entity | EntityName<Entity>
      • optionaldata: Entity | EntityData<Entity>
      • options: NativeInsertUpdateOptions<Entity> = {}

      Returns Promise<Primary<Entity>>

    nativeUpdate

    • nativeUpdate<Entity>(entityName: EntityName<Entity>, where: FilterQuery<Entity>, data: EntityData<Entity>, options?: UpdateOptions<Entity>): Promise<number>
    • Fires native update query. Calling this has no side effects on the context (identity map).


      Type parameters

      • Entity: object

      Parameters

      • entityName: EntityName<Entity>
      • where: FilterQuery<Entity>
      • data: EntityData<Entity>
      • options: UpdateOptions<Entity> = {}

      Returns Promise<number>

    persist

    • persist<Entity>(entity: Entity | Reference<Entity> | (Entity | Reference<Entity>)[]): MongoEntityManager<D>
    • Tells the EntityManager to make an instance managed and persistent. The entity will be entered into the database at or before transaction commit or as a result of the flush operation.


      Type parameters

      • Entity: object

      Parameters

      • entity: Entity | Reference<Entity> | (Entity | Reference<Entity>)[]

      Returns MongoEntityManager<D>

    persistAndFlush

    • persistAndFlush(entity: Partial<any> | Reference<Partial<any>> | (Partial<any> | Reference<Partial<any>>)[]): Promise<void>
    • Persists your entity immediately, flushing all not yet persisted changes to the database too. Equivalent to em.persist(e).flush().


      Parameters

      • entity: Partial<any> | Reference<Partial<any>> | (Partial<any> | Reference<Partial<any>>)[]

      Returns Promise<void>

    persistLater

    • persistLater(entity: Partial<any> | Partial<any>[]): void
    • Tells the EntityManager to make an instance managed and persistent. The entity will be entered into the database at or before transaction commit or as a result of the flush operation.

      @deprecated

      use persist()


      Parameters

      • entity: Partial<any> | Partial<any>[]

      Returns void

    populate

    • populate<Entity, Hint>(entities: Entity | Entity[], populate: boolean | (Hint extends any ? ((Hint & `${string}.`) extends never ? Hint : Hint & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Entity, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Entity, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${string}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Entity, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Entity, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Entity, never> ? (Exclude<GetStringKey<Entity, Q, never>, undefined | null> extends unknown ? Exclude<Hint, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Entity, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Entity, never> : never : never)[], options?: EntityLoaderOptions<Entity, Hint>): Promise<Loaded<Entity, Hint>[]>
    • Loads specified relations in batch. This will execute one query for each relation, that will populate it on all of the specified entities.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entities: Entity | Entity[]
      • populate: boolean | (Hint extends any ? ((Hint & `${string}.`) extends never ? Hint : Hint & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Entity, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Entity, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? `${A}.${string}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Entity, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<Entity, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Entity, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Entity, never> ? (Exclude<GetStringKey<Entity, Q, never>, undefined | null> extends unknown ? Exclude<Hint, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Entity, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Entity, never> : never : never)[]
      • options: EntityLoaderOptions<Entity, Hint> = {}

      Returns Promise<Loaded<Entity, Hint>[]>

    refresh

    • refresh<Entity, Hint>(entity: Entity, options?: FindOneOptions<Entity, Hint>): Promise<null | Loaded<Entity, Hint>>
    • Refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been persisted.


      Type parameters

      • Entity: object
      • Hint: string = never

      Parameters

      • entity: Entity
      • options: FindOneOptions<Entity, Hint> = {}

      Returns Promise<null | Loaded<Entity, Hint>>

    remove

    • remove<Entity>(entity: Entity | Reference<Entity> | (Entity | Reference<Entity>)[]): MongoEntityManager<D>
    • Marks entity for removal. A removed entity will be removed from the database at or before transaction commit or as a result of the flush operation.

      To remove entities by condition, use em.nativeDelete().


      Type parameters

      • Entity: object

      Parameters

      • entity: Entity | Reference<Entity> | (Entity | Reference<Entity>)[]

      Returns MongoEntityManager<D>

    removeAndFlush

    • removeAndFlush(entity: Partial<any> | Reference<Partial<any>>): Promise<void>
    • Removes an entity instance immediately, flushing all not yet persisted changes to the database too. Equivalent to em.remove(e).flush()


      Parameters

      • entity: Partial<any> | Reference<Partial<any>>

      Returns Promise<void>

    removeLater

    • removeLater<Entity>(entity: Entity): void
    • Marks entity for removal. A removed entity will be removed from the database at or before transaction commit or as a result of the flush operation.

      @deprecated

      use remove()


      Type parameters

      • Entity: object

      Parameters

      • entity: Entity

      Returns void

    repo

    • repo<Entity, Repository>(entityName: EntityName<Entity>): GetRepository<Entity, Repository>
    • Shortcut for em.getRepository().


      Type parameters

      • Entity: object
      • Repository: EntityRepository<Entity, Repository> = EntityRepository<Entity>

      Parameters

      • entityName: EntityName<Entity>

      Returns GetRepository<Entity, Repository>

    resetTransactionContext

    • resetTransactionContext(): void
    • Resets the transaction context.


      Returns void

    rollback

    • rollback(): Promise<void>
    • Rollbacks the transaction bound to this EntityManager.


      Returns Promise<void>

    setFilterParams

    • setFilterParams(name: string, args: Dictionary): void
    • Sets filter parameter values globally inside context defined by this entity manager. If you want to set shared value for all contexts, be sure to use the root entity manager.


      Parameters

      • name: string
      • args: Dictionary

      Returns void

    setFlushMode

    • setFlushMode(flushMode?: FlushMode): void
    • Parameters

      • optionalflushMode: FlushMode

      Returns void

    setTransactionContext

    • setTransactionContext(ctx: any): void
    • Sets the transaction context.


      Parameters

      • ctx: any

      Returns void

    transactional

    • transactional<T>(cb: (em: D[typeof EntityManagerType]) => Promise<T>, options?: TransactionOptions & TransactionOptions): Promise<T>
    • Runs your callback wrapped inside a database transaction.


      Type parameters

      • T

      Parameters

      • cb: (em: D[typeof EntityManagerType]) => Promise<T>
        • options: TransactionOptions & TransactionOptions = {}

        Returns Promise<T>

      upsert

      • upsert<Entity>(entityNameOrEntity: Entity | EntityName<Entity>, data?: Entity | EntityData<Entity>, options?: UpsertOptions<Entity>): Promise<Entity>
      • Creates or updates the entity, based on whether it is already present in the database. This method performs an insert on conflict merge query ensuring the database is in sync, returning a managed entity instance. The method accepts either entityName together with the entity data, or just entity instance.

        // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
        const author = await em.upsert(Author, { email: 'foo@bar.com', age: 33 });

        The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where Author.email is a unique property:

        // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
        // select "id" from "author" where "email" = 'foo@bar.com'
        const author = await em.upsert(Author, { email: 'foo@bar.com', age: 33 });

        Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the data.

        If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit flush will be required for those changes to be persisted.


        Type parameters

        • Entity: object

        Parameters

        • entityNameOrEntity: Entity | EntityName<Entity>
        • optionaldata: Entity | EntityData<Entity>
        • options: UpsertOptions<Entity> = {}

        Returns Promise<Entity>

      upsertMany

      • upsertMany<Entity>(entityNameOrEntity: EntityName<Entity> | Entity[], data?: (Entity | EntityData<Entity>)[], options?: UpsertManyOptions<Entity>): Promise<Entity[]>
      • Creates or updates the entity, based on whether it is already present in the database. This method performs an insert on conflict merge query ensuring the database is in sync, returning a managed entity instance. The method accepts either entityName together with the entity data, or just entity instance.

        // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
        const authors = await em.upsertMany(Author, [{ email: 'foo@bar.com', age: 33 }, ...]);

        The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where Author.email is a unique property:

        // insert into "author" ("age", "email") values (33, 'foo@bar.com'), (666, 'lol@lol.lol') on conflict ("email") do update set "age" = excluded."age"
        // select "id" from "author" where "email" = 'foo@bar.com'
        const author = await em.upsertMany(Author, [
        { email: 'foo@bar.com', age: 33 },
        { email: 'lol@lol.lol', age: 666 },
        ]);

        Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the data.

        If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit flush will be required for those changes to be persisted.


        Type parameters

        • Entity: object

        Parameters

        • entityNameOrEntity: EntityName<Entity> | Entity[]
        • optionaldata: (Entity | EntityData<Entity>)[]
        • options: UpsertManyOptions<Entity> = {}

        Returns Promise<Entity[]>