Skip to main content

CoreRepository

@webda/models


Interface: CoreRepository<T>

Defined in: packages/models/src/repositories/repository.ts:38

Core repository interface - every repository must implement this

Contains only the essential CRUD operations that work across all storage backends (SQL, NoSQL, in-memory, etc.)

Extended by​

Type Parameters​

T​

T extends ModelClass = ModelClass

Properties​

[WEBDA_TEST]?​

optional [WEBDA_TEST]?: object

Defined in: packages/models/src/repositories/repository.ts:258

clear()​

clear(): Promise<void>

Returns​

Promise<void>

Methods​

aggregate()​

aggregate<G, M>(spec, params?): Promise<AggregationResult<AggregatedRow<InstanceType<T>, G, M>>>

Defined in: packages/models/src/repositories/repository.ts:162

Aggregate the store

repo.aggregate({ filter: "done = FALSE", groupBy: ["status"], metrics: { n: { count: "*" } } });

Type Parameters​

G​

G extends readonly _NonFunctionPropertyPaths<InstanceType<T>>[] = []

M​

M extends Record<string, MetricSpec<InstanceType<T>>> = Record<string, MetricSpec<InstanceType<T>>>

Parameters​

spec​

AggregationSpec<InstanceType<T>, G, M>

filter, group by paths, metrics, ordering and limit

params?​

QueryParameters

values for the placeholders of spec.filter

Returns​

Promise<AggregationResult<AggregatedRow<InstanceType<T>, G, M>>>

the aggregated rows and whether the backend computed them natively


configureAggregation()​

configureAggregation(options): void

Defined in: packages/models/src/repositories/repository.ts:195

Configure the aggregation fallback policy, called by the owning store

Parameters​

options​

Partial<AggregationOptions>

options to override

Returns​

void


configureMonitoring()?​

optional configureMonitoring(options): void

Defined in: packages/models/src/repositories/repository.ts:201

Configure the query monitoring policy, called by the owning store

Parameters​

options​

Partial<QueryMonitorOptions>

options to override

Returns​

void


count()​

count(filter?, params?, options?): Promise<CountResult>

Defined in: packages/models/src/repositories/repository.ts:185

Count the objects matching a filter

repo.count("status = 'open'", undefined, { max: 10000 }); // { value: 1234, relation: "eq", native: true }

ORDER BY, LIMIT and OFFSET of the filter are ignored; DELETE, UPDATE and SELECT field lists are refused. Backends without a native count scan the matching objects in memory, under the aggregation fallback policy. Counts bypass the model permissions: see countModelWithPermissions in @webda/core for a client total.

Parameters​

filter?​

WebdaQLString<InstanceType<T>>

the WebdaQL filter, optionally with ? or :name placeholders; empty counts everything

params?​

QueryParameters

values for the placeholders: an array for ?, an object for :name

options?​

CountOptions

max stops counting after max objects (relation "gte")

Returns​

Promise<CountResult>

the count, whether it is exact and whether the backend computed it natively

Throws​

AggregationError AGGREGATION_NOT_NATIVE when the backend has no native count and the policy is deny


create()​

create(data, save?): Promise<InstanceType<T>>

Defined in: packages/models/src/repositories/repository.ts:102

Create data in the store

Parameters​

data​

Helpers<InstanceType<T>>

save?​

boolean

if false, create in-memory object without saving

Returns​

Promise<InstanceType<T>>


delete()​

delete<K>(uid, conditionField?, condition?): Promise<void>

Defined in: packages/models/src/repositories/repository.ts:111

Delete data from the store

Type Parameters​

K​

K extends string

Parameters​

uid​

string | PrimaryKeyType<InstanceType<T>>

Primary key or UID string

conditionField?​

K | null

Field to check for condition (optional)

condition?​

PropertyPathType<InstanceType<T>, K> | JSONed<PropertyPathType<InstanceType<T>, K>>

Value to check for condition (optional)

Returns​

Promise<void>


deleteMany()​

deleteMany(statement, params?): Promise<number>

Defined in: packages/models/src/repositories/repository.ts:218

Delete every object matching a DELETE [WHERE ...] [LIMIT n] statement, in bulk

Bulk operations bypass the model layer: no per-object repository event (Delete/Deleted), no model hook, no behaviour and no validation run, and no permission (canAct) is checked. Repositories are trusted internal APIs: application code exposing a bulk statement to clients must check permissions itself.

Stores use their native batch operation when they have one, otherwise the matching keys are collected first (up to LIMIT) and deleted with the store's per-key primitive.

Parameters​

statement​

WebdaQLStatement<InstanceType<T>> | Query

the DELETE statement, optionally with ? or :name placeholders, or its parsed form

params?​

QueryParameters

values for the placeholders: an array for ?, an object for :name

Returns​

Promise<number>

the number of objects deleted

Throws​

if the statement is not a DELETE


emit()​

emit<K>(event, payload): Promise<void>

Defined in: packages/models/src/repositories/repository.ts:253

Type Parameters​

K​

K extends string | number | symbol

Parameters​

event​

K

payload​

InstanceType<T>[typeof WEBDA_EVENTS][K]

Returns​

Promise<void>


excludePrimaryKey()​

excludePrimaryKey(object): any

Defined in: packages/models/src/repositories/repository.ts:81

Remove the primary key from the object

Parameters​

object​

any

Returns​

any


exists()​

exists(uid): Promise<boolean>

Defined in: packages/models/src/repositories/repository.ts:122

Verify if the object exists

Parameters​

uid​

string | PrimaryKeyType<InstanceType<T>>

Returns​

Promise<boolean>


fromUID()​

fromUID(uid, forceObject?): ModelRefWithCreate<InstanceType<T>>

Defined in: packages/models/src/repositories/repository.ts:52

In REST API, composite keys are represented as a string with the format "key1#key2#key3" We need a way to convert this string to the object

Parameters​

uid​

string

serialized primary key

forceObject?​

boolean

if true, the result will be an object with the primary key fields

Returns​

ModelRefWithCreate<InstanceType<T>>


get()​

get(primaryKey): Promise<HelperProperties<InstanceType<T>>>

Defined in: packages/models/src/repositories/repository.ts:94

Get data from the store

Parameters​

primaryKey​

PrimaryKeyType<InstanceType<T>>

Returns​

Promise<HelperProperties<InstanceType<T>>>


getPrimaryKey()​

getPrimaryKey<K>(object, forceObject?): K extends false ? PrimaryKeyType<InstanceType<T>> : PrimaryKey<InstanceType<T>>

Defined in: packages/models/src/repositories/repository.ts:66

Extract the primary key from the object

Type Parameters​

K​

K

Parameters​

object​

any

to extract the primary key from

forceObject?​

boolean

if true, the result will be an object with the primary key fields

Returns​

K extends false ? PrimaryKeyType<InstanceType<T>> : PrimaryKey<InstanceType<T>>


getRootModel()​

getRootModel(): T

Defined in: packages/models/src/repositories/repository.ts:43

Get the root model class for this repository

Returns​

T


getUID()​

getUID(object): string

Defined in: packages/models/src/repositories/repository.ts:75

Get the UID of the object

Parameters​

object​

any

to extract the primary key from

Returns​

string


iterate()​

iterate(query, params?): AsyncGenerator<InstanceType<T>>

Defined in: packages/models/src/repositories/repository.ts:150

Iterate over the store

Takes a filter query (a = 1 ORDER BY b LIMIT 10): DELETE and UPDATE statements go to deleteMany / updateMany, and SELECT field lists are refused (objects are returned whole).

Parameters​

query​

WebdaQLString<InstanceType<T>>

the WebdaQL query, optionally with ? or :name placeholders

params?​

QueryParameters

values for the placeholders: an array for ?, an object for :name

Returns​

AsyncGenerator<InstanceType<T>>


off()​

off<K>(event, listener): void

Defined in: packages/models/src/repositories/repository.ts:249

Type Parameters​

K​

K extends string | number | symbol

Parameters​

event​

K

listener​

(data) => void

Returns​

void


on()​

on<K>(event, listener): void

Defined in: packages/models/src/repositories/repository.ts:241

Event listeners

Type Parameters​

K​

K extends string | number | symbol

Parameters​

event​

K

listener​

(data) => void

Returns​

void


once()​

once<K>(event, listener): void

Defined in: packages/models/src/repositories/repository.ts:245

Type Parameters​

K​

K extends string | number | symbol

Parameters​

event​

K

listener​

(data) => void

Returns​

void


parseUID()​

parseUID(uid, forceObject?): PrimaryKeyType<InstanceType<T>> | PrimaryKey<InstanceType<T>>

Defined in: packages/models/src/repositories/repository.ts:59

Parse the UID into the primary key

Parameters​

uid​

string

forceObject?​

boolean

Returns​

PrimaryKeyType<InstanceType<T>> | PrimaryKey<InstanceType<T>>


query()​

query(query, params?): Promise<{ continuationToken?: string; results: InstanceType<T>[]; }>

Defined in: packages/models/src/repositories/repository.ts:133

Query the store

Takes a filter query (a = 1 ORDER BY b LIMIT 10): DELETE and UPDATE statements go to deleteMany / updateMany, and SELECT field lists are refused (objects are returned whole).

Parameters​

query​

WebdaQLString<InstanceType<T>>

the WebdaQL query, optionally with ? or :name placeholders

params?​

QueryParameters

values for the placeholders: an array for ?, an object for :name

Returns​

Promise<{ continuationToken?: string; results: InstanceType<T>[]; }>


ref()​

ref(pk): ModelRefWithCreate<InstanceType<T>>

Defined in: packages/models/src/repositories/repository.ts:87

Refers to the object in the store

Parameters​

pk​

SettablePrimaryKey<InstanceType<T>>

primary key

Returns​

ModelRefWithCreate<InstanceType<T>>


updateMany()​

updateMany(statement, params?): Promise<number>

Defined in: packages/models/src/repositories/repository.ts:236

Update every object matching an UPDATE SET a = v, ... [WHERE ...] [LIMIT n] statement, in bulk

Bulk operations bypass the model layer: no per-object repository event (Patch/Patched/Updated), no model hook, no behaviour and no validation run, and no permission (canAct) is checked. Repositories are trusted internal APIs: application code exposing a bulk statement to clients must check permissions itself.

SET targets may not be a primary key field nor a private (__) field, and must be fields of the model when its metadata lists them. Dotted targets (profile.verified) set a nested value, creating the intermediate objects. Each matching object is updated once, even when the SET changes a field of the WHERE.

Parameters​

statement​

WebdaQLStatement<InstanceType<T>> | Query

the UPDATE statement, optionally with ? or :name placeholders, or its parsed form

params?​

QueryParameters

values for the placeholders: an array for ?, an object for :name

Returns​

Promise<number>

the number of objects updated

Throws​

if the statement is not an UPDATE, or a SET target is refused