Version: 4.3

Class: SqlEntityRepository<T>

Type parameters

Name
T

Hierarchy

  • EntityRepository<T>

    SqlEntityRepository

Constructors

constructor

+ new SqlEntityRepository(_em: SqlEntityManager, entityName: EntityName<T>): SqlEntityRepository

Overrides void

Defined in packages/knex/src/SqlEntityRepository.ts:6

Parameters:

NameType
_emSqlEntityManager
entityNameEntityName<T>

Returns: SqlEntityRepository

Properties

_em

Protected Readonly _em: SqlEntityManager

Overrides void

Defined in packages/knex/src/SqlEntityRepository.ts:8


entityName

Protected Readonly entityName: EntityName<T>

Overrides void

Defined in packages/knex/src/SqlEntityRepository.ts:9

Accessors

em

Protectedget em(): SqlEntityManager

Overrides void

Defined in packages/knex/src/SqlEntityRepository.ts:27

Returns: SqlEntityManager

Methods

assign

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

Inherited from SqlEntityRepository.assign

Defined in packages/core/dist/entity/EntityRepository.d.ts:150

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

Parameters:

NameType
entityT
dataEntityData<T>

Returns: T


canPopulate

canPopulate(property: string): boolean

Inherited from SqlEntityRepository.canPopulate

Defined in packages/core/dist/entity/EntityRepository.d.ts:130

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>

Inherited from SqlEntityRepository.count

Defined in packages/core/dist/entity/EntityRepository.d.ts:159

Returns total number of entities matching your where query.

Parameters:

NameType
where?FilterQuery<T>
options?CountOptions<T>

Returns: Promise<number>


create

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

Inherited from SqlEntityRepository.create

Defined in packages/core/dist/entity/EntityRepository.d.ts:146

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>


createQueryBuilder

createQueryBuilder(alias?: string): QueryBuilder<T>

Defined in packages/knex/src/SqlEntityRepository.ts:16

Creates a QueryBuilder instance

Parameters:

NameType
alias?string

Returns: QueryBuilder<T>


find

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

Inherited from SqlEntityRepository.find

Defined in packages/core/dist/entity/EntityRepository.d.ts:50

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

Inherited from SqlEntityRepository.find

Defined in packages/core/dist/entity/EntityRepository.d.ts:54

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

Inherited from SqlEntityRepository.findAll

Defined in packages/core/dist/entity/EntityRepository.d.ts:68

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

Inherited from SqlEntityRepository.findAll

Defined in packages/core/dist/entity/EntityRepository.d.ts:72

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

Inherited from SqlEntityRepository.findAndCount

Defined in packages/core/dist/entity/EntityRepository.d.ts:59

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

Inherited from SqlEntityRepository.findAndCount

Defined in packages/core/dist/entity/EntityRepository.d.ts:64

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>

Inherited from SqlEntityRepository.findOne

Defined in packages/core/dist/entity/EntityRepository.d.ts:30

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>

Inherited from SqlEntityRepository.findOne

Defined in packages/core/dist/entity/EntityRepository.d.ts:34

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

Inherited from SqlEntityRepository.findOneOrFail

Defined in packages/core/dist/entity/EntityRepository.d.ts:40

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

Inherited from SqlEntityRepository.findOneOrFail

Defined in packages/core/dist/entity/EntityRepository.d.ts:46

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>

Inherited from SqlEntityRepository.flush

Defined in packages/core/dist/entity/EntityRepository.d.ts:98

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>


getKnex

getKnex(type?: "read" | "write"): Knex

Defined in packages/knex/src/SqlEntityRepository.ts:23

Returns configured knex instance.

Parameters:

NameType
type?"read" | "write"

Returns: Knex


getReference

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

Inherited from SqlEntityRepository.getReference

Defined in packages/core/dist/entity/EntityRepository.d.ts:118

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

Inherited from SqlEntityRepository.getReference

Defined in packages/core/dist/entity/EntityRepository.d.ts:122

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

Inherited from SqlEntityRepository.getReference

Defined in packages/core/dist/entity/EntityRepository.d.ts:126

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

Inherited from SqlEntityRepository.map

Defined in packages/core/dist/entity/EntityRepository.d.ts:114

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

Inherited from SqlEntityRepository.merge

Defined in packages/core/dist/entity/EntityRepository.d.ts:155

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>

Inherited from SqlEntityRepository.nativeDelete

Defined in packages/core/dist/entity/EntityRepository.d.ts:110

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

Inherited from SqlEntityRepository.nativeInsert

Defined in packages/core/dist/entity/EntityRepository.d.ts:102

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>

Inherited from SqlEntityRepository.nativeUpdate

Defined in packages/core/dist/entity/EntityRepository.d.ts:106

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

Inherited from SqlEntityRepository.persist

Defined in packages/core/dist/entity/EntityRepository.d.ts:14

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>

Inherited from SqlEntityRepository.persistAndFlush

Defined in packages/core/dist/entity/EntityRepository.d.ts:19

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

Inherited from SqlEntityRepository.persistLater

Defined in packages/core/dist/entity/EntityRepository.d.ts:26

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

Inherited from SqlEntityRepository.populate

Defined in packages/core/dist/entity/EntityRepository.d.ts:134

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

Inherited from SqlEntityRepository.populate

Defined in packages/core/dist/entity/EntityRepository.d.ts:138

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

Inherited from SqlEntityRepository.populate

Defined in packages/core/dist/entity/EntityRepository.d.ts:142

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

Inherited from SqlEntityRepository.remove

Defined in packages/core/dist/entity/EntityRepository.d.ts:79

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>

Inherited from SqlEntityRepository.removeAndFlush

Defined in packages/core/dist/entity/EntityRepository.d.ts:84

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

Inherited from SqlEntityRepository.removeLater

Defined in packages/core/dist/entity/EntityRepository.d.ts:91

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