Skip to main content

ModelRefWithCreate

@webda/models


Class: ModelRefWithCreate<T>

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

Model reference with create and upsert methods

Extends​

Type Parameters​

T​

T extends Storable

Constructors​

Constructor​

new ModelRefWithCreate<T>(key, repository?, parent?): ModelRefWithCreate<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​

ModelRefWithCreate<T>

Inherited from​

ModelRef.constructor

Properties​

[RelationKey]?​

optional [RelationKey]?: PrimaryKeyType<T>

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

Inherited from​

ModelRef.[RelationKey]


[RelationRepository]?​

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

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

Inherited from​

ModelRef.[RelationRepository]

Methods​

create()​

create(data): Promise<T>

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

Create a model reference

It will fail if the model already exists

Parameters​

data​

Omit<AttributesArgument<T>, T[typeof WEBDA_PRIMARY_KEY][number]>

the model data excluding primary key

Returns​

Promise<T>

the created model instance


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

Inherited from​

ModelRef.delete


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

Inherited from​

ModelRef.deleteItemFromCollection


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

Inherited from​

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

Inherited from​

ModelRef.get


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

Inherited from​

ModelRef.getKey


getPrimaryKey()​

getPrimaryKey(): PrimaryKeyType<T>

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

Get the model

Returns​

PrimaryKeyType<T>

the primary key value

Inherited from​

ModelRef.getPrimaryKey


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

Inherited from​

ModelRef.getRepository


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

Inherited from​

ModelRef.incrementAttribute


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

Inherited from​

ModelRef.incrementAttributes


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

Inherited from​

ModelRef.patch


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

Inherited from​

ModelRef.removeAttribute


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

Inherited from​

ModelRef.setAttribute


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

Inherited from​

ModelRef.toJSON


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

Inherited from​

ModelRef.update


upsert()​

upsert(data): Promise<T>

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

Upsert a model reference

Parameters​

data​

Omit<JSONed<T>, T[typeof WEBDA_PRIMARY_KEY][number]>

the model data excluding primary key

Returns​

Promise<T>

the upserted model instance


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 ? object & ArrayElement<T[K]>[L] : ArrayElement<T[K]> | null

expected value for the item write condition

Returns​

Promise<void>

a promise resolving when complete

Inherited from​

ModelRef.upsertItemToCollection