Skip to main content

ModelLinksArray

@webda/models


Class: ModelLinksArray<T, K>

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

Link to a collection of objects including some additional data

Uses composition instead of extending Array for better maintainability, performance, and type safety. Provides array-like interface through explicit methods and iterators.

It does not require to have a symetric relation on the other side The array contains uuid of the linked objects and additional properties defined as K

Sample usage:

class MyModel extends AbstractCoreModel {
_links: ModelLinksArray<OtherModel, { custom: string }>;
}

Type Parameters​

T​

T extends Storable

K​

K extends object

Implements​

Indexable​

[index: number]: ModelRefCustomProperties<T, K>

Support indexed access like an array

Constructors​

Constructor​

new ModelLinksArray<T, K>(repo, content?): ModelLinksArray<T, K>

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

Create a new ModelLinksCustomArray

Parameters​

repo​

Repository<ModelClass<T>>

the repository for the linked model

content?​

Pick<T, T[typeof WEBDA_PRIMARY_KEY][number]> & object & K[] = []

initial array of primary keys with custom data

Returns​

ModelLinksArray<T, K>

Properties​

[RelationAttributes]​

[RelationAttributes]: K

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


[RelationRole]​

[RelationRole]: "CustomLinks"

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

Implementation of​

ModelLinker.[RelationRole]


repo​

protected repo: Repository<ModelClass<T>>

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

the repository for the linked model

Accessors​

length​

Get Signature​

get length(): number

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

Get the number of items

Returns​

number

the item count

Methods​

[iterator]()​

[iterator](): Iterator<ModelRefCustomProperties<T, K>>

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

Make the collection iterable (for...of loops)

Returns​

Iterator<ModelRefCustomProperties<T, K>>

an iterator over the items


add()​

add(model): void

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

Add a model to the collection

Parameters​

model​

JSONed<ModelRefCustomProperties<T, K>>

the model to add

Returns​

void

Deprecated​

use push instead


at()​

at(index): ModelRefCustomProperties<T, K> | undefined

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

Get item at index

Parameters​

index​

number

the array index

Returns​

ModelRefCustomProperties<T, K> | undefined

the item, or undefined


every()​

every(predicate): boolean

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

Check if all items match predicate

Parameters​

predicate​

(item, index, array) => boolean

the test predicate

Returns​

boolean

true if all items match


filter()​

filter(predicate): ModelRefCustomProperties<T, K>[]

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

Filter items (does NOT mutate)

Parameters​

predicate​

(item, index, array) => boolean

the filter predicate

Returns​

ModelRefCustomProperties<T, K>[]

the filtered items


find()​

find(predicate): ModelRefCustomProperties<T, K> | undefined

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

Find item by predicate

Parameters​

predicate​

(item, index, array) => boolean

the search predicate

Returns​

ModelRefCustomProperties<T, K> | undefined

the found item, or undefined


findIndex()​

findIndex(predicate): number

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

Find index by predicate

Parameters​

predicate​

(item, index, array) => boolean

the search predicate

Returns​

number

the index, or -1


forEach()​

forEach(callback): void

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

Execute callback for each item

Parameters​

callback​

(item, index, array) => void

the function to execute for each item

Returns​

void


getModelRef()​

protected getModelRef(model): ModelRefCustomProperties<T, K>

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

Convert input into a ModelRefCustom, preserving custom properties

Parameters​

model​

ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>

the input to convert

Returns​

ModelRefCustomProperties<T, K>

the model reference with custom properties


includes()​

includes(search, fromIndex?): boolean

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

Check if item exists in collection

Parameters​

ModelRefCustomProperties<T, K>

the item to look for

fromIndex?​

number

optional start index

Returns​

boolean

true if found


map()​

map<U>(callback): U[]

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

Map over items (does NOT mutate)

Type Parameters​

U​

U

Parameters​

callback​

(item, index, array) => U

the mapping function

Returns​

U[]

the mapped array


pop()​

pop(): ModelRefCustomProperties<T, K> | undefined

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

Remove and return the last item

Returns​

ModelRefCustomProperties<T, K> | undefined

the removed item, or undefined


push()​

push(...items): number

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

Add items to the end of the collection

Parameters​

items​

...(ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>)[]

items to add

Returns​

number

the new collection length


remove()​

remove(model): boolean

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

Remove item by reference or primary key

Parameters​

model​

T | ModelRefCustomProperties<T, K> | PrimaryKeyType<T>

the item to remove

Returns​

boolean

true if found and removed


setDirty()​

protected setDirty(): void

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

Mark the parent model's property for this collection as dirty

Returns​

void


shift()​

shift(): ModelRefCustomProperties<T, K> | undefined

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

Remove and return the first item

Returns​

ModelRefCustomProperties<T, K> | undefined

the removed item, or undefined


some()​

some(predicate): boolean

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

Check if any item matches predicate

Parameters​

predicate​

(item, index, array) => boolean

the test predicate

Returns​

boolean

true if any item matches


splice()​

splice(start, deleteCount?, ...rest): ModelRefCustomProperties<T, K>[]

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

Remove/add items at specified index

Parameters​

start​

number

the start index

deleteCount?​

number

number of items to remove

rest​

...(ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>)[]

items to insert

Returns​

ModelRefCustomProperties<T, K>[]

the removed items


toJSON()​

toJSON(): JSONed<ModelRefCustomProperties<T, K>>[]

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

Convert to JSON

Returns​

JSONed<ModelRefCustomProperties<T, K>>[]

array of serialized items


unshift()​

unshift(...items): number

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

Add items to the beginning of the collection

Parameters​

items​

...(ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>)[]

items to prepend

Returns​

number

the new collection length