UNPKG

@azure-utils/durable-functions

Version:
92 lines (91 loc) 4.21 kB
/** * @module * Exports a class to create, register and manage a Durable Entity. * * > Note: This module is to be used inside Azure Functions app only. */ import type { InvocationContext } from "@azure/functions"; import * as df from "durable-functions"; /** * DurableEntity is a wrapper around Durable Functions entities. * * It provides a simple way to define an entity with a default state and operations. * It also provides methods to get, reset, and update the state of the entity. * The entity is registered with the Durable framework when the class is instantiated. * The entity is identified by a name and a key. * * Note: keep Entity state small, limit to 16kB for reliable read operations. * * @example * ```ts * type MyEntityState = { foo: string; bar: number }; * const defaultState: MyEntityState = { foo: "bar", bar: 0 }; * const myEntity = new DurableEntity("my-entity", defaultState, { * updateFoo: (input: { foo: string }) => ({ foo: input.foo }), * updateBar: (input: { bar: number }) => ({ bar: input.bar }), * }) * ``` */ export declare class DurableEntity<State extends Record<string, unknown>, Operations extends NoInfer<Readonly<Record<string, (input: never) => DeepPartial<State>>>>> { #private; name: string; constructor(entityName: string, defaultState: State, operations: Operations); /** * Converts a string key to an EntityId. * If the key is already an EntityId, it returns it as is. */ entityId(key: string | df.EntityId): df.EntityId; /** * Gets the durable task which yields the current state of the entity. * * Note: Can be only used in orchestrator functions. * The yielded value is not type-safe but can be safely casted to the Entity's state type. */ getState(context: df.OrchestrationContext, keyOrEntityId: string | df.EntityId): df.Task; /** * Gets the current state of the entity. * If the entity does not exists, it returns the default state. * * Note: Can be only used in client functions (HTTP triggers, etc.) */ getState(context: InvocationContext, keyOrEntityId: string | df.EntityId): Promise<State>; /** * Resets the state of the entity to the default state. */ resetState(context: InvocationContext | df.OrchestrationContext, keyOrEntityId: string | df.EntityId): Promise<State>; /** * Updates the state of the entity with the new state. * The new state is deeply-merged with the current state. */ updateState(context: InvocationContext | df.OrchestrationContext, keyOrEntityId: string | df.EntityId, newState: DeepPartial<State>): Promise<void>; /** * Returns the key of the entity. * If the entityId is a string, it extracts the key from it. * If the entityId is an EntityId, it returns the key property. */ getKey(entityId: string | df.EntityId): string; /** * Returns a map of operations that can be used to update the state of the entity. * The operations are defined in the constructor and are deeply-merged with the current state. */ operations(context: InvocationContext | df.OrchestrationContext, keyOrEntityId: string | df.EntityId): TransformedOperations<Operations>; /** * Lists all instances of the entity. */ listInstances(context: InvocationContext, filter?: df.OrchestrationFilter): Promise<Array<DurableEntityStatus<State>>>; static listInstances<T = unknown>(context: InvocationContext, entityName?: string, filter?: df.OrchestrationFilter): Promise<DurableEntityStatus<T>[]>; } /** * DurableEntityStatus is the status of a Durable Entity. */ export type DurableEntityStatus<State> = Pick<df.DurableOrchestrationStatus, "name" | "instanceId" | "createdTime" | "lastUpdatedTime"> & { state: State | null; key: string; }; type TransformedOperations<T extends Readonly<Record<string, (input: never) => unknown>>> = { [Prop in keyof T]: T[Prop] extends () => unknown ? () => Promise<void> : T[Prop] extends (input: infer I) => unknown ? (input: I) => Promise<void> : never; }; type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : Partial<T>; export {};