ddd-node 8.1.0 → 9.1.0

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.
@@ -11,15 +11,15 @@ export interface AggregateBaseMetadata extends EntityMetadata {
11
11
  export declare abstract class AggregateBase<Props extends object> extends Entity<Props> {
12
12
  protected readonly _version: number;
13
13
  constructor(metadata: AggregateBaseMetadata, props?: Props);
14
- abstract version: number;
14
+ abstract getVersion(): number;
15
15
  protected newEvent<E extends AnyEvent>(eventClass: EventClassWithTypedConstructor<E>, props: PropsOf<E>): E;
16
16
  }
17
17
  export declare class Aggregate<Props extends object> extends AggregateBase<Props> {
18
18
  private _events;
19
19
  constructor(metadata: AggregateBaseMetadata, props: Props);
20
20
  static newAggregate<T extends AnyAggregate>(this: AggregateClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
21
- get version(): number;
22
- get events(): AnyEvent[];
21
+ getVersion(): number;
22
+ getEvents(): AnyEvent[];
23
23
  protected recordEvent<E extends AnyEvent>(event: E): void;
24
24
  protected recordEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>): void;
25
25
  clearEvents(): void;
@@ -40,9 +40,10 @@ export declare class AggregateES<Props extends object> extends AggregateBase<Pro
40
40
  static fromSnapshot<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, snapshot: Snapshot<T>, events?: AnyEvent[]): T;
41
41
  static eventAppliersMap<T extends AnyAggregateES>(this: AggregateESClass<T>): Map<string, EventApplier>;
42
42
  static commandHandlersMap<T extends AnyAggregateES>(this: AggregateESClass<T>): Map<string, CommandHandler>;
43
- get version(): number;
44
- get events(): AnyEvent[];
45
- get handledCommands(): AnyCommand[];
43
+ getVersion(): number;
44
+ getPastEvents(): AnyEvent[];
45
+ getEvents(): AnyEvent[];
46
+ getHandledCommands(): AnyCommand[];
46
47
  eventAppliersMap(): Map<string, EventApplier>;
47
48
  hasEvent(): boolean;
48
49
  getApplierForEvent<E extends AnyEvent>(event: E): EventApplier<E>;
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AggregateES = exports.Aggregate = exports.AggregateBase = void 0;
4
4
  const to_array_1 = require("../utils/to-array");
5
5
  const entity_1 = require("./entity");
6
- const id_1 = require("./id");
7
6
  const metadata_1 = require("./metadata");
8
7
  class AggregateBase extends entity_1.Entity {
9
8
  constructor(metadata, props) {
@@ -12,9 +11,9 @@ class AggregateBase extends entity_1.Entity {
12
11
  }
13
12
  newEvent(eventClass, props) {
14
13
  const eventSource = {
15
- type: this.type,
16
- id: this.id,
17
- version: this.version,
14
+ type: this.getType(),
15
+ id: this.getId(),
16
+ version: this.getVersion(),
18
17
  };
19
18
  return eventClass.newEvent(eventSource, props);
20
19
  }
@@ -27,14 +26,14 @@ class Aggregate extends AggregateBase {
27
26
  }
28
27
  static newAggregate(props, id) {
29
28
  return new this({
30
- id: id !== null && id !== void 0 ? id : id_1.Uuid4.new(),
29
+ id: this.id(id),
31
30
  version: 0,
32
31
  }, props);
33
32
  }
34
- get version() {
33
+ getVersion() {
35
34
  return this._version;
36
35
  }
37
- get events() {
36
+ getEvents() {
38
37
  return this._events;
39
38
  }
40
39
  recordEvent(param1, param2) {
@@ -61,7 +60,7 @@ class AggregateES extends AggregateBase {
61
60
  }
62
61
  static newStream(id) {
63
62
  return new this({
64
- id: id !== null && id !== void 0 ? id : id_1.Uuid4.new(),
63
+ id: this.id(id),
65
64
  version: 0,
66
65
  });
67
66
  }
@@ -82,14 +81,16 @@ class AggregateES extends AggregateBase {
82
81
  static commandHandlersMap() {
83
82
  return (0, metadata_1.getCommandHandlersMap)(this.prototype);
84
83
  }
85
- get version() {
86
- return (this._version +
87
- (this.hasEvent() ? this._events.length : this._pastEvents.length));
84
+ getVersion() {
85
+ return this._version + this._pastEvents.length + this._events.length;
88
86
  }
89
- get events() {
87
+ getPastEvents() {
88
+ return this._pastEvents;
89
+ }
90
+ getEvents() {
90
91
  return this._events;
91
92
  }
92
- get handledCommands() {
93
+ getHandledCommands() {
93
94
  return this._handledCommands;
94
95
  }
95
96
  eventAppliersMap() {
@@ -99,19 +100,19 @@ class AggregateES extends AggregateBase {
99
100
  return Boolean(this._events.length);
100
101
  }
101
102
  getApplierForEvent(event) {
102
- const { type } = event;
103
- const applier = this.eventAppliersMap().get(type);
103
+ const eventType = event.getType();
104
+ const applier = this.eventAppliersMap().get(eventType);
104
105
  if (!applier)
105
106
  throw new Error("Event applier not found");
106
107
  return applier;
107
108
  }
108
109
  validateEventBeforeApply(event) {
109
- const { source } = event;
110
- if (source.type !== this.type)
110
+ const eventSource = event.getSource();
111
+ if (eventSource.type !== this.getType())
111
112
  throw new Error("Invalid source type");
112
- if (!source.id.equals(this.id))
113
+ if (!eventSource.id.equals(this._id))
113
114
  throw new Error("Invalid source id");
114
- if (source.version !== this.version)
115
+ if (eventSource.version !== this.getVersion())
115
116
  throw new Error("Invalid source version");
116
117
  }
117
118
  _applyEvent(event) {
@@ -143,8 +144,8 @@ class AggregateES extends AggregateBase {
143
144
  return this.constructor.commandHandlersMap();
144
145
  }
145
146
  getHandlerForCommand(command) {
146
- const { type } = command;
147
- const handler = this.commandHandlersMap().get(type);
147
+ const commandType = command.getType();
148
+ const handler = this.commandHandlersMap().get(commandType);
148
149
  if (!handler)
149
150
  throw new Error("Command handler not found");
150
151
  return handler;
@@ -155,8 +156,8 @@ class AggregateES extends AggregateBase {
155
156
  events.forEach((event) => {
156
157
  var _a;
157
158
  event.setContext({
158
- correlationId: (_a = command.context) === null || _a === void 0 ? void 0 : _a.correlationId,
159
- causationId: command.id.value,
159
+ correlationId: (_a = command.getContext()) === null || _a === void 0 ? void 0 : _a.correlationId,
160
+ causationId: command.getId().value,
160
161
  });
161
162
  });
162
163
  this.applyEvents(events);
@@ -168,8 +169,8 @@ class AggregateES extends AggregateBase {
168
169
  throw new Error();
169
170
  return {
170
171
  metadata: {
171
- id: this.id,
172
- version: this.version,
172
+ id: this.getId(),
173
+ version: this.getVersion(),
173
174
  },
174
175
  props: this.props(),
175
176
  };
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Command = void 0;
4
- const id_1 = require("./id");
5
4
  const message_1 = require("./message");
6
5
  class Command extends message_1.Message {
7
6
  constructor(metadata, props) {
@@ -9,7 +8,7 @@ class Command extends message_1.Message {
9
8
  }
10
9
  static newCommand(props, context) {
11
10
  return new this({
12
- id: id_1.Uuid4.new(),
11
+ id: this.id(),
13
12
  timestamp: Date.now(),
14
13
  context,
15
14
  }, props);
@@ -1,15 +1,16 @@
1
1
  import { Class } from "../types/class";
2
2
  import { ClassStatic } from "../types/class-static";
3
3
  import { Id } from "./id";
4
- import { Model, PropsOf } from "./model";
4
+ import { PropsOf } from "./model";
5
+ import { ModelWithId } from "./model-with-id";
5
6
  export interface EntityMetadata {
6
7
  readonly id: Id;
7
8
  }
8
- export declare class Entity<Props extends object> extends Model<Props> {
9
- private readonly _id;
9
+ export declare class Entity<Props extends object> extends ModelWithId<Props> {
10
+ protected readonly _id: Id;
10
11
  constructor(metadata: EntityMetadata, props?: Props);
11
- static newEntity<T extends AnyEntity>(this: EntityClassWithTypedConstructor<T>, props: PropsOf<T>): T;
12
- get id(): Id;
12
+ static newEntity<T extends AnyEntity>(this: EntityClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
13
+ getId(): Id;
13
14
  hasId(id: Id): boolean;
14
15
  }
15
16
  export type AnyEntity = Entity<object>;
@@ -1,23 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Entity = void 0;
4
- const id_1 = require("./id");
5
- const model_1 = require("./model");
6
- class Entity extends model_1.Model {
4
+ const model_with_id_1 = require("./model-with-id");
5
+ class Entity extends model_with_id_1.ModelWithId {
7
6
  constructor(metadata, props) {
8
7
  super(props);
9
8
  this._id = metadata.id;
10
9
  }
11
- static newEntity(props) {
10
+ static newEntity(props, id) {
12
11
  return new this({
13
- id: id_1.Uuid4.new(),
12
+ id: this.id(id),
14
13
  }, props);
15
14
  }
16
- get id() {
15
+ getId() {
17
16
  return this._id;
18
17
  }
19
18
  hasId(id) {
20
- return this.id.equals(id);
19
+ return this._id.equals(id);
21
20
  }
22
21
  }
23
22
  exports.Entity = Entity;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ModelError = void 0;
4
4
  class ModelError extends Error {
5
5
  constructor(model, message) {
6
- super(`[${model.type}] ${message}`);
6
+ super(`[${model.getType()}] ${message}`);
7
7
  }
8
8
  }
9
9
  exports.ModelError = ModelError;
@@ -16,7 +16,7 @@ export declare class Event<Props extends object> extends Message<Props> {
16
16
  private readonly _source;
17
17
  constructor(metadata: EventMetadata, props: Props);
18
18
  static newEvent<T extends AnyEvent>(this: EventClassWithTypedConstructor<T>, source: EventSource, props: PropsOf<T>, context?: MessageContext): T;
19
- get source(): Readonly<{
19
+ getSource(): Readonly<{
20
20
  type: `${string}#${string}`;
21
21
  id: Id;
22
22
  version: number;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Event = void 0;
4
- const id_1 = require("./id");
5
4
  const message_1 = require("./message");
6
5
  class Event extends message_1.Message {
7
6
  constructor(metadata, props) {
@@ -10,13 +9,13 @@ class Event extends message_1.Message {
10
9
  }
11
10
  static newEvent(source, props, context) {
12
11
  return new this({
13
- id: id_1.Uuid4.new(),
12
+ id: this.id(),
14
13
  timestamp: Date.now(),
15
14
  source,
16
15
  context,
17
16
  }, props);
18
17
  }
19
- get source() {
18
+ getSource() {
20
19
  return this._source;
21
20
  }
22
21
  }
package/dist/core/id.d.ts CHANGED
@@ -4,9 +4,14 @@ export declare class Id {
4
4
  get value(): string;
5
5
  equals(id: Id): boolean;
6
6
  }
7
- export declare class Uuid4 extends Id {
8
- private constructor();
9
- static new(): Uuid4;
10
- static from(value: string): Uuid4;
11
- static validate(value: string): boolean;
7
+ export declare abstract class IdGenerator {
8
+ abstract generateValue(): string;
9
+ abstract validateValue(value: string): void;
10
+ fromValue(value: string): Id;
11
+ fromId(id: Id): Id;
12
+ newId(): Id;
13
+ }
14
+ export declare class Uuid4Generator extends IdGenerator {
15
+ generateValue(): string;
16
+ validateValue(value: string): boolean;
12
17
  }
package/dist/core/id.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Uuid4 = exports.Id = void 0;
3
+ exports.Uuid4Generator = exports.IdGenerator = exports.Id = void 0;
4
4
  const uuid_1 = require("uuid");
5
5
  class Id {
6
6
  constructor(value) {
@@ -14,22 +14,27 @@ class Id {
14
14
  }
15
15
  }
16
16
  exports.Id = Id;
17
- class Uuid4 extends Id {
18
- constructor(value) {
19
- super(value);
17
+ class IdGenerator {
18
+ fromValue(value) {
19
+ this.validateValue(value);
20
+ return new Id(value);
21
+ }
22
+ fromId(id) {
23
+ return this.fromValue(id.value);
20
24
  }
21
- static new() {
22
- const newValue = (0, uuid_1.v4)();
23
- return this.from(newValue);
25
+ newId() {
26
+ return this.fromValue(this.generateValue());
24
27
  }
25
- static from(value) {
26
- this.validate(value);
27
- return new Uuid4(value);
28
+ }
29
+ exports.IdGenerator = IdGenerator;
30
+ class Uuid4Generator extends IdGenerator {
31
+ generateValue() {
32
+ return (0, uuid_1.v4)();
28
33
  }
29
- static validate(value) {
34
+ validateValue(value) {
30
35
  const isUuid = (0, uuid_1.validate)(value);
31
36
  const isV4 = (0, uuid_1.version)(value) === 4;
32
37
  return isUuid && isV4;
33
38
  }
34
39
  }
35
- exports.Uuid4 = Uuid4;
40
+ exports.Uuid4Generator = Uuid4Generator;
@@ -8,6 +8,7 @@ export * from "./message";
8
8
  export * from "./metadata";
9
9
  export * from "./model";
10
10
  export * from "./model-type";
11
+ export * from "./model-with-id";
11
12
  export * from "./registry";
12
13
  export * from "./repository";
13
14
  export * from "./value-object";
@@ -24,6 +24,7 @@ __exportStar(require("./message"), exports);
24
24
  __exportStar(require("./metadata"), exports);
25
25
  __exportStar(require("./model"), exports);
26
26
  __exportStar(require("./model-type"), exports);
27
+ __exportStar(require("./model-with-id"), exports);
27
28
  __exportStar(require("./registry"), exports);
28
29
  __exportStar(require("./repository"), exports);
29
30
  __exportStar(require("./value-object"), exports);
@@ -1,5 +1,5 @@
1
1
  import { Id } from "./id";
2
- import { Model } from "./model";
2
+ import { ModelWithId } from "./model-with-id";
3
3
  export interface MessageContext {
4
4
  correlationId?: string;
5
5
  causationId?: string;
@@ -9,13 +9,13 @@ export interface MessageMetadata {
9
9
  readonly timestamp: number;
10
10
  context?: MessageContext;
11
11
  }
12
- export declare class Message<Props extends object> extends Model<Props> {
12
+ export declare class Message<Props extends object> extends ModelWithId<Props> {
13
13
  private readonly _id;
14
14
  private readonly _timestamp;
15
15
  private _context?;
16
16
  protected constructor(metadata: MessageMetadata, props: Props);
17
- get id(): Id;
18
- get timestamp(): number;
19
- get context(): MessageContext | undefined;
17
+ getId(): Id;
18
+ getTimestamp(): number;
19
+ getContext(): MessageContext | undefined;
20
20
  setContext(context: Partial<MessageContext>): void;
21
21
  }
@@ -5,8 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Message = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
- const model_1 = require("./model");
9
- class Message extends model_1.Model {
8
+ const model_with_id_1 = require("./model-with-id");
9
+ class Message extends model_with_id_1.ModelWithId {
10
10
  constructor(metadata, props) {
11
11
  var _a;
12
12
  super(props);
@@ -14,17 +14,17 @@ class Message extends model_1.Model {
14
14
  this._timestamp = metadata.timestamp;
15
15
  this._context = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.context) !== null && _a !== void 0 ? _a : {};
16
16
  }
17
- get id() {
17
+ getId() {
18
18
  return this._id;
19
19
  }
20
- get timestamp() {
20
+ getTimestamp() {
21
21
  return this._timestamp;
22
22
  }
23
- get context() {
23
+ getContext() {
24
24
  return this._context;
25
25
  }
26
26
  setContext(context) {
27
- this._context = lodash_1.default.merge(this.context, context);
27
+ this._context = lodash_1.default.merge(this._context, context);
28
28
  }
29
29
  }
30
30
  exports.Message = Message;
@@ -3,9 +3,13 @@ import { CommandHandler, EventApplier } from "./aggregate";
3
3
  import { AnyCommand } from "./command";
4
4
  import { AnyEvent } from "./event";
5
5
  import { ModelTypePattern } from "./model-type";
6
+ import { IdGenerator } from "./id";
6
7
  export declare const MODEL_TYPE = "MODEL_TYPE";
7
8
  export declare const defineModelType: (target: object, type: ModelTypePattern) => void;
8
9
  export declare const getModelType: (target: object) => ModelTypePattern;
10
+ export declare const ID_GENERATOR = "ID_GENERATOR";
11
+ export declare const defineIdGenerator: (target: object, idGenerator: IdGenerator) => void;
12
+ export declare const getIdGenerator: (target: object) => any;
9
13
  export declare const EVENT_APPLIERS = "EVENT_APPLIERS";
10
14
  export declare const getEventAppliersMap: (target: object) => Map<string, EventApplier>;
11
15
  export declare const defineEventApplier: <T extends AnyEvent>(target: object, eventType: string, applier: EventApplier<T>) => void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defineCommandHandler = exports.getCommandHandlersMap = exports.COMMAND_HANDLERS = exports.defineEventApplier = exports.getEventAppliersMap = exports.EVENT_APPLIERS = exports.getModelType = exports.defineModelType = exports.MODEL_TYPE = void 0;
3
+ exports.defineCommandHandler = exports.getCommandHandlersMap = exports.COMMAND_HANDLERS = exports.defineEventApplier = exports.getEventAppliersMap = exports.EVENT_APPLIERS = exports.getIdGenerator = exports.defineIdGenerator = exports.ID_GENERATOR = exports.getModelType = exports.defineModelType = exports.MODEL_TYPE = void 0;
4
4
  require("reflect-metadata");
5
5
  // Model type
6
6
  exports.MODEL_TYPE = "MODEL_TYPE";
@@ -15,6 +15,16 @@ const getModelType = (target) => {
15
15
  return type;
16
16
  };
17
17
  exports.getModelType = getModelType;
18
+ // Id generator
19
+ exports.ID_GENERATOR = "ID_GENERATOR";
20
+ const defineIdGenerator = (target, idGenerator) => {
21
+ Reflect.defineMetadata(exports.ID_GENERATOR, idGenerator, target);
22
+ };
23
+ exports.defineIdGenerator = defineIdGenerator;
24
+ const getIdGenerator = (target) => {
25
+ return Reflect.getMetadata(exports.ID_GENERATOR, target);
26
+ };
27
+ exports.getIdGenerator = getIdGenerator;
18
28
  // Event applier map
19
29
  exports.EVENT_APPLIERS = "EVENT_APPLIERS";
20
30
  const getEventAppliersMap = (target) => {
@@ -0,0 +1,6 @@
1
+ import { Id, IdGenerator } from "./id";
2
+ import { Model } from "./model";
3
+ export declare class ModelWithId<Props extends object> extends Model<Props> {
4
+ static getIdGenerator(this: any): IdGenerator;
5
+ static id(id?: Id): Id;
6
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModelWithId = void 0;
4
+ const id_1 = require("./id");
5
+ const metadata_1 = require("./metadata");
6
+ const model_1 = require("./model");
7
+ class ModelWithId extends model_1.Model {
8
+ static getIdGenerator() {
9
+ return (0, metadata_1.getIdGenerator)(this) || new id_1.Uuid4Generator();
10
+ }
11
+ static id(id) {
12
+ const generator = this.getIdGenerator();
13
+ return id ? generator.fromId(id) : generator.newId();
14
+ }
15
+ }
16
+ exports.ModelWithId = ModelWithId;
@@ -6,7 +6,7 @@ export declare class Model<Props extends object> {
6
6
  static type(): `${string}#${string}`;
7
7
  protected initializeProps(props: Props): void;
8
8
  validate(): void;
9
- get type(): `${string}#${string}`;
9
+ getType(): `${string}#${string}`;
10
10
  props(): Props;
11
11
  }
12
12
  export type AnyModel = Model<object>;
@@ -24,7 +24,7 @@ class Model {
24
24
  }
25
25
  }
26
26
  validate() { }
27
- get type() {
27
+ getType() {
28
28
  return (0, metadata_1.getModelType)(Object.getPrototypeOf(this));
29
29
  }
30
30
  props() {
@@ -0,0 +1,2 @@
1
+ import { IdGenerator } from "../core/id";
2
+ export declare const id: <T extends IdGenerator>(idGenerator: T) => (target: object) => void;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.id = void 0;
4
+ const metadata_1 = require("../core/metadata");
5
+ const id = (idGenerator) => {
6
+ return (target) => {
7
+ console.log("Target", target);
8
+ (0, metadata_1.defineIdGenerator)(target, idGenerator);
9
+ };
10
+ };
11
+ exports.id = id;
@@ -2,5 +2,6 @@ export * from "./aggregate";
2
2
  export * from "./command";
3
3
  export * from "./entity";
4
4
  export * from "./event";
5
+ export * from "./id";
5
6
  export * from "./model";
6
7
  export * from "./value-object";
@@ -18,5 +18,6 @@ __exportStar(require("./aggregate"), exports);
18
18
  __exportStar(require("./command"), exports);
19
19
  __exportStar(require("./entity"), exports);
20
20
  __exportStar(require("./event"), exports);
21
+ __exportStar(require("./id"), exports);
21
22
  __exportStar(require("./model"), exports);
22
23
  __exportStar(require("./value-object"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddd-node",
3
- "version": "8.1.0",
3
+ "version": "9.1.0",
4
4
  "description": "Domain Driven Design base for NodeJs",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {