Version: 4.3

Class: EntityRepository<T>

Type parameters

NameType
TAnyEntity<T>

Hierarchy

  • EntityRepository

Constructors

constructor

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

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

Parameters:

NameType
_emEntityManager
entityNameEntityName<T>

Returns: EntityRepository

Properties

_em

Protected Readonly _em: EntityManager

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


entityName

Protected Readonly entityName: EntityName<T>

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

Accessors

em

Protectedget em(): EntityManager

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

Returns: EntityManager

Methods

assign

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

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

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

Parameters:

NameType
entityT
dataEntityData<T>

Returns: T


canPopulate

canPopulate(property: string): boolean

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

Checks whether given property can be populated on the entity.

Parameters:

NameType
propertystring

Returns: boolean


count

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

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

Returns total number of entities matching your where query.

Parameters:

NameTypeDefault value
whereFilterQuery<T>{}
optionsCountOptions<T>{}

Returns: Promise<number>


create

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

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

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

Type parameters:

NameTypeDefault
PPopulate<T>string[]

Parameters:

NameType
dataEntityData<T>

Returns: New<T, P>


find

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

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

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

Type parameters:

NameTypeDefault
PPopulate<T>any

Parameters:

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

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

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

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

Finds all entities matching your where query.

Type parameters:

NameTypeDefault
PPopulate<T>any

Parameters:

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

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


findAll

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

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

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

Type parameters:

NameTypeDefault
PPopulate<T>any

Parameters:

NameType
options?FindOptions<T, P>

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

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

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

Finds all entities of given type.

Type parameters:

NameTypeDefault
PPopulate<T>any

Parameters:

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

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


findAndCount

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

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

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
PPopulate<T>any

Parameters:

NameType
whereFilterQuery<T>
options?FindOptions<T>

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

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

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

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
PPopulate<T>any

Parameters:

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

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


findOne

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

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

Finds first entity matching your where query.

Type parameters:

NameTypeDefault
PPopulate<T>any

Parameters:

NameType
whereFilterQuery<T>
populate?P
orderBy?QueryOrderMap

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

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

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

Finds first entity matching your where query.

Type parameters:

NameTypeDefault
PPopulate<T>any

Parameters:

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

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


findOneOrFail

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

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

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
PPopulate<T>any

Parameters:

NameType
whereFilterQuery<T>
populate?P
orderBy?QueryOrderMap

Returns: Promise<Loaded<T, P>>

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

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

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
PPopulate<T>any

Parameters:

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

Returns: Promise<Loaded<T, P>>


flush

flush(): Promise<void>

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

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>


getReference

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

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

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
PKkeyof T

Parameters:

NameType
idPrimary<T>
wrappedtrue

Returns: IdentifiedReference<T, PK>

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

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

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
PKkeyof Tkeyof T

Parameters:

NameType
idPrimary<T>

Returns: T

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

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

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
PKkeyof Tkeyof T

Parameters:

NameType
idPrimary<T>
wrappedfalse

Returns: T


map

map(result: EntityData<T>): T

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

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

Parameters:

NameType
resultEntityData<T>

Returns: T


merge

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

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

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


nativeDelete

nativeDelete(where: FilterQuery<T> | any): Promise<number>

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

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

Parameters:

NameType
whereFilterQuery<T> | any

Returns: Promise<number>


nativeInsert

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

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

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

Parameters:

NameType
dataEntityData<T>

Returns: Promise<Primary<T>>


nativeUpdate

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

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

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

Parameters:

NameType
whereFilterQuery<T>
dataEntityData<T>

Returns: Promise<number>


persist

persist(entity: AnyEntity | AnyEntity[]): EntityManager

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

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 | AnyEntity[]

Returns: EntityManager


persistAndFlush

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

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

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

Parameters:

NameType
entityAnyEntity | AnyEntity[]

Returns: Promise<void>


persistLater

persistLater(entity: AnyEntity | AnyEntity[]): void

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

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 | AnyEntity[]

Returns: void


populate

populate<P>(entities: T, populate: P, where?: FilterQuery<T>, orderBy?: QueryOrderMap, refresh?: boolean, validate?: boolean): Promise<Loaded<T, P>>

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

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 | keyof T | Populate<T>

Parameters:

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

Returns: Promise<Loaded<T, P>>

populate<P>(entities: T[], populate: P, where?: FilterQuery<T>, orderBy?: QueryOrderMap, refresh?: boolean, validate?: boolean): Promise<Loaded<T, P>[]>

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

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 | keyof T | Populate<T>

Parameters:

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

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

populate<P>(entities: T | T[], populate: P, where?: FilterQuery<T>, orderBy?: QueryOrderMap, refresh?: boolean, validate?: boolean): Promise<Loaded<T, P> | Loaded<T, P>[]>

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

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 | keyof T | Populate<T>

Parameters:

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

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


remove

remove(entity: AnyEntity): EntityManager

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

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

Returns: EntityManager


removeAndFlush

removeAndFlush(entity: AnyEntity): Promise<void>

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

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

Parameters:

NameType
entityAnyEntity

Returns: Promise<void>


removeLater

removeLater(entity: AnyEntity): void

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

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

Returns: void

Last updated on by Martin Adámek