domain

2019-05-14 admin

domain是什么

什么是domain,domain for javascript

domain使用教程帮助文档

CQRS

DDD-CQRS-Actor framework.

Document [ chinese ]

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 message
  • actor GC
  • system time travel

Step

create Actor class

const { Actor } = require("cqrs");
class User extends Actor { /* see example */ }
class Transfer extends Actor { /* see example */ }

register Actor class to domain

const { domain } = require("cqrs"); // get default domain.
domain.register(User).register(Transfer);

create/get an Actor instance

// only javascript object

const user = await domain.create("User", {name:"Leo"});
user.json; // get actor instance data.
user.deduct(120.00); // call instance method.

const userInstance = await domain.get("User",userId); // get a User instance.

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) {
        // console.log(event,"21121----2");
    }

    async transfe(fromUserId, toUserId, money) {
      try{
        const $ = this.$;
        $.sagaBegin();
        $.lock();

          await $.subscribe({ actorType: "User"}, "log");
          // await $.unsubscribe({ actorType: "User"});
          await $.subscribe({ actorType: "User", actorId:toUserId , type: "add" }, "log");
          // await $.unsubscribe({ actorType: "User", actorId:toUserId , type: "add" });

        // console.log(fromUserId,toUserId);
        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){
        // console.log(e);
      }
    }

    get updater(){
      return {
        finish(data,event) {
            return { finish: true }
        }
      }
    }

}

LICENSE

MIT

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-64940.html

文章标题:domain

相关文章
什么是document.domain = document.domain吗?
GStomjs提出了一个问题:What does document.domain = document.domain do?,或许与您遇到的问题类似。 回答者jasonszhaoMichael Carter给出了该问题的处理方式: I ac...
2018-03-21
How to get the domain value for a cookie in Javascript?
Wabbitseason提出了一个问题:How to get the domain value for a cookie in Javascript?,或许与您遇到的问题类似。 回答者bobince给出了该问题的处理方式: Sorry, a...
2018-04-22
IE中使用ajax碰到的问题,No Transport(crossDomain=true属性设置问题)
IE中的crossDomain=true属性设置 再ajax请求过程中设置了crossDomain=true属性,再谷歌内核中是可以正确解读为support.cors = true,发现其他浏览器中都是support.cors = true...
2018-10-30
domain-browser
domain-browser是什么 什么是domain-browser,Node's domain module for the web browser. This is merely an evented try…catch with t...
2018-10-12
HTML domain 属性
<a href=“dom-obj-document.html”><img class=“navup” src="/public/upload/imgs/up.gif" alt=“Document 对象参...
2019-03-02
helmet-crossdomain
helmet-crossdomain是什么 什么是helmet-crossdomain,Set the X-Permitted-Cross-Domain-Policies header in Express apps helmet-cro...
2019-02-10
Why move your Javascript files to a different main domain that you also own?
CommunityTim Farley提出了一个问题:Why move your Javascript files to a different main domain that you also own?,或许与您遇到的问题类似。 回答者...
2018-04-26
connected-domain
connected-domain是什么 什么是connected-domain,calculate all the connected domains based on the given two-dimensional array co...
2018-10-28
How do I get the domain originating the request in express.js?
Nicola Peluchetti提出了一个问题:How do I get the domain originating the request in express.js?,或许与您遇到的问题类似。 回答者CommunityJonatha...
2018-04-10
Can't Load URL: The domain of this URL isn't included in the app's domains
gsiradze提出了一个问题:Can’t Load URL: The domain of this URL isn’t included in the app’s domains,或许与您遇到的问题类似。 回答者Neo给出了该问题的处理方...
2018-04-18
回到顶部