Skip to main content

ModelLinksArray

@webda/models


Class: ModelLinksArray<T, K>

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

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:949

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:937

[relation attributes]


[RelationRole]​

[RelationRole]: "CustomLinks"

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

[relation role]

Implementation of​

ModelLinker.[RelationRole]


repo​

protected repo: Repository<ModelClass<T>>

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

the repository for the linked model

Accessors​

length​

Get Signature​

get length(): number

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

Get the number of items

Returns​

number

the item count

Methods​

[iterator]()​

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

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

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:1001

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:1099

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:1183

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:1151

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:1108

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:1119

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:1161

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:1215

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:1131

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:1140

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:1020

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:1010

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:1075

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:1228

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:1032

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:1172

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:1058

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:1193

Convert to JSON

Returns​

JSONed<ModelRefCustomProperties<T, K>>[]

array of serialized items


unshift()​

unshift(...items): number

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

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