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

# Class: MongodbPersistence

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:20](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L20)

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.

## Implements

* [`Persistence`](../../bus-core/interfaces/Persistence.md)

## Constructors

### Constructor

```ts
new MongodbPersistence(configuration, client?): MongodbPersistence;
```

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:23](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L23)

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `configuration` | [`MongodbConfiguration`](../interfaces/MongodbConfiguration.md) |
| `client` | `MongoClient` |

#### Returns

`MongodbPersistence`

## Methods

### dispose()

```ts
dispose(): Promise<void>;
```

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:41](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L41)

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

#### Returns

`Promise`<`void`>

#### Implementation of

[`Persistence`](../../bus-core/interfaces/Persistence.md).[`dispose`](../../bus-core/interfaces/Persistence.md#dispose)

***

### getWorkflowState()

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

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:63](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L63)

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

#### Type Parameters

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

#### Parameters

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

***

### initialize()

```ts
initialize(): Promise<void>;
```

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:34](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L34)

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

#### Returns

`Promise`<`void`>

#### Implementation of

[`Persistence`](../../bus-core/interfaces/Persistence.md).[`initialize`](../../bus-core/interfaces/Persistence.md#initialize)

***

### initializeWorkflow()

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

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:47](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L47)

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 |
| ------ |
| `WorkflowStateType` *extends* [`WorkflowState`](../../bus-core/classes/WorkflowState.md) |

#### Parameters

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

#### Returns

`Promise`<`void`>

#### Implementation of

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

***

### prepare()

```ts
prepare(coreDependencies): void;
```

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:28](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L28)

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`](../../bus-core/interfaces/CoreDependencies.md) | the dependencies of the bus that's preparing the persistence |

#### Returns

`void`

#### Implementation of

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

***

### saveWorkflowState()

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

Defined in: [packages/bus-mongodb/src/mongodb-persistence.ts:100](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-mongodb/src/mongodb-persistence.ts#L100)

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`](../../bus-core/classes/WorkflowState.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `workflowState` | `WorkflowStateType` | the workflow state as plain JSON values, ready to store as it is |

#### Returns

`Promise`<`void`>

#### Implementation of

[`Persistence`](../../bus-core/interfaces/Persistence.md).[`saveWorkflowState`](../../bus-core/interfaces/Persistence.md#saveworkflowstate)
