Skip to content

PlatformInstance

Runs application code against one Workspace as one principal; the entry point to every Runtime.

Obtain an instance from the Runtime for a Workspace and an auth context, then call execute to run Queries in read mode and Mutations, Listeners and hooks in read-write mode.

readonly auth: AlpyneAuthContext

The auth context every execution on this instance runs under; a copy, so changes to the original do not affect it.


readonly workspaceId: string

The identifier of the Workspace this instance operates in.

execute<TResult>(mode, fn): Promise<TResult>

Runs a callback in read mode, where Items cannot be modified and Events cannot be published.

TResult

The value the callback resolves to.

"read"

'read'.

RuntimeReadFn<TResult>

Receives the root App and the read context.

Promise<TResult>

Whatever fn resolves to.

Use read mode for Queries. Items returned reflect committed data at the time they are read.

Run a Query

const total = await instance.execute('read', async (_root, ctx) => {
const page = await ctx.apps.library.templates.content.selectItems().execute();
return page.results.length;
});

execute<TWriteAppInstance, TResult>(mode, event, fn, options?): Promise<TResult>

Runs a callback in read-write mode inside one transaction, with full access to Items and data under the instance’s permissions.

TWriteAppInstance extends WriteRuntimeInstance = WriteRuntimeInstance

The write context type, narrowed to an App’s generated context where one exists.

TResult = unknown

The value the callback resolves to.

"readwrite"

'readwrite'.

RuntimeExternalEvent | RuntimeInternalEvent

The Event that caused this execution, such as an API call or a processed Event; it supplies the identity used for deduplication and lineage.

RuntimeReadWriteFn<TResult, TWriteAppInstance>

Receives the write context.

ReadWriteExecuteOptions

Deduplication and retry settings.

Promise<TResult>

Whatever fn resolves to, once the transaction has committed.

Use read-write mode for Mutations, Listeners and App Hooks. Items marked external by their Template stay read-only. Every write in the callback commits or rolls back together; an attempt that fails with a transient error is retried within maximumTransientRetries. Awaiting the result awaits the commit, not the asynchronous Listeners and Metric projections that follow it.

(error-020-idempotent-execution-rejected) when idempotencyScope is set and the Event has already been executed in that scope.

ReadWriteExecuteOptions

execute<TResult>(mode, eventOrFn, fn?, options?): Promise<TResult>

Runs a callback in the given mode; the 'read' and 'readwrite' overloads describe each mode.

TResult

The value the callback resolves to.

RuntimeMode

'read' or 'readwrite'.

RuntimeExternalEvent | RuntimeInternalEvent | RuntimeReadFn<TResult>

The read callback, or the causing Event of a read-write execution.

RuntimeReadWriteFn<TResult, WriteRuntimeInstance>

The write callback of a read-write execution.

ReadWriteExecuteOptions

Deduplication and retry settings of a read-write execution.

Promise<TResult>


publishExternalEvent(event): Promise<void>

Enqueues one or more external Events for the Workspace; the reactor processes them asynchronously.

Omit<AlpyneExternalEvent, "workspaceId"> | Omit<AlpyneExternalEvent, "workspaceId">[]

The Event or Events to enqueue, without a Workspace identifier; this instance’s Workspace is applied.

Promise<void>

Awaiting the result awaits the enqueue, not the processing. An empty array enqueues nothing.

Handle events


runCreate(event): Promise<void>

Runs the onCreate hooks of every App in the Definition inside one transaction, to initialize a new Workspace.

RuntimeExternalEvent

The external Event that caused the creation.

Promise<void>


runSeed(event): Promise<void>

Runs the onSeed hooks of every App in the Definition inside one transaction, to populate a Workspace with seed Items.

RuntimeExternalEvent

The external Event that caused the seeding; its payload may restrict which Templates are seeded.

Promise<void>


setRefreshBy(itemId, duration): Promise<WriteItem<ItemInstanceTypes> | undefined>

Schedules the next refresh of an Item, in its own transaction.

string

The identifier of the Item to schedule.

DurationLikeObject | { immediate: true; }

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

Promise<WriteItem<ItemInstanceTypes> | undefined>

The updated Item, or undefined when the Item cannot be read.

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. With immediate, refreshBy is set to now.

WriteItem.setRefreshBy