Skip to content

WriteItem

Presents a writable Item below the Workspace Item, adding archiving and refresh scheduling to BaseWriteItem.

TItemInstanceTypes extends ItemInstanceTypes = ItemInstanceTypes

The generated map from Template PRN to Item type, used to type created children.

readonly createdAt: Date

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

BaseWriteItem.createdAt


readonly definition: TemplateDefinition

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

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

BaseWriteItem.events


readonly id: string

The Item identifier, a UUID.

BaseWriteItem.id


readonly isArchived: boolean

Whether the Item is archived; archived Items are left out of selections unless archived asks for them.

StandardItem.isArchived


readonly isRefreshOverdue: boolean

Whether a scheduled refresh time has passed without the Item being refreshed since.

StandardItem.isRefreshOverdue


readonly isRefreshPending: boolean

Whether a refresh is scheduled for a time still in the future.

StandardItem.isRefreshPending


readonly key: string | undefined

The Item’s key relative to its parent, or undefined when it has none.

StandardItem.key


readonly parentId: string | undefined

The identifier of the Item’s parent.

StandardItem.parentId


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

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

BaseWriteItem.propertyValues


readonly refreshBy: Date | undefined

When the next refresh is due, or undefined when none is scheduled.

StandardItem.refreshBy


readonly refreshedAt: Date | undefined

When the Item was last refreshed, or undefined when it never has been.

StandardItem.refreshedAt


readonly relations: Record<string, WriteRelationInstance>

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

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

BaseWriteItem.state


readonly templatePrn: string

The PRN of the Template the Item was created from.

BaseWriteItem.templatePrn


readonly workspaceId: string

The identifier of the Workspace holding the Item.

BaseWriteItem.workspaceId

archive(): Promise<WriteItem<TItemInstanceTypes>>

Archives this Item so that selections leave it out by default.

Promise<WriteItem<TItemInstanceTypes>>

The archived Item.

Archiving an archived Item leaves it archived. Archived Items stay readable by identifier and through selections that include archived Items.


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.

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

BaseWriteItem.assignParent


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>

BaseWriteItem.checkRole


clearKey(): Promise<WriteItem<TItemInstanceTypes>>

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

Promise<WriteItem<TItemInstanceTypes>>

The updated Item.

BaseWriteItem.clearKey


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

BaseWriteItem.createChild


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

BaseWriteItem.currentRoles


extensions<TReadTemplate>(template): Promise<WriteTemplateExtension>

Opens the Property extensions for children of this Item created from an extensible Template, for modification.

TReadTemplate extends BaseReadTemplate & object

TReadTemplate

The extensible Template.

Promise<WriteTemplateExtension>

when the Template is not extensible or this is the Workspace Item.

WriteTemplateExtension


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.

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

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

BaseWriteItem.getParent


selectChildren(): ItemCursorSelector<WriteItem<ItemInstanceTypes>>

Starts a selection over the children of this Item, returning writable Items.

ItemCursorSelector<WriteItem<ItemInstanceTypes>>


setRefreshBy(duration): Promise<WriteItem<TItemInstanceTypes>>

Schedules the next refresh of this Item.

DurationLikeObject | { immediate: true; }

The refresh period as a Luxon duration object, or { immediate: true } to make the Item due now.

Promise<WriteItem<TItemInstanceTypes>>

The updated Item.

With a period, the due time is the next occurrence of a phase derived from the Item’s identifier within that period, which spreads Items sharing a period evenly across it; the due time is always at least a short guard interval after the execution time. With immediate, refreshBy is set to the execution time.

Refresh hourly

await step.setRefreshBy({ hour: 1 });

unarchive(): Promise<WriteItem<TItemInstanceTypes>>

Restores an archived Item so that selections include it again.

Promise<WriteItem<TItemInstanceTypes>>

The restored Item.

Restoring an Item that is not archived leaves it active.


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

Gives this Item a new key relative to its parent.

string

The new key; it must be a valid key.

Promise<WriteItem<TItemInstanceTypes>>

The updated Item.

when key is not a valid key.

BaseWriteItem.updateKey


updateProperties(input): Promise<WriteItem<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<WriteItem<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

BaseWriteItem.updateProperties


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

BaseWriteItem.upsertChildByKey


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

BaseWriteItem.upsertChildrenByKey