CQRS-EventStore
You need Node.js 4+ to use it!
Installation
$ npm install cqrs-eventstore
Working example
A full example is provided in the demo folder. To run it:
- cd to the demo folder
- npm install
- node event-store-demo.js
Domain and DTOs
In order to use CQRS-EventStore, you need to implement your own aggregate and DTOs. Your aggregate must extend Aggregate.
An aggregate example including the DTOs:
"use strict"; { thisid thisversion } moduleexports = BaseEvent
"use strict" const BaseEvent = { super thisaddress = address } moduleexports = AddressUpdated
"use strict" const BaseEvent = { super thismobile = mobile } moduleexports = MobileUpdated
"use strict" const BaseEvent = { super thisname = name thissurname = surname thisaddress = address thismobile = mobile } moduleexports = UserInfoCreated
"use strict" const NodeEventStore = const UserInfoCreated = const AddressUpdated = const MobileUpdated = const clone = //Clone is used for the snapshot, it's totally up to you how to implement it. { //We are not exposing the UserInfo to the outside world, but we access to it through query. { thisname thissurname thisaddress thismobile } let _userInfo Aggregate { superid _userInfo = } { return } { _userInfo = payload } //Queries { return _userInfomobile } { return _userInfoaddress } //Mutators { super } { super } { super } //Apply { _userInfoname = payloadname _userInfosurname = payloadsurname _userInfoaddress = payloadaddress _userInfomobile = payloadmobile } { _userInfoaddress = payloadaddress } { _userInfomobile = payloadmobile } return id} moduleexports = UserInfo
Implementing the persistence layer
In order to implement your own persistence layer, you need to extend PersistenceAdapter and register it into the configurator (I'll show it later). The methods save, readSnapshot and readEvents must be implemented. All methods must return a promise. In the save method you need to persist your events and snapshot.
Below an example how to implement a sqlite persistor.
"use strict" const nodeEventStore = const fs = ;const sqlite3 = ;const _ = const util = const uuid = PersistenceAdapter { super const file = "eventStore.db"; const exists = fs; thisdb = file; thisdb; } { const self = this; return { selfdb } } //return a promise { return { thisdb; } } //return a promise { return { thisdball"SELECT * FROM Events WHERE streamId = ? AND version > ? ORDER BY version" id fromVersion { if err return }; } } moduleexports =
Implementing hooks
CQRS-EventStore comes with a build-in hook functionality. We can execute a task after each commands.
A simple hook that print into the console on each mobile number update:
"use strict" const util = module { console}
Hooks need to be registered into the configurator
Configuration
Before to use CQRS-EventStore, we need to configure it.
The parameters are:
- cacheExpiration: cache expiration in seconds, the default is 0 (unlimited).
- cacheDeleteCheckInterval: The period in seconds, used for the automatic delete check interval. Default is 60 seconds.
- repository: your extended persistance layer, if not provived an in-memory persistence will be used.
- snapshotEvery: event threshold for the snapshot, the default is 0 (snapshot disabled). For example, if we assign 50, every 50 events we create the snapshot.
- payloadSerializationFormat: payload serialization/compression, default is NodeEventStore.serializationFormats.stringify
The "cqrs-eventstore" module exposes an 'enumeration' calledserializationFormats
. Available values are:stringify
,zip
,unserialized
.
Usage Example
"use strict" const NodeEventStore = const UserInfoAggregate = const mobileUpdatedHook = //We need to register the hooks here, the name of the hook must match the apply methodNodeEventStore //Configurationconst EventStore = NodeEventStore const repository = UserInfoAggregatelet userInfoAggregate = 1userInfoAggregate repository
Contributing
-
clone
this repo -
npm run setup
Run the demo
npm start
Run the tests
npm test
Run the tests and listen for a debugger
npm run test-debug