Skip to main content

UpdatableCollection

Defined in: domain/UpdatableCollection.ts:28

Represents a collection of Create/Update entities. Represents a collection of deletable entities. Extends DomainCollection to provide additional functionality for creating and updating items.

Remarks

Use UpdatableCollection.getCollection to retrieve an instance of this collection.

Example

const organisations = await sdk.getOrganisations();
await organisations.refresh();
const allOrganisations = organisations.getItems();
const organisation = organisations.findById(1);
await organisations.create(new Organisation(2, "New Organisation"));
await organisations.update(new Organisation(2, "Updated Organisation"));
for (const organisation of organisations) { console.log(organisation); }
organisations.forEach((organisation, idx) => console.log(idx, organisation));

Extends

Extended by

Type Parameters

T

T extends Id<U>

The entity type, which must extend Id.

U

U

The type of the entity's unique identifier.

Constructors

Constructor

new UpdatableCollection<T, U>(transform): UpdatableCollection<T, U>;

Defined in: domain/UpdatableCollection.ts:36

Constructs a new UpdatableCollection.

Parameters

transform

Optional function to transform raw API data to entity type T.

null | (data) => T

Returns

UpdatableCollection<T, U>

Overrides

DomainCollection<T, U>.constructor

Accessors

length

Get Signature

get length(): number;

Defined in: domain/DomainCollection.ts:214

The number of items in the collection.

Returns

number

The item count.

Inherited from

DomainCollection.length

Methods

[iterator]()

iterator: Iterator<T>;

Defined in: domain/DomainCollection.ts:228

Iterator support for for...of loops.

Returns

Iterator<T>

Example

for (const item of collection) {
console.log(item);
}

Inherited from

DomainCollection.[iterator]


create()

create(item): Promise<T>;

Defined in: domain/UpdatableCollection.ts:46

Creates a new item in the collection and adds it locally.

Parameters

item

T

The item to create.

Returns

Promise<T>

A promise that resolves to the created item.

Throws

If an item with the same ID already exists.


findById()

findById(id): undefined | T;

Defined in: domain/DomainCollection.ts:186

Finds an item in the collection by its ID.

Parameters

id

U

The unique identifier of the item.

Returns

undefined | T

The found item, or undefined if not found.

Inherited from

DomainCollection.findById


forEach()

forEach(callback): void;

Defined in: domain/DomainCollection.ts:244

Executes a provided function once for each item in the collection.

Parameters

callback

(item, index, collection) => void

Function to execute for each item.

Returns

void

Example

collection.forEach((item, idx) => {
console.log(idx, item);
});

Inherited from

DomainCollection.forEach


get()

get(index): undefined | T;

Defined in: domain/DomainCollection.ts:205

Gets the item at the specified index.

Parameters

index

number

The zero-based index of the item.

Returns

undefined | T

The item at the given index, or undefined if out of bounds.

Inherited from

DomainCollection.get


getItems()

getItems(): T[];

Defined in: domain/DomainCollection.ts:195

Gets all items in the collection.

Returns

T[]

A shallow copy of the array of items.

Inherited from

DomainCollection.getItems


map()

map<R>(fn): R[];

Defined in: domain/DomainCollection.ts:254

Creates a new array populated with the results of calling a provided function on every item.

Type Parameters

R

R

Parameters

fn

(item, index, collection) => R

Function that produces an element of the new array.

Returns

R[]

A new array with each element being the result of the callback function.

Inherited from

DomainCollection.map


refresh()

refresh(): Promise<void>;

Defined in: domain/DomainCollection.ts:174

Refreshes the collection by fetching items from the API.

Returns

Promise<void>

A promise that resolves when the items are refreshed.

Remarks

  • If transform is provided, raw API results are mapped through it.
  • If transform is omitted, API responses must already conform to type T.

Throws

If the API response is not an array.

Inherited from

DomainCollection.refresh


toArray()

toArray(): T[];

Defined in: domain/DomainCollection.ts:263

Returns a shallow copy of all items as a plain array.

Returns

T[]

A new array containing the same items.

Inherited from

DomainCollection.toArray


update()

update(update): Promise<null | T>;

Defined in: domain/UpdatableCollection.ts:68

Updates an existing item in the collection.

Parameters

update

T

The updated item.

Returns

Promise<null | T>

A promise that resolves to the updated item, or null if not found.


getCollection()

static getCollection<T, U, Self>(this, transform): Self & {
[n: number]: undefined | T;
length: number;
};

Defined in: domain/DomainCollection.ts:83

Static factory to create a proxied, array-like collection instance.

The returned proxy enables:

  • Numeric index access (collection[0])
  • Iteration (for...of)
  • Length property (collection.length)

Type Parameters

T

T extends Id<U>

U

U

Self

Self extends DomainCollection<T, U>

Parameters

this

(transform) => Self

The concrete subclass constructor.

transform

Optional function to transform raw API data into entities.

null | (data) => T

Returns

Self & { [n: number]: undefined | T; length: number; }

A proxied DomainCollection with array-like behavior.

Example

const orgs = Organisations.create();
console.log(orgs.length); // behaves like an array
console.log(orgs[0]); // index access

Inherited from

DomainCollection.getCollection