GithubHelp home page GithubHelp logo

lirenhao / cqrs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from honestyallan/domain

0.0 0.0 0.0 1.51 MB

CQRS-DDD-Actor framework for Node.js

Home Page: http://liangzeng.github.io/cqrs

License: MIT License

JavaScript 100.00%

cqrs's Introduction

DDD-CQRS-Actor framework for javascript.

Schematic diagram

Understand ActorListener

Install

npm install cqrs --save

Test

npm test

Use

Step 1 . create a domain

var Domain = require("cqrs").Domain;
var Actor = require("cqrs").Actor;
var domain = new Domain(options);  // create a cqrs domain

Step 2 . create a actor class

// use es5 extend tool
var extend = require("cqrs").extend;    
var User = extend({

   type:'User',
   
   init(data){
       data.name = '';
       data.accessNum = 0;
   },
   
   when:function(event){
       switch(event.name){
           case "changeName":
               this._data.name = event.name;
           break;
       }
   },

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


--- or use es6 class ---

class User extends Actor{

    static get type(){
       return 'User';
    }
    
    constructor(data){
        super({
            name;'',
            accessNum:0
        });
    }

    when(event){
        switch(event.name){
            case "changeName":
                this._data.name = event.name;
            break;
        }
    }

    changeName(name){
        this.apply("changeName",name);
    }
    
    access(){
        this.apply("access");
    }

}

Step 3 . register actor class to domain

domain.register(User);

Step 4 . listen domain's event

domain.on("User.changeName",function(event){
    // ......
})

Step 5 . actor operating

// create a user
domain.create("User",{name:"leo"},function(err,userId){
    ......
});


// get a user object
domain.get("User",userId,function(err,actor){

});

// or

domain.get("User",userId).then(function(actor){
    actor.changeName("leo giese");
});

// or

domain.call('User',userId,'changeName',['leo giese']);  // return a promise object
domain.call(`User.${userId}.changeName`,['leo giese']); 
domain.call(['User',userId,'changeName'],['leo giese']);
domain.call(`User.${userId}.changeName`,['leo giese'],function(err,result){ ... });

// if no need arguments
domain.call('User',userId,'access'); 
domain.call('User.${userId}.access'); 
domain.call(['User',userId,'access']); 

Advanced Usage

Actor listen domain'event, and handle.

export default
class Transfer extends Actor {

    static get type() {
        return "Transfer";
    }

    transfer(fromId, toId, money) {
        ......

        this.listen("User." + fromId + ":deduct&" + this.id, "__userDeduct");
        this.listen("User." + toId + ":recharge&" + this.id, "__userRecharge");

        ......
    }

    // event handle method
    __userDeduct(event) {
        ......
    }

    __userRecharge(event) {
        ......
    }

}

LICENSE

MIT

cqrs's People

Contributors

brighthas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.