Skip to content

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

Class: InMemoryPersistence ​

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

A non-durable in-memory persistence for storage and retrieval of workflow state. Before using this, be warned that all workflow state will not survive a process restart or application shut down. As such this should only be used for testing, prototyping or handling unimportant workflows.

Implements ​

Constructors ​

Constructor ​

ts
new InMemoryPersistence(): InMemoryPersistence;

Returns ​

InMemoryPersistence

Methods ​

getWorkflowState() ​

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

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

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

Implementation of ​

Persistence.getWorkflowState


initializeWorkflow() ​

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

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

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>
_MessageWorkflowMapping<Message, WorkflowState>[]

Returns ​

Promise<void>

Implementation of ​

Persistence.initializeWorkflow


length() ​

ts
length(workflowStateConstructor): number;

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

Gets the number of workflow states held in memory for a workflow state type

Parameters ​

ParameterTypeDescription
workflowStateConstructorClassConstructor<WorkflowState>the type of workflow state to count

Returns ​

number

the number of workflow states held, including completed ones

Throws ​

WorkflowStateNotInitialized if the workflow state hasn't been initialized


prepare() ​

ts
prepare(coreDependencies): void;

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

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

Implementation of ​

Persistence.prepare


saveWorkflowState() ​

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

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

Saves a copy of the workflow state with its $version incremented. The $version of the state being saved must match the version held in memory, in the same way as durable persistence adapters enforce optimistic concurrency.

Type Parameters ​

Type Parameter
WorkflowStateType extends WorkflowState

Parameters ​

ParameterTypeDescription
workflowStateWorkflowStateTypethe workflow state to save

Returns ​

Promise<void>

Throws ​

WorkflowStateNotInitialized if the workflow state hasn't been initialized

Throws ​

WorkflowStateVersionConflict if the workflow state was saved elsewhere since it was read

Implementation of ​

Persistence.saveWorkflowState

Released under the MIT License.