Skip to content

API reference / @node-ts/bus-core / Persistence

Interface: Persistence ​

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:14

Infrastructure that provides the ability to persist workflow state for long running processes.

Workflow state is passed to and returned from the persistence as plain JSON values (Dates as ISO strings, Maps as objects, Sets as arrays and bigints as strings). The bus converts it to and from its classes with its own serializer and message types, so one persistence instance can be shared by several buses. A shared persistence is prepared and initialized by each bus, and only disposed by the last bus that uses it.

Methods ​

dispose()? ​

ts
optional dispose(): Promise<void>;

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:33

If provided, will dispose any resources related to the persistence. This is where things like closing database connections should occur.

Returns ​

Promise<void>


getWorkflowState() ​

ts
getWorkflowState<WorkflowStateType, MessageType>(
   workflowStateConstructor, 
   messageMap, 
   message, 
   messageOptions, 
   includeCompleted?
): Promise<WorkflowStateType[]>;

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:54

Retrieves all workflow state models that match the given messageMap criteria

Type Parameters ​

Type Parameter
WorkflowStateType extends WorkflowState
MessageType extends Message

Parameters ​

ParameterTypeDescription
workflowStateConstructorClassConstructor<WorkflowStateType>The workflow model type to retrieve
messageMapMessageWorkflowMapping<MessageType, WorkflowStateType>How the message is mapped to workflow state models
messageMessageTypeThe message to map to workflow state
messageOptionsMessageAttributes-
includeCompleted?booleanIf completed workflow state items should also be returned. False by default

Returns ​

Promise<WorkflowStateType[]>

the matching workflow state as it was saved. The bus restores its classes, so it doesn't need to be converted.


initialize()? ​

ts
optional initialize(): Promise<void>;

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:27

If provided, initializes the persistence implementation. This is where database connections are started.

Returns ​

Promise<void>


initializeWorkflow() ​

ts
initializeWorkflow<TWorkflowState>(workflowStateConstructor, messageWorkflowMappings): Promise<void>;

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:40

Allows the persistence implementation to set up its internal structure to support the workflow state that it will be persisting. Typically for a database this could mean setting up the internal table schema to support persisting of each of the workflow state models.

Type Parameters ​

Type Parameter
TWorkflowState extends WorkflowState

Parameters ​

ParameterType
workflowStateConstructorClassConstructor<TWorkflowState>
messageWorkflowMappingsMessageWorkflowMapping<Message, WorkflowState>[]

Returns ​

Promise<void>


prepare() ​

ts
prepare(coreDependencies): void;

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:21

An optional function that is called before startup that will provide core dependencies to the persistence. This can be used to fetch loggers etc that are used in initialization steps. A persistence shared by several buses is prepared by each of them.

Parameters ​

ParameterTypeDescription
coreDependenciesCoreDependenciesthe dependencies of the bus that's preparing the persistence

Returns ​

void


saveWorkflowState() ​

ts
saveWorkflowState<WorkflowStateType>(workflowState): Promise<void>;

Defined in: packages/bus-core/src/workflow/persistence/persistence.ts:71

Saves a new workflow state model or updates an existing one. Persistence implementations should take care to observe the change in $version of the workflow state model when persisting to ensure race conditions don't occur.

Type Parameters ​

Type Parameter
WorkflowStateType extends WorkflowState

Parameters ​

ParameterTypeDescription
workflowStateWorkflowStateTypethe workflow state as plain JSON values, ready to store as it is

Returns ​

Promise<void>

Released under the MIT License.