ddd-node 8.0.1 → 9.0.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>;
@@ -12,9 +12,9 @@ class AggregateBase extends entity_1.Entity {
12
12
  }
13
13
  newEvent(eventClass, props) {
14
14
  const eventSource = {
15
- type: this.type,
16
- id: this.id,
17
- version: this.version,
15
+ type: this.getType(),
16
+ id: this.getId(),
17
+ version: this.getVersion(),
18
18
  };
19
19
  return eventClass.newEvent(eventSource, props);
20
20
  }
@@ -31,10 +31,10 @@ class Aggregate extends AggregateBase {
31
31
  version: 0,
32
32
  }, props);
33
33
  }
34
- get version() {
34
+ getVersion() {
35
35
  return this._version;
36
36
  }
37
- get events() {
37
+ getEvents() {
38
38
  return this._events;
39
39
  }
40
40
  recordEvent(param1, param2) {
@@ -82,14 +82,16 @@ class AggregateES extends AggregateBase {
82
82
  static commandHandlersMap() {
83
83
  return (0, metadata_1.getCommandHandlersMap)(this.prototype);
84
84
  }
85
- get version() {
86
- return (this._version +
87
- (this.hasEvent() ? this._events.length : this._pastEvents.length));
85
+ getVersion() {
86
+ return this._version + this._pastEvents.length + this._events.length;
88
87
  }
89
- get events() {
88
+ getPastEvents() {
89
+ return this._pastEvents;
90
+ }
91
+ getEvents() {
90
92
  return this._events;
91
93
  }
92
- get handledCommands() {
94
+ getHandledCommands() {
93
95
  return this._handledCommands;
94
96
  }
95
97
  eventAppliersMap() {
@@ -99,19 +101,19 @@ class AggregateES extends AggregateBase {
99
101
  return Boolean(this._events.length);
100
102
  }
101
103
  getApplierForEvent(event) {
102
- const { type } = event;
103
- const applier = this.eventAppliersMap().get(type);
104
+ const eventType = event.getType();
105
+ const applier = this.eventAppliersMap().get(eventType);
104
106
  if (!applier)
105
107
  throw new Error("Event applier not found");
106
108
  return applier;
107
109
  }
108
110
  validateEventBeforeApply(event) {
109
- const { source } = event;
110
- if (source.type !== this.type)
111
+ const eventSource = event.getSource();
112
+ if (eventSource.type !== this.getType())
111
113
  throw new Error("Invalid source type");
112
- if (!source.id.equals(this.id))
114
+ if (!eventSource.id.equals(this._id))
113
115
  throw new Error("Invalid source id");
114
- if (source.version !== this.version)
116
+ if (eventSource.version !== this.getVersion())
115
117
  throw new Error("Invalid source version");
116
118
  }
117
119
  _applyEvent(event) {
@@ -143,8 +145,8 @@ class AggregateES extends AggregateBase {
143
145
  return this.constructor.commandHandlersMap();
144
146
  }
145
147
  getHandlerForCommand(command) {
146
- const { type } = command;
147
- const handler = this.commandHandlersMap().get(type);
148
+ const commandType = command.getType();
149
+ const handler = this.commandHandlersMap().get(commandType);
148
150
  if (!handler)
149
151
  throw new Error("Command handler not found");
150
152
  return handler;
@@ -155,8 +157,8 @@ class AggregateES extends AggregateBase {
155
157
  events.forEach((event) => {
156
158
  var _a;
157
159
  event.setContext({
158
- correlationId: (_a = command.context) === null || _a === void 0 ? void 0 : _a.correlationId,
159
- causationId: command.id.value,
160
+ correlationId: (_a = command.getContext()) === null || _a === void 0 ? void 0 : _a.correlationId,
161
+ causationId: command.getId().value,
160
162
  });
161
163
  });
162
164
  this.applyEvents(events);
@@ -168,8 +170,8 @@ class AggregateES extends AggregateBase {
168
170
  throw new Error();
169
171
  return {
170
172
  metadata: {
171
- id: this.id,
172
- version: this.version,
173
+ id: this.getId(),
174
+ version: this.getVersion(),
173
175
  },
174
176
  props: this.props(),
175
177
  };
@@ -6,10 +6,10 @@ export interface EntityMetadata {
6
6
  readonly id: Id;
7
7
  }
8
8
  export declare class Entity<Props extends object> extends Model<Props> {
9
- private readonly _id;
9
+ protected readonly _id: Id;
10
10
  constructor(metadata: EntityMetadata, props?: Props);
11
11
  static newEntity<T extends AnyEntity>(this: EntityClassWithTypedConstructor<T>, props: PropsOf<T>): T;
12
- get id(): Id;
12
+ getId(): Id;
13
13
  hasId(id: Id): boolean;
14
14
  }
15
15
  export type AnyEntity = Entity<object>;
@@ -13,11 +13,11 @@ class Entity extends model_1.Model {
13
13
  id: id_1.Uuid4.new(),
14
14
  }, props);
15
15
  }
16
- get id() {
16
+ getId() {
17
17
  return this._id;
18
18
  }
19
19
  hasId(id) {
20
- return this.id.equals(id);
20
+ return this._id.equals(id);
21
21
  }
22
22
  }
23
23
  exports.Entity = Entity;
@@ -0,0 +1,4 @@
1
+ import { AnyModel } from "./model";
2
+ export declare class ModelError extends Error {
3
+ constructor(model: AnyModel, message: string);
4
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModelError = void 0;
4
+ class ModelError extends Error {
5
+ constructor(model, message) {
6
+ super(`[${model.getType()}] ${message}`);
7
+ }
8
+ }
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;
@@ -16,7 +16,7 @@ class Event extends message_1.Message {
16
16
  context,
17
17
  }, props);
18
18
  }
19
- get source() {
19
+ getSource() {
20
20
  return this._source;
21
21
  }
22
22
  }
@@ -1,6 +1,7 @@
1
1
  export * from "./aggregate";
2
2
  export * from "./command";
3
3
  export * from "./entity";
4
+ export * from "./error";
4
5
  export * from "./event";
5
6
  export * from "./id";
6
7
  export * from "./message";
@@ -8,4 +9,5 @@ export * from "./metadata";
8
9
  export * from "./model";
9
10
  export * from "./model-type";
10
11
  export * from "./registry";
12
+ export * from "./repository";
11
13
  export * from "./value-object";
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./aggregate"), exports);
18
18
  __exportStar(require("./command"), exports);
19
19
  __exportStar(require("./entity"), exports);
20
+ __exportStar(require("./error"), exports);
20
21
  __exportStar(require("./event"), exports);
21
22
  __exportStar(require("./id"), exports);
22
23
  __exportStar(require("./message"), exports);
@@ -24,4 +25,5 @@ __exportStar(require("./metadata"), exports);
24
25
  __exportStar(require("./model"), exports);
25
26
  __exportStar(require("./model-type"), exports);
26
27
  __exportStar(require("./registry"), exports);
28
+ __exportStar(require("./repository"), exports);
27
29
  __exportStar(require("./value-object"), exports);
@@ -14,8 +14,8 @@ export declare class Message<Props extends object> extends Model<Props> {
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
  }
@@ -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,10 @@ export declare class Model<Props extends object> {
3
3
  protected _props: Props;
4
4
  constructor(props?: Props);
5
5
  static isModel(obj: object): obj is AnyModel;
6
+ static type(): `${string}#${string}`;
6
7
  protected initializeProps(props: Props): void;
7
8
  validate(): void;
8
- get type(): `${string}#${string}`;
9
+ getType(): `${string}#${string}`;
9
10
  props(): Props;
10
11
  }
11
12
  export type AnyModel = Model<object>;
@@ -14,6 +14,9 @@ class Model {
14
14
  static isModel(obj) {
15
15
  return obj instanceof Model;
16
16
  }
17
+ static type() {
18
+ return (0, metadata_1.getModelType)(this.prototype);
19
+ }
17
20
  initializeProps(props) {
18
21
  if (!this._props) {
19
22
  this._props = props;
@@ -21,7 +24,7 @@ class Model {
21
24
  }
22
25
  }
23
26
  validate() { }
24
- get type() {
27
+ getType() {
25
28
  return (0, metadata_1.getModelType)(Object.getPrototypeOf(this));
26
29
  }
27
30
  props() {
@@ -0,0 +1,6 @@
1
+ import { AnyAggregate, AnyAggregateES } from "./aggregate";
2
+ import { Id } from "./id";
3
+ export interface IRepository<T extends AnyAggregate | AnyAggregateES> {
4
+ findById(id: Id): Promise<T | null>;
5
+ save(instance: T): Promise<any>;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddd-node",
3
- "version": "8.0.1",
3
+ "version": "9.0.0",
4
4
  "description": "Domain Driven Design base for NodeJs",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {