cqrs

cqrs for javascript

Install

npm install cqrs --save

Use

var domain = require("cqrs")();

or

var domain = require("cqrs")(options);

register Actor class , Actor like DDD's AggregateRoot.

e.g.

domain.register({
    type:"User",
    changeName:function(name){},
    when:function(event){}
})

or

var Actor = require("cqrs").Actor;
var User = Actor.extend({
     type:"User",
     changeName:function(name){},
     when:function(event){}
})
domain.register(User);

create actor object.

domain.create("User",data,function(err,actorId){})

listen a domain event.

domain.addListener("User:changeName",function(domainEvent){});
domain.get("User","id0001" , function(err , actor){ })

Actor API

var User = Actor.extend(options);
Actor.extend({
    type:"User",
    changeName:function(name){
        // you can validat and throw error ...
        di.apply("changeName",name);
    },
    when:function(event){
        //see next step
    }
})

or

Actor.extend({
    type:"User",
    changeName:true,
    when:function(event){
        //see next step
    }
})

changeName:true Equivalent to

changeName:function(data){
    this.apply("changeName",data.name);
}

change:["name","age"] Equivalent to

change:function(data){
    this.apply("change" , {name:data.name,age:data.age})
}

the method cann't change self data. and must use when method to changed.

The method only set self data according event , no only logic code.

  • event , domain event.

e.g.

Actor.extend("User",{
    changeName:function(name){
        this.apply("changeName",name);
    },
    when:function(event){
        if(event.name === "changName"){
            this._data.name = event.data;
        }
    }
})

e.g.

Actor.extend("User",{
    test:function(){
        // listen `Book.changeName` domain' event , and use `handle` method to handle.
        this.listen("Book.changeName","handle");
        this.apply("test");
    },

    handle:function(event){

    }

})

static method

return actor object.

static method

return actor's json data.

LICENSE

MIT