#
Class: EntityRepository<T>knex.EntityRepository
#
Type parametersName |
---|
T |
#
Hierarchy↳ EntityRepository
#
Constructors#
constructor+ new EntityRepository<T>(_em
: EntityManager<AbstractSqlDriver<AbstractSqlConnection>>, entityName
: EntityName<T>): EntityRepository<T>
#
Type parameters:Name |
---|
T |
#
Parameters:Name | Type |
---|---|
_em | EntityManager<AbstractSqlDriver<AbstractSqlConnection>> |
entityName | EntityName<T> |
Returns: EntityRepository<T>
Inherited from: EntityRepository
Defined in: packages/knex/src/SqlEntityRepository.ts:6
#
Properties#
_em• Protected
Readonly
_em: EntityManager<AbstractSqlDriver<AbstractSqlConnection>>
Inherited from: EntityRepository._em
#
entityName• Protected
Readonly
entityName: EntityName<T>
Inherited from: EntityRepository.entityName
#
Accessors#
em• em(): EntityManager<AbstractSqlDriver<AbstractSqlConnection>>
Returns: EntityManager<AbstractSqlDriver<AbstractSqlConnection>>
Defined in: packages/knex/src/SqlEntityRepository.ts:27
#
Methods#
assignâ–¸ assign(entity
: T, data
: EntityData<T>): T
Shortcut for wrap(entity).assign(data, { em })
#
Parameters:Name | Type |
---|---|
entity | T |
data | EntityData<T> |
Returns: T
Inherited from: EntityRepository
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:Name | Type |
---|---|
property | string |
Returns: boolean
Inherited from: EntityRepository
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:Name | Type | Default value |
---|---|---|
where | FilterQuery<T> | ... |
options | CountOptions<T> | ... |
Returns: Promise<number>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | string[] |
#
Parameters:Name | Type |
---|---|
data | EntityData<T> |
Returns: Loaded<T, P>
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:252
#
createQueryBuilderâ–¸ createQueryBuilder(alias?
: string): QueryBuilder<T>
Creates a QueryBuilder instance
#
Parameters:Name | Type |
---|---|
alias? | string |
Returns: QueryBuilder<T>
Defined in: packages/knex/src/SqlEntityRepository.ts:16
#
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
options? | FindOptions<T, P> |
Returns: Promise<Loaded<T, P>[]>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
populate? | P |
orderBy? | QueryOrderMap |
limit? | number |
offset? | number |
Returns: Promise<Loaded<T, P>[]>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
options? | FindOptions<T, P> |
Returns: Promise<Loaded<T, P>[]>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
populate? | P |
orderBy? | QueryOrderMap |
limit? | number |
offset? | number |
Returns: Promise<Loaded<T, P>[]>
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:123
#
findAndCountâ–¸ findAndCount<P>(where
: FilterQuery<T>, options?
: FindOptions<T, Populate<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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
options? | FindOptions<T, Populate<T>> |
Returns: Promise<[Loaded<T, P>[], number]>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
populate? | P |
orderBy? | QueryOrderMap |
limit? | number |
offset? | number |
Returns: Promise<[Loaded<T, P>[], number]>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
populate? | P |
orderBy? | QueryOrderMap |
Returns: Promise<null | Loaded<T, P>>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
populate? | FindOneOptions<T, P> |
orderBy? | QueryOrderMap |
Returns: Promise<null | Loaded<T, P>>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
populate? | P |
orderBy? | QueryOrderMap |
Returns: Promise<Loaded<T, P>>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
P | Populate<T> | any |
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
populate? | FindOneOrFailOptions<T, P> |
orderBy? | QueryOrderMap |
Returns: Promise<Loaded<T, P>>
Inherited from: EntityRepository
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>
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:166
#
getKnexâ–¸ getKnex(type?
: read | write): Knex<any, unknown[]>
Returns configured knex instance.
#
Parameters:Name | Type |
---|---|
type? | read | write |
Returns: Knex<any, unknown[]>
Defined in: packages/knex/src/SqlEntityRepository.ts:23
#
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:Name | Type |
---|---|
PK | string | number | symbol |
#
Parameters:Name | Type |
---|---|
id | Primary<T> |
wrapped | true |
Returns: IdentifiedReference<T, PK>
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
PK | string | number | symbol | keyof T |
#
Parameters:Name | Type |
---|---|
id | Primary<T> |
Returns: T
Inherited from: EntityRepository
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:Name | Type | Default |
---|---|---|
PK | string | number | symbol | keyof T |
#
Parameters:Name | Type |
---|---|
id | Primary<T> |
wrapped | false |
Returns: T
Inherited from: EntityRepository
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:Name | Type |
---|---|
result | EntityData<T> |
Returns: T
Inherited from: EntityRepository
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:Name | Type |
---|---|
data | T | EntityData<T> |
refresh? | boolean |
convertCustomTypes? | boolean |
Returns: T
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:267
#
nativeDeleteâ–¸ nativeDelete(where
: any): Promise<number>
Fires native delete query. Calling this has no side effects on the context (identity map).
#
Parameters:Name | Type |
---|---|
where | any |
Returns: Promise<number>
Inherited from: EntityRepository
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:Name | Type |
---|---|
data | EntityData<T> |
Returns: Promise<Primary<T>>
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:173
#
nativeUpdateâ–¸ nativeUpdate(where
: FilterQuery<T>, data
: EntityData<T>): Promise<number>
Fires native update query. Calling this has no side effects on the context (identity map).
#
Parameters:Name | Type |
---|---|
where | FilterQuery<T> |
data | EntityData<T> |
Returns: Promise<number>
Inherited from: EntityRepository
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:Name | Type |
---|---|
entity | AnyEntity<any> | AnyEntity<any>[] |
Returns: EntityManager<IDatabaseDriver<Connection>>
Inherited from: EntityRepository
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:Name | Type |
---|---|
entity | AnyEntity<any> | AnyEntity<any>[] |
Returns: Promise<void>
Inherited from: EntityRepository
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:Name | Type |
---|---|
entity | AnyEntity<any> | AnyEntity<any>[] |
Returns: void
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:34
#
populateâ–¸ populate<P>(entities
: T, populate
: P, where?
: { __@PrimaryKeyType@41543?
: any } | NonNullable<Query<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:Name | Type |
---|---|
P | string | number | boolean | symbol | readonly string[] | readonly keyof T[] | PopulateChildren<T> |
#
Parameters:Name | Type |
---|---|
entities | T |
populate | P |
where? | { __@PrimaryKeyType@41543? : any } | NonNullable<Query<T>> |
orderBy? | QueryOrderMap |
refresh? | boolean |
validate? | boolean |
Returns: Promise<Loaded<T, P>>
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:230
â–¸ populate<P>(entities
: T[], populate
: P, where?
: { __@PrimaryKeyType@41543?
: any } | NonNullable<Query<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:Name | Type |
---|---|
P | string | number | boolean | symbol | readonly string[] | readonly keyof T[] | PopulateChildren<T> |
#
Parameters:Name | Type |
---|---|
entities | T[] |
populate | P |
where? | { __@PrimaryKeyType@41543? : any } | NonNullable<Query<T>> |
orderBy? | QueryOrderMap |
refresh? | boolean |
validate? | boolean |
Returns: Promise<Loaded<T, P>[]>
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:235
â–¸ populate<P>(entities
: T | T[], populate
: P, where?
: { __@PrimaryKeyType@41543?
: any } | NonNullable<Query<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:Name | Type |
---|---|
P | string | number | boolean | symbol | readonly string[] | readonly keyof T[] | PopulateChildren<T> |
#
Parameters:Name | Type |
---|---|
entities | T | T[] |
populate | P |
where? | { __@PrimaryKeyType@41543? : any } | NonNullable<Query<T>> |
orderBy? | QueryOrderMap |
refresh? | boolean |
validate? | boolean |
Returns: Promise<Loaded<T, P> | Loaded<T, P>[]>
Inherited from: EntityRepository
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:Name | Type |
---|---|
entity | AnyEntity<any> |
Returns: EntityManager<IDatabaseDriver<Connection>>
Inherited from: EntityRepository
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:Name | Type |
---|---|
entity | AnyEntity<any> |
Returns: Promise<void>
Inherited from: EntityRepository
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:Name | Type |
---|---|
entity | AnyEntity<any> |
Returns: void
Inherited from: EntityRepository
Defined in: packages/core/src/entity/EntityRepository.ts:156