Skip to main content

ModelRef

@webda/models


Class: ModelRef<T>

Defined in: packages/models/src/relations.ts:47

A reference to a model

Extended by​

Type Parameters​

T​

T extends Storable

Constructors​

Constructor​

new ModelRef<T>(key, repository?, parent?): ModelRef<T>

Defined in: packages/models/src/relations.ts:58

Create a new model reference

Parameters​

key​

PrimaryKeyType<T>

Primary key of the referenced model

repository?​

Repository<ModelClass<T>>

Repository to resolve the reference against

parent?​

Storable<any, any>

Parent storable that owns this reference

Returns​

ModelRef<T>

Properties​

[RelationKey]?​

optional [RelationKey]?: PrimaryKeyType<T>

Defined in: packages/models/src/relations.ts:49


[RelationRepository]?​

optional [RelationRepository]?: Repository<ModelClass<T>>

Defined in: packages/models/src/relations.ts:50

Methods​

delete()​

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

Defined in: packages/models/src/relations.ts:159

Delete the model

If a condition is specified, it will be used to check if the model can be deleted

Type Parameters​

A​

A extends string

Parameters​

conditionField?​

A

optional field for optimistic locking

condition?​

PropertyPathType<T, A> | JSONed<PropertyPathType<T, A>>

expected value for the condition field

Returns​

Promise<void>

a promise resolving when complete


deleteItemFromCollection()​

deleteItemFromCollection<K, L>(collection, index, itemWriteConditionField?, itemWriteCondition?): Promise<void>

Defined in: packages/models/src/relations.ts:224

Remove an item from a collection

Type Parameters​

K​

K extends string

L​

L extends string | number | symbol

Parameters​

collection​

K

the collection field name

index​

number

the position to remove from

itemWriteConditionField?​

L

optional field for item write condition

itemWriteCondition?​

ArrayElement<T[K]>[L]

expected value for the item write condition

Returns​

Promise<void>

a promise resolving when complete


exists()​

exists(): Promise<boolean>

Defined in: packages/models/src/relations.ts:170

Check if the model exists in the repository

Returns​

Promise<boolean>

true if the model exists


get()​

get(): Promise<T>

Defined in: packages/models/src/relations.ts:146

Get the model referenced by the key

Returns​

Promise<T>

the resolved model instance


getKey()​

protected getKey(): PrimaryKeyType<T>

Defined in: packages/models/src/relations.ts:79

Get the primary key and ensure it's initialized

Returns​

PrimaryKeyType<T>

the primary key value


getPrimaryKey()​

getPrimaryKey(): PrimaryKeyType<T>

Defined in: packages/models/src/relations.ts:87

Get the model

Returns​

PrimaryKeyType<T>

the primary key value


getRepository()​

protected getRepository(): Repository<ModelClass<T>>

Defined in: packages/models/src/relations.ts:68

Get the repository and ensure it's initialized

Returns​

Repository<ModelClass<T>>

the repository instance


incrementAttribute()​

incrementAttribute(property, value?, conditionField?, condition?): Promise<void | Record<string, number>>

Defined in: packages/models/src/relations.ts:261

Increment only one attribute of the model

Parameters​

property​

NumericPropertyPaths<T>

the numeric property to increment

value?​

number

the increment amount

conditionField?​

_NonFunctionPropertyPaths<T>

optional field for optimistic locking

condition?​

PropertyPathType<T, _NonFunctionPropertyPaths<T>>

expected value for the condition field

Returns​

Promise<void | Record<string, number>>

a promise resolving when complete


incrementAttributes()​

incrementAttributes(info, conditionField?, condition?): Promise<void | Record<string, number>>

Defined in: packages/models/src/relations.ts:181

Increment the attributes of the model

Parameters​

info​

(NumericPropertyPaths<T> | { property: NumericPropertyPaths<T>; value?: number; })[] | Record<NumericPropertyPaths<T>, number>

The attributes to increment

conditionField?​

_NonFunctionPropertyPaths<T>

optional field for optimistic locking

condition?​

PropertyPathType<T, _NonFunctionPropertyPaths<T>>

expected value for the condition field

Returns​

Promise<void | Record<string, number>>

a promise resolving when complete


patch()​

patch<K, A>(data, conditionField?, condition?): Promise<void>

Defined in: packages/models/src/relations.ts:119

Patch the model

Type Parameters​

K​

K extends string | number | symbol

A​

A extends string

Parameters​

data​

Partial<Omit<T, PrimaryKeyAttributes<T>>>

the partial data to patch

conditionField?​

A

optional field for optimistic locking

condition?​

PropertyPathType<T, A>

expected value for the condition field

Returns​

Promise<void>

a promise resolving when complete


removeAttribute()​

removeAttribute<A>(attribute, conditionField?, condition?): Promise<void>

Defined in: packages/models/src/relations.ts:246

Remove an attribute from the model

Type Parameters​

A​

A extends string

Parameters​

attribute​

Exclude<_NonFunctionPropertyPaths<T>, PrimaryKeyAttributes<T>>

the attribute to remove

conditionField?​

A

optional field for optimistic locking

condition?​

PropertyPathType<T, A>

expected value for the condition field

Returns​

Promise<void>

a promise resolving when complete


setAttribute()​

setAttribute<A>(attribute, value): Promise<void>

Defined in: packages/models/src/relations.ts:108

Set the attribute of the model

Type Parameters​

A​

A extends string

Parameters​

attribute​

A

the attribute path to set

value​

JSONed<PropertyPathType<T, A>>

the new attribute value

Returns​

Promise<void>

a promise resolving when complete


toJSON()​

toJSON(): PrimaryKeyType<T>

Defined in: packages/models/src/relations.ts:98

Serialize the reference to its primary key value

Returns​

PrimaryKeyType<T>

the primary key value


update()​

update<A>(data, conditionField?, condition?): Promise<void>

Defined in: packages/models/src/relations.ts:134

Fully replace the model data (except primary key fields)

Type Parameters​

A​

A extends string

Parameters​

data​

Omit<Helpers<T>, PrimaryKeyAttributes<T>>

Complete model data excluding primary key

conditionField?​

A

Optional field for optimistic locking

condition?​

PropertyPathType<T, A>

Expected value for the condition field

Returns​

Promise<void>

a promise resolving when complete


upsertItemToCollection()​

upsertItemToCollection<K, L>(collection, item, index?, itemWriteConditionField?, itemWriteCondition?): Promise<void>

Defined in: packages/models/src/relations.ts:199

Add an item to a collection

Type Parameters​

K​

K extends string

L​

L extends string | number | symbol

Parameters​

collection​

K

the collection field name

item​

ArrayElement<T[K]> | JSONed<ArrayElement<T[K]>>

the item to add or update

index?​

number

optional position to insert at

itemWriteConditionField?​

ArrayElement<T[K]> extends object ? L : never

optional field for item write condition

itemWriteCondition?​

ArrayElement<T[K]> extends object ? ArrayElement[L] : ArrayElement<T[K]> | null

expected value for the item write condition

Returns​

Promise<void>

a promise resolving when complete