Skip to main content
Version: 5.2

EntityManager <D>

@inheritdoc

Hierarchy

Index

Properties

readonly_id

_id: number = ...

readonlyconfig

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

Methods

addFilter

  • Registers global filter to this entity manager. Global filters are enabled by default (unless disabled via last parameter).


    Type parameters

    Parameters

    Returns void

assign

  • Shortcut for wrap(entity).assign(data, { em })


    Type parameters

    Parameters

    Returns T

begin

  • Starts new transaction bound to this EntityManager. Use ctx parameter to provide the parent when nesting transactions.


    Parameters

    Returns Promise<void>

canPopulate

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


    Type parameters

    Parameters

    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 experiation 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

  • Returns total number of entities matching your where query.


    Type parameters

    • T
    • P: string = never

    Parameters

    Returns Promise<number>

create

  • 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

    Parameters

    Returns T

createQueryBuilder

execute

  • execute<T>(queryOrKnex: string | QueryBuilder<any, any> | Raw<any>, params?: any[], method?: get | all | run): Promise<T>

find

  • Finds all entities matching your where query. You can pass additional options via the options parameter.


    Type parameters

    Parameters

    Returns Promise<Loaded<T, P>[]>

findAndCount

  • 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

    Parameters

    Returns Promise<[Loaded<T, P>[], number]>

findOne

  • Finds first entity matching your where query.


    Type parameters

    Parameters

    Returns Promise<null | Loaded<T, P>>

findOneOrFail

  • 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

    Parameters

    Returns Promise<Loaded<T, P>>

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

  • Returns new EntityManager instance with its own identity map


    Parameters

    Returns D[typeof EntityManagerType]

getComparator

  • Gets the EntityComparator.


    Returns EntityComparator

getConnection

  • Gets the Connection instance, by default returns write connection


    Parameters

    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

  • Gets the EntityFactory used by the EntityManager.


    Returns EntityFactory

getEventManager

  • Returns EventManager

getFilterParams

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


    Type parameters

    Parameters

    • name: string

    Returns T

getKnex

  • Returns configured knex instance.


    Parameters

    Returns Knex<any, any[]>

getMetadata

  • 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

  • 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

    Parameters

    Returns IdentifiedReference<T, PK>

getRepository

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

  • Gets the UnitOfWork used by the EntityManager to coordinate operations.


    Parameters

    • useContext: boolean = true

    Returns UnitOfWork

getValidator

  • Gets EntityValidator instance


    Returns EntityValidator

isInTransaction

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


    Returns boolean

lock

  • Runs your callback wrapped inside a database transaction.


    Type parameters

    • T

    Parameters

    Returns Promise<void>

map

  • Maps raw database result to an entity and merges it to this EntityManager.


    Type parameters

    Parameters

    Returns T

merge

  • 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

    Parameters

    Returns T

nativeDelete

  • Fires native delete query. Calling this has no side effects on the context (identity map).


    Type parameters

    Parameters

    Returns Promise<number>

nativeInsert

  • Fires native insert query. Calling this has no side effects on the context (identity map).


    Type parameters

    Parameters

    Returns Promise<Primary<T>>

nativeUpdate

  • Fires native update query. Calling this has no side effects on the context (identity map).


    Type parameters

    Parameters

    Returns Promise<number>

persist

  • 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.


    Parameters

    Returns SqlEntityManager<D>

persistAndFlush

  • Persists your entity immediately, flushing all not yet persisted changes to the database too. Equivalent to em.persist(e).flush().


    Parameters

    Returns Promise<void>

persistLater

  • 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

    Returns void

populate

  • populate<T, P>(entities: T | T[], populate: boolean | (P extends any ? ((P & `${string}.`) extends never ? P : P & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<T, never> ? `${A}.${B extends any ? ((B & `${string}.`) extends never ? B : B & `${string}.`) extends Q ? Q extends `${A}.${B}` ? A extends StringKeys<Exclude<GetStringKey<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, 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<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, 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<T, 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<T, A, never>, undefined | null>, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<Exclude<GetStringKey<T, 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<T, A, never>, undefined | null>, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> ? (Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, Q, never>, undefined | null> extends unknown ? Exclude<B, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<Exclude<GetStringKey<T, A, never>, undefined | null>, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<Exclude<GetStringKey<T, A, never>, undefined | null>, never> : never : never}` : never : Q extends StringKeys<T, never> ? (Exclude<GetStringKey<T, Q, never>, undefined | null> extends unknown ? Exclude<P, `${string}.`> : never) | (StringKeys<Exclude<GetStringKey<T, Q, never>, undefined | null>, never> extends never ? never : `${Q}.`) : StringKeys<T, never> : never : never)[], options?: EntityLoaderOptions<T, P>): Promise<Loaded<T, P>[]>
  • 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

    Parameters

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

    Returns Promise<Loaded<T, P>[]>

qb

raw

  • raw<R>(sql: string, bindings?: ValueDict | RawBinding[]): R
  • Creates raw SQL query that won’t be escaped when used as a parameter.


    Type parameters

    • R = Raw<any>

    Parameters

    • sql: string
    • bindings: ValueDict | RawBinding[] = []

    Returns R

remove

  • 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

    Parameters

    Returns SqlEntityManager<D>

removeAndFlush

  • Removes an entity instance immediately, flushing all not yet persisted changes to the database too. Equivalent to em.remove(e).flush()


    Parameters

    Returns Promise<void>

removeLater

  • 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()


    Parameters

    Returns void

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<any>): 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

    Returns void

setFlushMode

  • Parameters

    Returns void

setTransactionContext

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


    Parameters

    • ctx: any

    Returns void

transactional

  • Runs your callback wrapped inside a database transaction.


    Type parameters

    • T

    Parameters

    Returns Promise<T>