Skip to content

BaseWriteItem

Presents one Item with the operations that modify it inside the current read-write execution.

Every write returns a fresh Item reflecting the stored result; the instance the method was called on is not updated in place. Use the returned Item, or a fresh lookup, for anything that follows.

Unit of Work

TItemInstanceTypes extends ItemInstanceTypes = ItemInstanceTypes

The generated map from Template PRN to Item type, used to type the children returned by createChild and the keyed upsert methods.

readonly createdAt: Date

When the Item was created, taken from the execution time of the execution that created it.

BaseReadItem.createdAt


readonly definition: TemplateDefinition

The Template definition the Item was created from, with any extension Properties of its parent merged in.

BaseReadItem.definition


readonly events: Record<string, WriteEventPublishInstance>

Publishers for the Events declared on the Item’s Template and on Unions it belongs to, keyed by Event key. A Template’s created is not included, as only the Runtime publishes it.


readonly id: string

The Item identifier, a UUID.

BaseReadItem.id


readonly properties: PropertyMap

Plain Property values keyed by Property key, ready for application logic.

Select values are reduced to option keys: a single-select reads as its option key or null, a multi-select as an array of option keys. instant and datetime values are Luxon DateTime objects. A Property that is unset, invalid or not granted to the principal reads as undefined.

PropertyMap

BaseReadItem.properties


readonly propertyValues: PropertyValueMap

Property values keyed by Property key, each tagged with its data type.

Use this form when the data type is significant, for example to tell an unset value from a stored value that has become invalid. Properties the principal is not granted are absent.

PropertyValue

BaseReadItem.propertyValues


readonly relations: Record<string, WriteRelationInstance>

Relations declared on the Item’s Template, keyed by Relation key, with write operations for this side.

BaseReadItem.relations


readonly state: WriteStateInstance<WriteItem<ItemInstanceTypes>, string | null, {[transition: string]: any; }> | null

The Item’s current state with its available transitions, or null when the Template declares no states.

BaseReadItem.state


readonly templatePrn: string

The PRN of the Template the Item was created from.

BaseReadItem.templatePrn


readonly workspaceId: string

The identifier of the Workspace holding the Item.

BaseReadItem.workspaceId

assertRole(role): Promise<true>

Confirms that the active principal holds a Template Role on this Item.

TemplateRoleInstance

A Role from the Template’s roles.

Promise<true>

true when the Role is held.

AssertRoleFailedError when the active principal does not hold the Role on this Item.

BaseReadItem.assertRole


assignParent(parent): Promise<WriteItem<ItemInstanceTypes>>

Moves this Item under a new parent.

ReadItem

The Item to become the parent; it must not be this Item or one of its descendants.

Promise<WriteItem<ItemInstanceTypes>>

The moved Item.

(error-009-parent-child-circular-reference) when the new parent is this Item or descends from it.


checkRole(role): Promise<boolean>

Reports whether the active principal holds a Template Role on this Item.

TemplateRoleInstance

A Role from the Template’s roles.

Promise<boolean>

BaseReadItem.checkRole


clearKey(): Promise<BaseWriteItem<TItemInstanceTypes>>

Removes this Item’s key so that it can no longer be found by key.

Promise<BaseWriteItem<TItemInstanceTypes>>

The updated Item.


createChild<TReadTemplate>(template, options): Promise<TItemInstanceTypes["WriteItemsByPrn"][TReadTemplate["prn"]]>

Creates a new Item of a Template as a child of this Item.

TReadTemplate extends BaseReadTemplate

The Template, whose PRN selects the returned Item type.

TReadTemplate

The Template to create the Item from, normally taken from an App’s templates.

CreateChildOptions

Key, Property values and initial state for the new Item.

Promise<TItemInstanceTypes["WriteItemsByPrn"][TReadTemplate["prn"]]>

The created Item, writable in the current execution.

Property values are converted against the Template and the parent’s extension of it; a value that does not fit its Property definition, or whose key is not defined, is not stored and reads back as unset. When key is supplied, a sibling carrying that key loses it, so a key names at most one child of a parent. When the Template declares a created Event, it is published against the new Item with a {} payload.

when options.state is supplied and is not an initial state of the Template.

Create a child with Properties

const directory = await ctx.getItemByKeyPath('directory');
if (!directory) throw new Error('Missing directory');
const employee = await directory.createChild(ctx.apps.employeeProfile.templates.employee, {
properties: { displayName: 'Ada Lovelace' },
});

CreateChildOptions


currentRoles(): Promise<BaseRoleInstance[]>

Lists the Template Roles the active principal holds on this Item.

Promise<BaseRoleInstance[]>

Sudo and system contexts hold every Role the Template declares.

Grant access

BaseReadItem.currentRoles


getAncestors(): Promise<WriteItem<ItemInstanceTypes>[]>

Loads the ancestors of this Item as writable Items, nearest parent first, up to and including the Workspace Item.

Promise<WriteItem<ItemInstanceTypes>[]>

The ancestors the active principal can read, stopping at the first unreadable one; the Workspace Item has none.

BaseReadItem.getAncestors


getChildByKey(key): Promise<WriteItem<ItemInstanceTypes> | undefined>

Finds the child of this Item that carries a key and returns it writable.

string

The child’s key, relative to this Item.

Promise<WriteItem<ItemInstanceTypes> | undefined>

The child, or undefined when no child the active principal can read carries the key.

BaseReadItem.getChildByKey


getParent(): Promise<WriteItem<ItemInstanceTypes> | undefined>

Loads the parent of this Item as a writable Item.

Promise<WriteItem<ItemInstanceTypes> | undefined>

The parent, or undefined for the Workspace Item, which has no parent.

BaseReadItem.getParent


updateKey(key): Promise<BaseWriteItem<TItemInstanceTypes>>

Gives this Item a new key relative to its parent.

string

The new key; it must be a valid key.

Promise<BaseWriteItem<TItemInstanceTypes>>

The updated Item.

when key is not a valid key.


updateProperties(input): Promise<BaseWriteItem<TItemInstanceTypes>>

Stores new values for some of this Item’s Properties.

Record<string, unknown>

Property values keyed by Property key; Properties not named keep their stored values.

Promise<BaseWriteItem<TItemInstanceTypes>>

The updated Item; the instance the method was called on is unchanged.

Only Properties that the Template or its extension defines and that the active principal is granted are written. Values are converted against the Property definitions before being stored, so a value that does not fit its definition reads back as unset.

Patch one Property

const updated = await item.updateProperties({ installation: 'requested' });

Handle incomplete data


upsertChildByKey<TReadTemplate>(template, options): Promise<TItemInstanceTypes["WriteItemsByPrn"][TReadTemplate["prn"]]>

Creates the child of this Item that carries a key, or updates the existing child when there already is one.

TReadTemplate extends BaseReadTemplate

The Template, whose PRN selects the returned Item type.

TReadTemplate

The Template for a newly created child.

UpsertChildByKeyOptions

The key, Property values, initial state for a new child and the conflict policy.

Promise<TItemInstanceTypes["WriteItemsByPrn"][TReadTemplate["prn"]]>

The child carrying the key after the operation.

The existing child is found by key among this Item’s children. With onConflict: 'merge' the supplied Properties are written to it and its state is left unchanged; with 'ignore' it is returned untouched. A newly created child takes initialState, or the Template’s default state, and its created Event is published as for BaseWriteItem.createChild.

when key is missing or not a valid key.

Ensure a keyed child exists without overwriting it

const integration = await settings.upsertChildByKey(ctx.apps.email.templates.integration, {
key: 'email',
properties: { category: 'communication', title: 'Email' },
initialState: 'active',
onConflict: 'ignore',
});

UpsertChildByKeyOptions


upsertChildrenByKey<TReadTemplate>(template, options): Promise<Record<string, TItemInstanceTypes["WriteItemsByPrn"][TReadTemplate["prn"]]>>

Creates or updates several keyed children of this Item in one call.

TReadTemplate extends BaseReadTemplate

The Template, whose PRN selects the returned Item type.

TReadTemplate

The Template for newly created children.

UpsertChildrenByKeyOptions

The children to upsert, keyed by child key, and the conflict policy applied to all of them.

Promise<Record<string, TItemInstanceTypes["WriteItemsByPrn"][TReadTemplate["prn"]]>>

The resulting children keyed by child key.

Each child follows the rules of BaseWriteItem.upsertChildByKey. An empty children map returns an empty result.

UpsertChildrenByKeyOptions