Skip to main content
Version: 4.5

Class: EntityRepository<T>#

core.EntityRepository

Type parameters#

NameType
TAnyEntity<T>

Hierarchy#

Constructors#

constructor#

+ new EntityRepository<T>(_em: EntityManager<IDatabaseDriver<Connection>>, entityName: EntityName<T>): EntityRepository<T>

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameType
_emEntityManager<IDatabaseDriver<Connection>>
entityNameEntityName<T>

Returns: EntityRepository<T>

Defined in: packages/core/src/entity/EntityRepository.ts:7

Properties#

_em#

Protected Readonly _em: EntityManager<IDatabaseDriver<Connection>>


entityName#

Protected Readonly entityName: EntityName<T>

Accessors#

em#

Protectedget em(): EntityManager<IDatabaseDriver<Connection>>

Returns: EntityManager<IDatabaseDriver<Connection>>

Defined in: packages/core/src/entity/EntityRepository.ts:278

Methods#

assign#

assign(entity: T, data: EntityData<T>): T

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

Parameters:#

NameType
entityT
dataEntityData<T>

Returns: T

Defined in: packages/core/src/entity/EntityRepository.ts:259


canPopulate#

canPopulate(property: string): boolean

Checks whether given property can be populated on the entity.

Parameters:#

NameType
propertystring

Returns: boolean

Defined in: packages/core/src/entity/EntityRepository.ts:223


count#

count(where?: FilterQuery<T>, options?: CountOptions<T>): Promise<number>

Returns total number of entities matching your where query.

Parameters:#

NameType
whereFilterQuery<T>
optionsCountOptions<T>

Returns: Promise<number>

Defined in: packages/core/src/entity/EntityRepository.ts:274


create#

create<P>(data: EntityData<T>): Loaded<T, P>

Creates new instance of given entity and populates it with given data

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>string[]

Parameters:#

NameType
dataEntityData<T>

Returns: Loaded<T, P>

Defined in: packages/core/src/entity/EntityRepository.ts:252


find#

find<P>(where: FilterQuery<T>, options?: FindOptions<T, P>): Promise<Loaded<T, P>[]>

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

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
options?FindOptions<T, P>

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

Defined in: packages/core/src/entity/EntityRepository.ts:81

find<P>(where: FilterQuery<T>, populate?: P, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<Loaded<T, P>[]>

Finds all entities matching your where query.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
populate?P
orderBy?QueryOrderMap
limit?number
offset?number

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

Defined in: packages/core/src/entity/EntityRepository.ts:86


findAll#

findAll<P>(options?: FindOptions<T, P>): Promise<Loaded<T, P>[]>

Finds all entities of given type. You can pass additional options via the options parameter.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
options?FindOptions<T, P>

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

Defined in: packages/core/src/entity/EntityRepository.ts:118

findAll<P>(populate?: P, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<Loaded<T, P>[]>

Finds all entities of given type.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
populate?P
orderBy?QueryOrderMap
limit?number
offset?number

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

Defined in: packages/core/src/entity/EntityRepository.ts:123


findAndCount#

findAndCount<P>(where: FilterQuery<T>, options?: FindOptions<T, readonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>>): Promise<[Loaded<T, P>[], 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:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
options?FindOptions<T, readonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>>

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

Defined in: packages/core/src/entity/EntityRepository.ts:99

findAndCount<P>(where: FilterQuery<T>, populate?: P, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<[Loaded<T, P>[], 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:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
populate?P
orderBy?QueryOrderMap
limit?number
offset?number

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

Defined in: packages/core/src/entity/EntityRepository.ts:105


findOne#

findOne<P>(where: FilterQuery<T>, populate?: P, orderBy?: QueryOrderMap): Promise<null | Loaded<T, P>>

Finds first entity matching your where query.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
populate?P
orderBy?QueryOrderMap

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

Defined in: packages/core/src/entity/EntityRepository.ts:41

findOne<P>(where: FilterQuery<T>, populate?: FindOneOptions<T, P>, orderBy?: QueryOrderMap): Promise<null | Loaded<T, P>>

Finds first entity matching your where query.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
populate?FindOneOptions<T, P>
orderBy?QueryOrderMap

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

Defined in: packages/core/src/entity/EntityRepository.ts:46


findOneOrFail#

findOneOrFail<P>(where: FilterQuery<T>, populate?: P, orderBy?: QueryOrderMap): Promise<Loaded<T, P>>

Finds first entity matching your where query. If nothing found, it will throw an error. You can override the factory for creating this method via options.failHandler locally or via Configuration.findOneOrFailHandler globally.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
populate?P
orderBy?QueryOrderMap

Returns: Promise<Loaded<T, P>>

Defined in: packages/core/src/entity/EntityRepository.ts:60

findOneOrFail<P>(where: FilterQuery<T>, populate?: FindOneOrFailOptions<T, P>, orderBy?: QueryOrderMap): Promise<Loaded<T, P>>

Finds first entity matching your where query. If nothing found, it will throw an error. You can override the factory for creating this method via options.failHandler locally or via Configuration.findOneOrFailHandler globally.

Type parameters:#

NameTypeDefault
Preadonly string[] | readonly keyof T[] | boolean | LoadStrategy | PopulateChildren<T>any

Parameters:#

NameType
whereFilterQuery<T>
populate?FindOneOrFailOptions<T, P>
orderBy?QueryOrderMap

Returns: Promise<Loaded<T, P>>

Defined in: packages/core/src/entity/EntityRepository.ts:67


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. This method is a shortcut for em.flush(), in other words, it will flush the whole UoW, not just entities registered via this particular repository.

Returns: Promise<void>

Defined in: packages/core/src/entity/EntityRepository.ts:166


getReference#

getReference<PK>(id: Primary<T>, wrapped: true): IdentifiedReference<T, PK>

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:#

NameType
PKstring | number | symbol

Parameters:#

NameType
idPrimary<T>
wrappedtrue

Returns: IdentifiedReference<T, PK>

Defined in: packages/core/src/entity/EntityRepository.ts:201

getReference<PK>(id: Primary<T>): T

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:#

NameTypeDefault
PKstring | number | symbolkeyof T

Parameters:#

NameType
idPrimary<T>

Returns: T

Defined in: packages/core/src/entity/EntityRepository.ts:206

getReference<PK>(id: Primary<T>, wrapped: false): T

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:#

NameTypeDefault
PKstring | number | symbolkeyof T

Parameters:#

NameType
idPrimary<T>
wrappedfalse

Returns: T

Defined in: packages/core/src/entity/EntityRepository.ts:211


map#

map(result: EntityData<T>): T

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

Parameters:#

NameType
resultEntityData<T>

Returns: T

Defined in: packages/core/src/entity/EntityRepository.ts:194


merge#

merge(data: T | EntityData<T>, refresh?: boolean, convertCustomTypes?: boolean): T

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.

Parameters:#

NameType
dataT | EntityData<T>
refresh?boolean
convertCustomTypes?boolean

Returns: T

Defined in: packages/core/src/entity/EntityRepository.ts:267


nativeDelete#

nativeDelete(where: FilterQuery<T>, options?: DeleteOptions<T>): Promise<number>

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

Parameters:#

NameType
whereFilterQuery<T>
options?DeleteOptions<T>

Returns: Promise<number>

Defined in: packages/core/src/entity/EntityRepository.ts:187


nativeInsert#

nativeInsert(data: EntityData<T>): Promise<Primary<T>>

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

Parameters:#

NameType
dataEntityData<T>

Returns: Promise<Primary<T>>

Defined in: packages/core/src/entity/EntityRepository.ts:173


nativeUpdate#

nativeUpdate(where: FilterQuery<T>, data: EntityData<T>, options?: UpdateOptions<T>): Promise<number>

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

Parameters:#

NameType
whereFilterQuery<T>
dataEntityData<T>
options?UpdateOptions<T>

Returns: Promise<number>

Defined in: packages/core/src/entity/EntityRepository.ts:180


persist#

persist(entity: AnyEntity<any> | AnyEntity<any>[]): EntityManager<IDatabaseDriver<Connection>>

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:#

NameType
entityAnyEntity<any> | AnyEntity<any>[]

Returns: EntityManager<IDatabaseDriver<Connection>>

Defined in: packages/core/src/entity/EntityRepository.ts:16


persistAndFlush#

persistAndFlush(entity: AnyEntity<any> | AnyEntity<any>[]): Promise<void>

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

Parameters:#

NameType
entityAnyEntity<any> | AnyEntity<any>[]

Returns: Promise<void>

Defined in: packages/core/src/entity/EntityRepository.ts:24


persistLater#

persistLater(entity: AnyEntity<any> | AnyEntity<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:#

NameType
entityAnyEntity<any> | AnyEntity<any>[]

Returns: void

Defined in: packages/core/src/entity/EntityRepository.ts:34


populate#

populate<P>(entities: T, populate: P, where?: FilterQuery<T>, orderBy?: QueryOrderMap, refresh?: boolean, validate?: boolean): 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:#

NameType
Pstring | number | boolean | symbol | readonly string[] | readonly keyof T[] | PopulateChildren<T>

Parameters:#

NameType
entitiesT
populateP
where?FilterQuery<T>
orderBy?QueryOrderMap
refresh?boolean
validate?boolean

Returns: Promise<Loaded<T, P>>

Defined in: packages/core/src/entity/EntityRepository.ts:230

populate<P>(entities: T[], populate: P, where?: FilterQuery<T>, orderBy?: QueryOrderMap, refresh?: boolean, validate?: boolean): 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:#

NameType
Pstring | number | boolean | symbol | readonly string[] | readonly keyof T[] | PopulateChildren<T>

Parameters:#

NameType
entitiesT[]
populateP
where?FilterQuery<T>
orderBy?QueryOrderMap
refresh?boolean
validate?boolean

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

Defined in: packages/core/src/entity/EntityRepository.ts:235

populate<P>(entities: T | T[], populate: P, where?: FilterQuery<T>, orderBy?: QueryOrderMap, refresh?: boolean, validate?: boolean): Promise<Loaded<T, P> | 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:#

NameType
Pstring | number | boolean | symbol | readonly string[] | readonly keyof T[] | PopulateChildren<T>

Parameters:#

NameType
entitiesT | T[]
populateP
where?FilterQuery<T>
orderBy?QueryOrderMap
refresh?boolean
validate?boolean

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

Defined in: packages/core/src/entity/EntityRepository.ts:240


remove#

remove(entity: AnyEntity<any>): EntityManager<IDatabaseDriver<Connection>>

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

Parameters:#

NameType
entityAnyEntity<any>

Returns: EntityManager<IDatabaseDriver<Connection>>

Defined in: packages/core/src/entity/EntityRepository.ts:138


removeAndFlush#

removeAndFlush(entity: AnyEntity<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:#

NameType
entityAnyEntity<any>

Returns: Promise<void>

Defined in: packages/core/src/entity/EntityRepository.ts:146


removeLater#

removeLater(entity: AnyEntity<any>): 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()

Parameters:#

NameType
entityAnyEntity<any>

Returns: void

Defined in: packages/core/src/entity/EntityRepository.ts:156

Last updated on by Renovate Bot