MongoDB
@node-ts/bus-mongodb stores workflow state in MongoDB, using version 7 of the mongodb driver (MongoDB server 4.2 or later). This page covers installing and configuring it.
Installation
sh
npm i @node-ts/bus-mongodbsh
pnpm add @node-ts/bus-mongodbsh
yarn add @node-ts/bus-mongodbCreate a MongodbPersistence and pass it to the bus configuration:
ts
import { Bus } from '@node-ts/bus-core'
import { MongodbConfiguration, MongodbPersistence } from '@node-ts/bus-mongodb'
import { messageTypes } from './message-types.generated'
import { fulfilmentWorkflow } from './workflows/fulfilment-workflow'
const configuration: MongodbConfiguration = {
connection: 'mongodb://localhost:27017',
databaseName: 'workflows'
}
const mongodbPersistence = new MongodbPersistence(configuration)
const bus = Bus.configure()
.withMessageTypes(messageTypes)
.withPersistence(mongodbPersistence)
.withWorkflow(fulfilmentWorkflow)
.build()
// Creates a collection, and indexes for its lookups, for each workflow state
await bus.initialize()
await bus.start()Configuration
| Option | Description |
|---|---|
connection | The connection string: a single server, a replica set, or a mongodb+srv connection. |
databaseName | The database to create the workflow collections in. |
To share a client with the rest of your application, pass your MongoClient as the second constructor argument.
Collections
Each workflow state has a collection, named after its $name with invalid characters removed. initialize() creates the collections, and an index for each field that messages are looked up by. It never drops indexes, so ones you add are safe.
Keys in the state are percent-encoded when stored ($ as %24, . as %2E and % as %25), since MongoDB doesn't allow them in field names, and decoded when read.
Running MongoDB locally
sh
docker run -d -p 27017:27017 mongoSee also
- Workflows
MongodbConfigurationin the API reference