---
url: https://node-ts.github.io/bus/api/bus-core/classes/InMemoryPersistence.md
---
[API reference](../../index.md) / [@node-ts/bus-core](../index.md) / InMemoryPersistence

# Class: InMemoryPersistence

Defined in: [packages/bus-core/src/workflow/persistence/in-memory-persistence.ts:21](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/persistence/in-memory-persistence.ts#L21)

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

* [`Persistence`](../interfaces/Persistence.md)

## 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](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/persistence/in-memory-persistence.ts#L39)

Retrieves all workflow state models that match the given `messageMap` criteria

#### Type Parameters

| Type Parameter |
| ------ |
| `WorkflowStateType` *extends* [`WorkflowState`](WorkflowState.md) |
| `MessageType` *extends* [`Message`](../../bus-messages/classes/Message.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `workflowStateConstructor` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<`WorkflowStateType`> | The workflow model type to retrieve |
| `messageMap` | [`MessageWorkflowMapping`](../interfaces/MessageWorkflowMapping.md)<`MessageType`, `WorkflowStateType`> | How the message is mapped to workflow state models |
| `message` | `MessageType` | The message to map to workflow state |
| `attributes` | [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md) | - |
| `includeCompleted?` | `boolean` | If 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`](../interfaces/Persistence.md).[`getWorkflowState`](../interfaces/Persistence.md#getworkflowstate)

***

### initializeWorkflow()

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

Defined in: [packages/bus-core/src/workflow/persistence/in-memory-persistence.ts:31](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/persistence/in-memory-persistence.ts#L31)

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`](WorkflowState.md) |

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `workflowStateConstructor` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<`TWorkflowState`> |
| `_` | [`MessageWorkflowMapping`](../interfaces/MessageWorkflowMapping.md)<[`Message`](../../bus-messages/classes/Message.md), [`WorkflowState`](WorkflowState.md)>\[] |

#### Returns

`Promise`<`void`>

#### Implementation of

[`Persistence`](../interfaces/Persistence.md).[`initializeWorkflow`](../interfaces/Persistence.md#initializeworkflow)

***

### length()

```ts
length(workflowStateConstructor): number;
```

Defined in: [packages/bus-core/src/workflow/persistence/in-memory-persistence.ts:122](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/persistence/in-memory-persistence.ts#L122)

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

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `workflowStateConstructor` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<[`WorkflowState`](WorkflowState.md)> | 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](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/persistence/in-memory-persistence.ts#L25)

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

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `coreDependencies` | [`CoreDependencies`](../interfaces/CoreDependencies.md) | the dependencies of the bus that's preparing the persistence |

#### Returns

`void`

#### Implementation of

[`Persistence`](../interfaces/Persistence.md).[`prepare`](../interfaces/Persistence.md#prepare)

***

### saveWorkflowState()

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

Defined in: [packages/bus-core/src/workflow/persistence/in-memory-persistence.ts:78](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/persistence/in-memory-persistence.ts#L78)

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`](WorkflowState.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `workflowState` | `WorkflowStateType` | the 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`](../interfaces/Persistence.md).[`saveWorkflowState`](../interfaces/Persistence.md#saveworkflowstate)
