@pocket-architect/core 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/AggregateRoot.d.ts +2 -1
- package/build/AggregateRoot.js.map +1 -1
- package/build/Entity.d.ts +8 -6
- package/build/Entity.js +14 -5
- package/build/Entity.js.map +1 -1
- package/build/Entity.spec.js +12 -6
- package/build/Entity.spec.js.map +1 -1
- package/build/EntityId.d.ts +8 -0
- package/build/EntityId.js +30 -0
- package/build/EntityId.js.map +1 -0
- package/build/index.d.ts +2 -1
- package/build/index.js +3 -1
- package/build/index.js.map +1 -1
- package/build/mixins/Eventable.d.ts +15 -0
- package/build/mixins/Eventable.js +35 -0
- package/build/mixins/Eventable.js.map +1 -0
- package/build/mixins/Eventable.spec.d.ts +1 -0
- package/build/mixins/Eventable.spec.js +27 -0
- package/build/mixins/Eventable.spec.js.map +1 -0
- package/package.json +1 -1
- package/src/AggregateRoot.ts +2 -1
- package/src/Entity.spec.ts +14 -7
- package/src/Entity.ts +22 -13
- package/src/EntityId.ts +26 -0
- package/src/index.ts +2 -1
- package/src/mixins/Eventable.spec.ts +25 -0
- package/src/mixins/Eventable.ts +27 -0
package/build/AggregateRoot.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AggregateRoot.js","sourceRoot":"","sources":["../src/AggregateRoot.ts"],"names":[],"mappings":";;;;AAAA,mCAAkC;
|
|
1
|
+
{"version":3,"file":"AggregateRoot.js","sourceRoot":"","sources":["../src/AggregateRoot.ts"],"names":[],"mappings":";;;;AAAA,mCAAkC;AAGlC;IAAmE,yCAAY;IAA/E;;IAEA,CAAC;IAAD,oBAAC;AAAD,CAAC,AAFD,CAAmE,eAAM,GAExE;AAFqB,sCAAa"}
|
package/build/Entity.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
export declare abstract class Entity<T> {
|
|
3
|
-
protected readonly _id:
|
|
1
|
+
import { EntityId } from './EntityId';
|
|
2
|
+
export declare abstract class Entity<T, H extends EntityId> {
|
|
3
|
+
protected readonly _id: H;
|
|
4
4
|
protected props: T;
|
|
5
|
-
protected constructor(props: T, id?:
|
|
6
|
-
equals(object?: Entity<T>): boolean;
|
|
7
|
-
get id():
|
|
5
|
+
protected constructor(props: T, id?: H | string | number);
|
|
6
|
+
equals(object?: Entity<T, H>): boolean;
|
|
7
|
+
get id(): H;
|
|
8
|
+
toPrimitive(): T;
|
|
9
|
+
toJSON(): T;
|
|
8
10
|
}
|
package/build/Entity.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Entity = void 0;
|
|
4
|
-
var
|
|
5
|
-
var uuid_by_string_1 = tslib_1.__importDefault(require("uuid-by-string"));
|
|
6
|
-
var cuid2_1 = require("@paralleldrive/cuid2");
|
|
4
|
+
var EntityId_1 = require("./EntityId");
|
|
7
5
|
var isEntity = function (v) {
|
|
8
6
|
return v instanceof Entity;
|
|
9
7
|
};
|
|
10
8
|
var Entity = /** @class */ (function () {
|
|
11
9
|
function Entity(props, id) {
|
|
12
|
-
|
|
10
|
+
if (id) {
|
|
11
|
+
this._id = (typeof id === 'object' ? id : new EntityId_1.EntityId(id));
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
this._id = (new EntityId_1.EntityId());
|
|
15
|
+
}
|
|
13
16
|
this.props = props;
|
|
14
17
|
}
|
|
15
18
|
Entity.prototype.equals = function (object) {
|
|
@@ -22,7 +25,7 @@ var Entity = /** @class */ (function () {
|
|
|
22
25
|
if (!isEntity(object)) {
|
|
23
26
|
return false;
|
|
24
27
|
}
|
|
25
|
-
return this._id
|
|
28
|
+
return this._id.equals(object._id);
|
|
26
29
|
};
|
|
27
30
|
Object.defineProperty(Entity.prototype, "id", {
|
|
28
31
|
get: function () {
|
|
@@ -31,6 +34,12 @@ var Entity = /** @class */ (function () {
|
|
|
31
34
|
enumerable: false,
|
|
32
35
|
configurable: true
|
|
33
36
|
});
|
|
37
|
+
Entity.prototype.toPrimitive = function () {
|
|
38
|
+
return this.props;
|
|
39
|
+
};
|
|
40
|
+
Entity.prototype.toJSON = function () {
|
|
41
|
+
return this.toPrimitive();
|
|
42
|
+
};
|
|
34
43
|
return Entity;
|
|
35
44
|
}());
|
|
36
45
|
exports.Entity = Entity;
|
package/build/Entity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Entity.js","sourceRoot":"","sources":["../src/Entity.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Entity.js","sourceRoot":"","sources":["../src/Entity.ts"],"names":[],"mappings":";;;AAAA,uCAAsC;AAEtC,IAAM,QAAQ,GAAG,UAAwB,CAAe;IACtD,OAAO,CAAC,YAAY,MAAM,CAAA;AAC5B,CAAC,CAAA;AAED;IAIE,gBAAsB,KAAQ,EAAE,EAAoB;QAClD,IAAI,EAAE,EAAE,CAAC;YACP,IAAI,CAAC,GAAG,GAAM,CAAC,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,mBAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;QACjE,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,GAAG,GAAM,CAAC,IAAI,mBAAQ,EAAE,CAAC,CAAC;QACjC,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAEM,uBAAM,GAAb,UAAc,MAAqB;QACjC,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YAC5C,OAAO,KAAK,CAAA;QACd,CAAC;QAED,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,OAAO,IAAI,CAAA;QACb,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YACtB,OAAO,KAAK,CAAA;QACd,CAAC;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACrC,CAAC;IAED,sBAAI,sBAAE;aAAN;YACE,OAAO,IAAI,CAAC,GAAG,CAAA;QACjB,CAAC;;;OAAA;IAED,4BAAW,GAAX;QACE,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,uBAAM,GAAN;QACE,OAAO,IAAI,CAAC,WAAW,EAAE,CAAA;IAC3B,CAAC;IACH,aAAC;AAAD,CAAC,AAxCD,IAwCC;AAxCqB,wBAAM"}
|
package/build/Entity.spec.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var tslib_1 = require("tslib");
|
|
4
4
|
var Entity_1 = require("./Entity");
|
|
5
|
+
var EntityId_1 = require("./EntityId");
|
|
5
6
|
var TestEntity = /** @class */ (function (_super) {
|
|
6
7
|
tslib_1.__extends(TestEntity, _super);
|
|
7
8
|
function TestEntity() {
|
|
@@ -15,19 +16,24 @@ var TestEntity = /** @class */ (function (_super) {
|
|
|
15
16
|
describe('Entity', function () {
|
|
16
17
|
var entity;
|
|
17
18
|
beforeAll(function () {
|
|
18
|
-
entity = TestEntity.create({ name: 'test' },
|
|
19
|
+
entity = TestEntity.create({ name: 'test' }, 1);
|
|
19
20
|
});
|
|
20
21
|
test('base', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
21
|
-
var entityNew, entity2;
|
|
22
|
+
var entityNew, entity2, entity3;
|
|
22
23
|
return tslib_1.__generator(this, function (_a) {
|
|
23
24
|
entityNew = TestEntity.create({ name: 'test2' });
|
|
24
|
-
expect(entityNew.id.length).toEqual(36); // random id
|
|
25
|
-
entity2 = TestEntity.create({ name: 'test2' },
|
|
25
|
+
expect(entityNew.id.toString().length).toEqual(36); // random id
|
|
26
|
+
entity2 = TestEntity.create({ name: 'test2' }, 1);
|
|
26
27
|
expect(entity.equals(entity2)).toBeTruthy(); // same id
|
|
27
28
|
expect(entity.equals(entity)).toBeTruthy(); // same instance
|
|
28
29
|
expect(entity.equals(null)).toBeFalsy(); // null
|
|
29
|
-
expect(entity.equals({ id:
|
|
30
|
-
expect(entity.id).toEqual('1');
|
|
30
|
+
expect(entity.equals({ id: new EntityId_1.EntityId(1) })).toBeFalsy(); // not Entity
|
|
31
|
+
expect(entity.id.toPrimitive()).toEqual('1');
|
|
32
|
+
expect(entity.id.toString()).toEqual('6061cf31-edcb-5d22-9adc-927af7c186fa');
|
|
33
|
+
entity3 = TestEntity.create({ name: 'test2' }, entity.id);
|
|
34
|
+
expect(entity.equals(entity3)).toBeTruthy(); // same id
|
|
35
|
+
expect(entity3.id.toPrimitive()).toEqual('1');
|
|
36
|
+
expect(entity3.id.toString()).toEqual('6061cf31-edcb-5d22-9adc-927af7c186fa');
|
|
31
37
|
return [2 /*return*/];
|
|
32
38
|
});
|
|
33
39
|
}); });
|
package/build/Entity.spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Entity.spec.js","sourceRoot":"","sources":["../src/Entity.spec.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;
|
|
1
|
+
{"version":3,"file":"Entity.spec.js","sourceRoot":"","sources":["../src/Entity.spec.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAClC,uCAAsC;AAKtC;IAAyB,sCAA2B;IAApD;;IAIA,CAAC;IAHQ,iBAAM,GAAb,UAAc,KAAgB,EAAE,EAA0B;QACxD,OAAO,IAAI,UAAU,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACnC,CAAC;IACH,iBAAC;AAAD,CAAC,AAJD,CAAyB,eAAM,GAI9B;AAED,QAAQ,CAAC,QAAQ,EAAE;IACjB,IAAI,MAAkB,CAAC;IAEvB,SAAS,CAAC;QACR,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,MAAM,EAAE;;;YACL,SAAS,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;YACvD,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY;YAE1D,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;YACxD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,UAAU;YACvD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,gBAAgB;YAC5D,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,OAAO;YAChD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAa,EAAE,EAAE,EAAE,IAAI,mBAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,aAAa;YACrF,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC7C,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,sCAAsC,CAAC,CAAC;YAEvE,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;YAChE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,UAAU;YACvD,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC9C,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,sCAAsC,CAAC,CAAC;;;SAC/E,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityId = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var uuid_by_string_1 = tslib_1.__importDefault(require("uuid-by-string"));
|
|
6
|
+
var cuid2_1 = require("@paralleldrive/cuid2");
|
|
7
|
+
var EntityId = /** @class */ (function () {
|
|
8
|
+
function EntityId(recordId, uuid) {
|
|
9
|
+
if (recordId === void 0) { recordId = null; }
|
|
10
|
+
if (uuid === void 0) { uuid = null; }
|
|
11
|
+
this._recordId = null;
|
|
12
|
+
this._uuid = null;
|
|
13
|
+
this._recordId = recordId ? recordId.toString() : (0, cuid2_1.createId)();
|
|
14
|
+
if (!uuid) {
|
|
15
|
+
this._uuid = (0, uuid_by_string_1.default)(recordId ? recordId.toString() : (0, cuid2_1.createId)(), (0, uuid_by_string_1.default)(this.constructor.name));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
EntityId.prototype.toPrimitive = function () {
|
|
19
|
+
return this._recordId;
|
|
20
|
+
};
|
|
21
|
+
EntityId.prototype.toString = function () {
|
|
22
|
+
return this._uuid;
|
|
23
|
+
};
|
|
24
|
+
EntityId.prototype.equals = function (id) {
|
|
25
|
+
return this._uuid === id._uuid;
|
|
26
|
+
};
|
|
27
|
+
return EntityId;
|
|
28
|
+
}());
|
|
29
|
+
exports.EntityId = EntityId;
|
|
30
|
+
//# sourceMappingURL=EntityId.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EntityId.js","sourceRoot":"","sources":["../src/EntityId.ts"],"names":[],"mappings":";;;;AAAA,0EAAuC;AACvC,8CAA+C;AAE/C;IAIE,kBAAY,QAA8B,EAAE,IAAmB;QAAnD,yBAAA,EAAA,eAA8B;QAAE,qBAAA,EAAA,WAAmB;QAHrD,cAAS,GAAW,IAAI,CAAC;QACzB,UAAK,GAAW,IAAI,CAAC;QAG7B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAA,gBAAQ,GAAE,CAAC;QAC7D,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,IAAI,CAAC,KAAK,GAAG,IAAA,wBAAU,EAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAA,gBAAQ,GAAE,EAAE,IAAA,wBAAU,EAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;QAC1G,CAAC;IACH,CAAC;IAED,8BAAW,GAAX;QACE,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,2BAAQ,GAAR;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,yBAAM,GAAN,UAAO,EAAY;QACjB,OAAO,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC;IACjC,CAAC;IACH,eAAC;AAAD,CAAC,AAtBD,IAsBC;AAtBY,4BAAQ"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Entity
|
|
1
|
+
import { Entity } from './Entity';
|
|
2
|
+
import { EntityId } from './EntityId';
|
|
2
3
|
import { ValueObject } from './ValueObject';
|
|
3
4
|
import { AggregateRoot } from './AggregateRoot';
|
|
4
5
|
export { Entity, EntityId, ValueObject, AggregateRoot };
|
package/build/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AggregateRoot = exports.ValueObject = exports.Entity = void 0;
|
|
3
|
+
exports.AggregateRoot = exports.ValueObject = exports.EntityId = exports.Entity = void 0;
|
|
4
4
|
var Entity_1 = require("./Entity");
|
|
5
5
|
Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return Entity_1.Entity; } });
|
|
6
|
+
var EntityId_1 = require("./EntityId");
|
|
7
|
+
Object.defineProperty(exports, "EntityId", { enumerable: true, get: function () { return EntityId_1.EntityId; } });
|
|
6
8
|
var ValueObject_1 = require("./ValueObject");
|
|
7
9
|
Object.defineProperty(exports, "ValueObject", { enumerable: true, get: function () { return ValueObject_1.ValueObject; } });
|
|
8
10
|
var AggregateRoot_1 = require("./AggregateRoot");
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAMhC,uFANO,eAAM,OAMP;AALR,uCAAsC;AAMpC,yFANO,mBAAQ,OAMP;AALV,6CAA4C;AAM1C,4FANO,yBAAW,OAMP;AALb,iDAAgD;AAM9C,8FANO,6BAAa,OAMP"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface IEventable<TEvent> {
|
|
2
|
+
registerEvent(handler: Event<TEvent>): void;
|
|
3
|
+
releaseEvents(): Event<TEvent>[];
|
|
4
|
+
}
|
|
5
|
+
export declare class Event<TEvent> {
|
|
6
|
+
payload: TEvent;
|
|
7
|
+
constructor(payload: TEvent);
|
|
8
|
+
}
|
|
9
|
+
export declare function Eventable<TEvent>(): <TBase extends new (...args: any[]) => {}>(Base: TBase) => {
|
|
10
|
+
new (...args: any[]): {
|
|
11
|
+
_events: Event<TEvent>[];
|
|
12
|
+
registerEvent(handler: Event<TEvent>): void;
|
|
13
|
+
releaseEvents(): Event<TEvent>[];
|
|
14
|
+
};
|
|
15
|
+
} & TBase;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Eventable = exports.Event = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var Event = /** @class */ (function () {
|
|
6
|
+
function Event(payload) {
|
|
7
|
+
this.payload = payload;
|
|
8
|
+
}
|
|
9
|
+
return Event;
|
|
10
|
+
}());
|
|
11
|
+
exports.Event = Event;
|
|
12
|
+
function Eventable() {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
|
|
14
|
+
return function (Base) {
|
|
15
|
+
return /** @class */ (function (_super) {
|
|
16
|
+
tslib_1.__extends(class_1, _super);
|
|
17
|
+
function class_1() {
|
|
18
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
19
|
+
_this._events = [];
|
|
20
|
+
return _this;
|
|
21
|
+
}
|
|
22
|
+
class_1.prototype.registerEvent = function (handler) {
|
|
23
|
+
this._events.push(handler);
|
|
24
|
+
};
|
|
25
|
+
class_1.prototype.releaseEvents = function () {
|
|
26
|
+
var list = this._events;
|
|
27
|
+
this._events = [];
|
|
28
|
+
return list;
|
|
29
|
+
};
|
|
30
|
+
return class_1;
|
|
31
|
+
}(Base));
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
exports.Eventable = Eventable;
|
|
35
|
+
//# sourceMappingURL=Eventable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Eventable.js","sourceRoot":"","sources":["../../src/mixins/Eventable.ts"],"names":[],"mappings":";;;;AAIA;IACE,eAAmB,OAAe;QAAf,YAAO,GAAP,OAAO,CAAQ;IAAG,CAAC;IACxC,YAAC;AAAD,CAAC,AAFD,IAEC;AAFY,sBAAK;AAIlB,SACS,SAAS;IAChB,8IAA8I;IAC9I,OAAO,UAAoD,IAAW;QACpE;YAAqB,mCAAI;YAAlB;;gBACL,aAAO,GAAoB,EAAE,CAAC;;YAWhC,CAAC;YATC,+BAAa,GAAb,UAAc,OAAsB;gBAClC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC7B,CAAC;YAED,+BAAa,GAAb;gBACE,IAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC;gBAC1B,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;gBAClB,OAAO,IAAI,CAAC;YACd,CAAC;YACH,cAAC;QAAD,CAAC,AAZM,CAAc,IAAI,GAYvB;IACJ,CAAC,CAAC;AACJ,CAAC;AAlBD,8BAkBC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
var Eventable_1 = require("./Eventable");
|
|
5
|
+
var MyDomainEntity = /** @class */ (function () {
|
|
6
|
+
function MyDomainEntity() {
|
|
7
|
+
}
|
|
8
|
+
return MyDomainEntity;
|
|
9
|
+
}());
|
|
10
|
+
var EventableMyDomainEntity = (0, Eventable_1.Eventable)()(MyDomainEntity);
|
|
11
|
+
describe('Eventable', function () {
|
|
12
|
+
var entity;
|
|
13
|
+
beforeAll(function () {
|
|
14
|
+
entity = new EventableMyDomainEntity();
|
|
15
|
+
});
|
|
16
|
+
test('base', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
17
|
+
var event;
|
|
18
|
+
return tslib_1.__generator(this, function (_a) {
|
|
19
|
+
event = new Eventable_1.Event({ message: 'hello' });
|
|
20
|
+
entity.registerEvent(event);
|
|
21
|
+
expect(entity.releaseEvents()).toEqual([event]);
|
|
22
|
+
expect(entity.releaseEvents()).toEqual([]);
|
|
23
|
+
return [2 /*return*/];
|
|
24
|
+
});
|
|
25
|
+
}); });
|
|
26
|
+
});
|
|
27
|
+
//# sourceMappingURL=Eventable.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Eventable.spec.js","sourceRoot":"","sources":["../../src/mixins/Eventable.spec.ts"],"names":[],"mappings":";;;AAAA,yCAA+C;AAM/C;IAAA;IACA,CAAC;IAAD,qBAAC;AAAD,CAAC,AADD,IACC;AACD,IAAM,uBAAuB,GAAG,IAAA,qBAAS,GAAkB,CAAC,cAAc,CAAC,CAAC;AAE5E,QAAQ,CAAC,WAAW,EAAE;IACpB,IAAI,MAAoD,CAAC;IAEzD,SAAS,CAAC;QACR,MAAM,GAAG,IAAI,uBAAuB,EAAE,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,MAAM,EAAE;;;YACL,KAAK,GAAG,IAAI,iBAAK,CAAiB,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;YAC9D,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAE5B,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YAChD,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;;;SAC5C,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/AggregateRoot.ts
CHANGED
package/src/Entity.spec.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Entity } from './Entity';
|
|
2
|
+
import { EntityId } from './EntityId';
|
|
2
3
|
|
|
3
4
|
interface TestProps {
|
|
4
5
|
name: string;
|
|
5
6
|
}
|
|
6
|
-
class TestEntity extends Entity<TestProps> {
|
|
7
|
-
static create(props: TestProps, id?:string): TestEntity {
|
|
7
|
+
class TestEntity extends Entity<TestProps, EntityId> {
|
|
8
|
+
static create(props: TestProps, id?:EntityId|string|number): TestEntity {
|
|
8
9
|
return new TestEntity(props, id);
|
|
9
10
|
}
|
|
10
11
|
}
|
|
@@ -13,18 +14,24 @@ describe('Entity', () => {
|
|
|
13
14
|
let entity: TestEntity;
|
|
14
15
|
|
|
15
16
|
beforeAll(() => {
|
|
16
|
-
entity = TestEntity.create({ name: 'test' },
|
|
17
|
+
entity = TestEntity.create({ name: 'test' }, 1);
|
|
17
18
|
});
|
|
18
19
|
|
|
19
20
|
test('base', async () => {
|
|
20
21
|
const entityNew = TestEntity.create({ name: 'test2' });
|
|
21
|
-
expect(entityNew.id.length).toEqual(36); // random id
|
|
22
|
+
expect(entityNew.id.toString().length).toEqual(36); // random id
|
|
22
23
|
|
|
23
|
-
const entity2 = TestEntity.create({ name: 'test2' },
|
|
24
|
+
const entity2 = TestEntity.create({ name: 'test2' }, 1);
|
|
24
25
|
expect(entity.equals(entity2)).toBeTruthy(); // same id
|
|
25
26
|
expect(entity.equals(entity)).toBeTruthy(); // same instance
|
|
26
27
|
expect(entity.equals(null)).toBeFalsy(); // null
|
|
27
|
-
expect(entity.equals(<TestEntity>{ id:
|
|
28
|
-
expect(entity.id).toEqual('1');
|
|
28
|
+
expect(entity.equals(<TestEntity>{ id: new EntityId(1) })).toBeFalsy(); // not Entity
|
|
29
|
+
expect(entity.id.toPrimitive()).toEqual('1');
|
|
30
|
+
expect(entity.id.toString()).toEqual('6061cf31-edcb-5d22-9adc-927af7c186fa');
|
|
31
|
+
|
|
32
|
+
const entity3 = TestEntity.create({ name: 'test2' }, entity.id);
|
|
33
|
+
expect(entity.equals(entity3)).toBeTruthy(); // same id
|
|
34
|
+
expect(entity3.id.toPrimitive()).toEqual('1');
|
|
35
|
+
expect(entity3.id.toString()).toEqual('6061cf31-edcb-5d22-9adc-927af7c186fa');
|
|
29
36
|
});
|
|
30
37
|
});
|
package/src/Entity.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { createId } from '@paralleldrive/cuid2'
|
|
1
|
+
import { EntityId } from './EntityId';
|
|
3
2
|
|
|
4
|
-
const isEntity = <T>(v: Entity<T>): v is Entity<T> => {
|
|
3
|
+
const isEntity = <T, H extends EntityId>(v: Entity<T, H>): v is Entity<T, H> => {
|
|
5
4
|
return v instanceof Entity
|
|
6
5
|
}
|
|
7
6
|
|
|
8
|
-
export
|
|
7
|
+
export abstract class Entity<T, H extends EntityId> {
|
|
8
|
+
protected readonly _id: H;
|
|
9
|
+
protected props: T;
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
protected constructor(props: T, id?: H|string|number) {
|
|
12
|
+
if (id) {
|
|
13
|
+
this._id = <H>(typeof id === 'object' ? id : new EntityId(id));
|
|
14
|
+
} else {
|
|
15
|
+
this._id = <H>(new EntityId());
|
|
16
|
+
}
|
|
16
17
|
this.props = props
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
public equals(object?: Entity<T>): boolean {
|
|
20
|
+
public equals(object?: Entity<T, H>): boolean {
|
|
20
21
|
if (object === null || object === undefined) {
|
|
21
22
|
return false
|
|
22
23
|
}
|
|
@@ -29,10 +30,18 @@ export abstract class Entity<T> {
|
|
|
29
30
|
return false
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
return this._id
|
|
33
|
+
return this._id.equals(object._id);
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
get id():
|
|
36
|
+
get id(): H {
|
|
36
37
|
return this._id
|
|
37
38
|
}
|
|
39
|
+
|
|
40
|
+
toPrimitive(): T {
|
|
41
|
+
return this.props
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
toJSON(): T {
|
|
45
|
+
return this.toPrimitive()
|
|
46
|
+
}
|
|
38
47
|
}
|
package/src/EntityId.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import createUUID from 'uuid-by-string'
|
|
2
|
+
import { createId } from '@paralleldrive/cuid2'
|
|
3
|
+
|
|
4
|
+
export class EntityId {
|
|
5
|
+
protected _recordId: string = null;
|
|
6
|
+
protected _uuid: string = null;
|
|
7
|
+
|
|
8
|
+
constructor(recordId: string|number = null, uuid: string = null) {
|
|
9
|
+
this._recordId = recordId ? recordId.toString() : createId();
|
|
10
|
+
if (!uuid) {
|
|
11
|
+
this._uuid = createUUID(recordId ? recordId.toString() : createId(), createUUID(this.constructor.name));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
toPrimitive(): string {
|
|
16
|
+
return this._recordId;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
toString(): string {
|
|
20
|
+
return this._uuid;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
equals(id: EntityId): boolean {
|
|
24
|
+
return this._uuid === id._uuid;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Event, Eventable } from './Eventable';
|
|
2
|
+
|
|
3
|
+
interface MyEventPayload {
|
|
4
|
+
message: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
class MyDomainEntity {
|
|
8
|
+
}
|
|
9
|
+
const EventableMyDomainEntity = Eventable<MyEventPayload>()(MyDomainEntity);
|
|
10
|
+
|
|
11
|
+
describe('Eventable', () => {
|
|
12
|
+
let entity: InstanceType<typeof EventableMyDomainEntity>;
|
|
13
|
+
|
|
14
|
+
beforeAll(() => {
|
|
15
|
+
entity = new EventableMyDomainEntity();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('base', async () => {
|
|
19
|
+
const event = new Event<MyEventPayload>({ message: 'hello' });
|
|
20
|
+
entity.registerEvent(event);
|
|
21
|
+
|
|
22
|
+
expect(entity.releaseEvents()).toEqual([event]);
|
|
23
|
+
expect(entity.releaseEvents()).toEqual([]);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface IEventable<TEvent> {
|
|
2
|
+
registerEvent(handler: Event<TEvent>): void;
|
|
3
|
+
releaseEvents():Event<TEvent>[];
|
|
4
|
+
}
|
|
5
|
+
export class Event<TEvent> {
|
|
6
|
+
constructor(public payload: TEvent) {}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export
|
|
10
|
+
function Eventable<TEvent>() {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any
|
|
12
|
+
return function <TBase extends new (...args: any[]) => {}>(Base: TBase) {
|
|
13
|
+
return class extends Base implements IEventable<TEvent> {
|
|
14
|
+
_events: Event<TEvent>[] = [];
|
|
15
|
+
|
|
16
|
+
registerEvent(handler: Event<TEvent>):void {
|
|
17
|
+
this._events.push(handler);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
releaseEvents(): Event<TEvent>[] {
|
|
21
|
+
const list = this._events;
|
|
22
|
+
this._events = [];
|
|
23
|
+
return list;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
}
|