memory-adapter
A library that implements the Write Model and Read Model Adapter interface for storing events and projections in memory. Useful for development and testing but not for production, obviously πŸ˜›.

Methods

build

build({ entityName }, { eventStore, projectionStore )
builds a read-model and write-model adapter

Parameters

attribute
type
description
entityName
string
the name of the entity
eventStore
object
(optional) an object where keys are entityNames and values are arrays of commit objects
projectionStore
object
(optional) an object where keys are entity names and values are projections objects keyed by id

Returns

an object with write model methods and read model methods​

Example

adapter.js
1
const memoryAdapterBuilder = require('serverless-cqrs.memory-adapter')
2
module.exports = memoryAdapterBuilder.build({
3
entityName: 'todo'
4
})
Copied!
You can also provide the initial eventStore and/or projectionStore objects (both are optional). This is useful when writing tests.
test.js
1
const { test } = require('tap')
2
const { build } = require('serverless-cqrs.memory-adapter')
3
​
4
test('loadEvents', async assert => {
5
const eventStore = {
6
'foo': [
7
{ entityId: '123', version: 0, events: [ 'a' ] },
8
{ entityId: '123', version: 1, events: [ 'b', 'c' ]},
9
{ entityId: '123', version: 3, events: [ 'd', 'e', 'f' ] },
10
{ entityId: '123', version: 6, events: [ 'g', 'h', 'i' ] },
11
]
12
}
13
const expected = [ 'd', 'e', 'f', 'g', 'h', 'i' ]
14
​
15
const adapter = build({ entityName: 'foo' }, { eventStore })
16
17
const res = await adapter.loadEvents('123', 1)
18
​
19
assert.deepEquals(res, expected, 'returns events after given version')
20
})
21
​
22
test('get', async assert => {
23
const projectionStore = {
24
foo: {
25
'123':{
26
id: '123',
27
version: 0,
28
state: 'foobar',
29
}
30
}
31
}
32
​
33
const expected = {
34
id: '123',
35
version: 0,
36
state: 'foobar',
37
}
38
​
39
const adapter = build({ entityName: 'foo' }, { projectionStore })
40
41
const res = await adapter.get('123')
42
​
43
assert.deepEquals(res, expected, 'returns exisiting projection from store')
44
})
Copied!
Last modified 2yr ago
Copy link