CQRS
DDD-CQRS-Actor framework.
Version
cqrs@2.10.11
Install
npm install cqrs
yarn add cqrs
Consumers
- Auxo (vue & express & cqrs framework)
EventStore
const {Domain} = require("cqrs");
const MongoStore = require("cqrs-mongo-eventstore").default;
const eventstore = new MongoStore("localhost/test");
const domain = new Domain({eventstore});
Roadmap
- preview core
- use typescript rewrite core
- saga rollback
- join the distributed system
- DCI support
use protobuf messageactor GCsystem time travel
Step
create Actor class
const { Actor } = require("cqrs");
class User extends Actor { }
class Transfer extends Actor { }
register Actor class to domain
const { domain } = require("cqrs");
domain.register(User).register(Transfer);
create/get an Actor instance
const user = await domain.create("User", {name:"Leo"});
user.json;
user.deduct(120.00);
const userInstance = await domain.get("User",userId);
Preview Example
Example
User.js
const { Actor } = require("..");
module.exports = class User extends Actor {
constructor(data) {
super({ money: data.money || 0, name: data.name, id:data.id });
}
changename(name) {
this.$.apply("changename", name);
}
get updater(){
return {
changename(data,event){
return { name: event.name }
}
}
}
}
Transfer.js
const { Actor } = require("cqrs");
module.exports = class Transfer extends Actor {
constructor(data) {
super({ finish: false });
}
log(event) {
}
async transfe(fromUserId, toUserId, money) {
try{
const $ = this.$;
$.sagaBegin();
$.lock();
await $.subscribe({ actorType: "User"}, "log");
await $.subscribe({ actorType: "User", actorId:toUserId , type: "add" }, "log");
const fromUser = await $.get("User.payers", fromUserId);
const toUser = await $.get("User.charger", toUserId);
fromUser.deduct(money);
toUser.add(money);
if (money > 100)
throw new Error("hhhh")
$.unlock();
$.sagaEnd();
$("finish", null);
}catch(e){
}
}
get updater(){
return {
finish(data,event) {
return { finish: true }
}
}
}
}
LICENSE
MIT