Creating a persistence
You can use your own persistence technology by implementing the Persistence interface and providing it as a configuration to bus.
The Persistence interface has the following properties that need to be implemented
1
/**
2
* Infrastructure that provides the ability to persist workflow state for long running processes
3
*/
4
export interface Persistence {
5
/**
6
* An optional function that is called before startup that will provide core dependencies
7
* to the persistence. This can be used to fetch loggers etc that are used
8
* in initialization steps
9
* @param coreDependencies
10
*/
11
prepare (coreDependencies: CoreDependencies): void
12
​
13
/**
14
* If provided, initializes the persistence implementation. This is where database connections are
15
* started.
16
*/
17
initialize? (): Promise<void>
18
​
19
/**
20
* If provided, will dispose any resources related to the persistence. This is where things like
21
* closing database connections should occur.
22
*/
23
dispose? (): Promise<void>
24
​
25
/**
26
* Allows the persistence implementation to set up its internal structure to support the workflow state
27
* that it will be persisting. Typically for a database this could mean setting up the internal table
28
* schema to support persisting of each of the workflow state models.
29
*/
30
initializeWorkflow<TWorkflowState extends WorkflowState> (
31
workflowStateConstructor: ClassConstructor<TWorkflowState>,
32
messageWorkflowMappings: MessageWorkflowMapping<Message, WorkflowState>[]
33
): Promise<void>
34
​
35
/**
36
* Retrieves all workflow state models that match the given `messageMap` criteria
37
* @param workflowStateConstructor The workflow model type to retrieve
38
* @param messageMap How the message is mapped to workflow state models
39
* @param message The message to map to workflow state
40
* @param includeCompleted If completed workflow state items should also be returned. False by default
41
*/
42
getWorkflowState<WorkflowStateType extends WorkflowState, MessageType extends Message> (
43
workflowStateConstructor: ClassConstructor<WorkflowStateType>,
44
messageMap: MessageWorkflowMapping<MessageType, WorkflowStateType>,
45
message: MessageType,
46
messageOptions: MessageAttributes,
47
includeCompleted?: boolean
48
): Promise<WorkflowStateType[]>
49
​
50
/**
51
* Saves a new workflow state model or updates an existing one. Persistence implementations should take care
52
* to observe the change in `$version` of the workflow state model when persisting to ensure race conditions
53
* don't occur.
54
*/
55
saveWorkflowState<WorkflowStateType extends WorkflowState> (
56
workflowState: WorkflowStateType
57
): Promise<void>
58
}
59
​
Copied!
Once this is implemented, provide it to the bus configuration
1
import { Bus } from '@node-ts/bus-core'
2
import { MyPersistence } from './my-persistence'
3
​
4
const myPersistence = new MyPersistence()
5
await Bus
6
.configure()
7
.withPersistsence(myPersistence)
8
.initialize()
Copied!
Copy link