ddd-node 4.0.1 → 5.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.
@@ -6,21 +6,25 @@ import { AnyEvent, EventClass, NewEventMetadataOptions } from "./event";
6
6
  import { Id } from "./id";
7
7
  import { PropsOf } from "./props-envelope";
8
8
  export interface IAggregateMetadata extends IEntityMetadata {
9
+ readonly aggregateType: string;
9
10
  readonly version: number;
10
11
  }
12
+ export type NewAggregateMetadata = Partial<Omit<IAggregateMetadata, "aggregateType">>;
11
13
  export declare class Aggregate<P extends object> extends Entity<P> implements IAggregateMetadata {
14
+ private readonly _aggregateType;
12
15
  protected readonly _version: number;
13
16
  protected events: AnyEvent[];
14
17
  constructor(metadata: IAggregateMetadata, props?: P);
15
18
  static aggregateType(): string;
16
- static newAggregate<T extends AnyAggregate>(this: AggregateClass<T>, props?: PropsOf<T>): T;
19
+ static newAggregate<T extends AnyAggregate>(this: AggregateClass<T>, props?: PropsOf<T>, metadata?: NewAggregateMetadata): T;
17
20
  get version(): number;
18
- aggregateType(): string;
21
+ get aggregateType(): string;
19
22
  getEvents(): AnyEvent[];
20
23
  hasEvents(): boolean;
21
24
  nextVersion(): number;
22
25
  protected newEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>, metadata?: NewEventMetadataOptions): E;
23
26
  protected addEvent<E extends AnyEvent>(event: E): void;
27
+ protected addNewEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>, metadata?: NewEventMetadataOptions): void;
24
28
  }
25
29
  export interface SnapshotWithProps<P extends object> {
26
30
  metadata: IAggregateMetadata;
@@ -40,10 +44,13 @@ export declare class AggregateES<P extends object> extends Aggregate<P> {
40
44
  nextVersion(): number;
41
45
  private addPastEvent;
42
46
  private validateEventBeforeApply;
43
- getEventApplier(eventType: string): AggregateEventApplier<AnyEvent> | null;
47
+ getEventApplierSafe(eventType: string): AggregateEventApplier<AnyEvent> | null;
48
+ getEventApplier(eventType: string): AggregateEventApplier<AnyEvent>;
44
49
  applyEvent<E extends AnyEvent>(event: E, fromHistory?: boolean): void;
45
50
  applyEvents(events: AnyEvent[], fromHistory?: boolean): void;
46
- getCommandHandler(commandType: string): AggregateCommandHandler<AnyCommand> | null;
51
+ getCommandHandlerSafe(commandType: string): AggregateCommandHandler<AnyCommand> | null;
52
+ getCommandHandler(commandType: string): AggregateCommandHandler<AnyCommand>;
53
+ private validateCommandBeforeProcess;
47
54
  processCommand<C extends AnyCommand>(command: C): AnyEvent[];
48
55
  snap(): SnapshotWithProps<P>;
49
56
  }
@@ -58,4 +65,4 @@ export type AggregateConstructorParams<T extends AnyAggregate> = AggregateConstr
58
65
  export type AggregateClass<T extends AnyAggregate> = Class<T, AggregateConstructorParams<T>> & ClassStatic<typeof Aggregate<PropsOf<T>>>;
59
66
  export type AggregateESClass<T extends AnyAggregateES> = Class<T, AggregateConstructorParams<T>> & ClassStatic<typeof AggregateES<PropsOf<T>>>;
60
67
  export type InferAggregateClass<T extends AnyAggregate> = T extends AnyAggregateES ? AggregateESClass<T> : AggregateClass<T>;
61
- export type AnyAggregateClass = AggregateClass<AnyAggregate>;
68
+ export type AnyAggregateClass = Class<AnyAggregate>;
@@ -10,23 +10,20 @@ class Aggregate extends entity_1.Entity {
10
10
  constructor(metadata, props) {
11
11
  super({ id: metadata.id }, props);
12
12
  this.events = [];
13
+ this._aggregateType = metadata.aggregateType;
13
14
  this._version = metadata.version;
14
15
  }
15
16
  static aggregateType() {
16
17
  return (0, aggregate_1.getAggregateType)(this.prototype);
17
18
  }
18
- static newAggregate(props) {
19
- return new this({
20
- id: id_1.Id.unique(),
21
- version: 0,
22
- }, props);
19
+ static newAggregate(props, metadata) {
20
+ return new this(Object.assign({ aggregateType: this.aggregateType(), id: id_1.Id.unique(), version: 0 }, metadata), props);
23
21
  }
24
22
  get version() {
25
23
  return this._version;
26
24
  }
27
- aggregateType() {
28
- const prototype = Object.getPrototypeOf(this);
29
- return (0, aggregate_1.getAggregateType)(prototype);
25
+ get aggregateType() {
26
+ return this._aggregateType;
30
27
  }
31
28
  getEvents() {
32
29
  return this.events;
@@ -40,13 +37,16 @@ class Aggregate extends entity_1.Entity {
40
37
  newEvent(eventClass, props, metadata) {
41
38
  return eventClass.newEvent({
42
39
  id: this.id,
43
- type: this.aggregateType(),
44
40
  version: this.nextVersion(),
45
41
  }, props, metadata);
46
42
  }
47
43
  addEvent(event) {
48
44
  this.events.push(event);
49
45
  }
46
+ addNewEvent(eventClass, props, metadata) {
47
+ const event = this.newEvent(eventClass, props, metadata);
48
+ this.addEvent(event);
49
+ }
50
50
  }
51
51
  exports.Aggregate = Aggregate;
52
52
  class AggregateES extends Aggregate {
@@ -56,7 +56,7 @@ class AggregateES extends Aggregate {
56
56
  this.pastEvents = [];
57
57
  }
58
58
  static stream(id, events) {
59
- const aggregate = new this({ id, version: 0 });
59
+ const aggregate = this.newAggregate(undefined, { id });
60
60
  aggregate.applyEvents(events, true);
61
61
  return aggregate;
62
62
  }
@@ -90,26 +90,31 @@ class AggregateES extends Aggregate {
90
90
  this.pastEvents.push(event);
91
91
  }
92
92
  validateEventBeforeApply(event) {
93
- const { type, id, version } = event.aggregate;
94
- if (type !== this.aggregateType())
93
+ const { aggregateType } = event;
94
+ const { id, version } = event.aggregate;
95
+ if (aggregateType !== this.aggregateType)
95
96
  throw new aggregate_error_1.InvalidEventAggregateTypeError();
96
- if (id !== this.id)
97
+ if (!id.equals(this.id))
97
98
  throw new aggregate_error_1.InvalidEventAggregateIdError();
98
99
  if (version !== this.nextVersion())
99
100
  throw new aggregate_error_1.InvalidEventAggregateVersionError();
100
101
  }
101
- getEventApplier(eventType) {
102
+ getEventApplierSafe(eventType) {
102
103
  const prototype = Object.getPrototypeOf(this);
103
104
  const eventApplier = (0, aggregate_1.getAggregateEventApplier)(prototype, eventType);
104
105
  if (eventApplier)
105
106
  return eventApplier.bind(this);
106
107
  return null;
107
108
  }
108
- applyEvent(event, fromHistory = false) {
109
- const eventType = event.eventType();
110
- const applier = this.getEventApplier(eventType);
109
+ getEventApplier(eventType) {
110
+ const applier = this.getEventApplierSafe(eventType);
111
111
  if (!applier)
112
112
  throw new aggregate_error_1.EventApplierNotFoundError(eventType);
113
+ return applier;
114
+ }
115
+ applyEvent(event, fromHistory = false) {
116
+ const { eventType } = event;
117
+ const applier = this.getEventApplier(eventType);
113
118
  this.validateEventBeforeApply(event);
114
119
  if (fromHistory)
115
120
  this.addPastEvent(event);
@@ -122,18 +127,28 @@ class AggregateES extends Aggregate {
122
127
  this.applyEvent(event, fromHistory);
123
128
  });
124
129
  }
125
- getCommandHandler(commandType) {
130
+ getCommandHandlerSafe(commandType) {
126
131
  const prototype = Object.getPrototypeOf(this);
127
132
  const commandHandler = (0, aggregate_1.getAggregateCommandHandler)(prototype, commandType);
128
133
  if (commandHandler)
129
134
  return commandHandler.bind(this);
130
135
  return null;
131
136
  }
132
- processCommand(command) {
133
- const commandType = command.commandType();
134
- const handler = this.getCommandHandler(commandType);
137
+ getCommandHandler(commandType) {
138
+ const handler = this.getCommandHandlerSafe(commandType);
135
139
  if (!handler)
136
140
  throw new aggregate_error_1.CommandHandlerNotFoundError(commandType);
141
+ return handler;
142
+ }
143
+ validateCommandBeforeProcess(command) {
144
+ const { aggregateType } = command;
145
+ if (aggregateType !== this.aggregateType)
146
+ throw new aggregate_error_1.InvalidCommandAggregateTypeError();
147
+ }
148
+ processCommand(command) {
149
+ const { commandType } = command;
150
+ const handler = this.getCommandHandler(commandType);
151
+ this.validateCommandBeforeProcess(command);
137
152
  const events = (0, to_array_1.toArray)(handler(command));
138
153
  const { correlationId, causationId } = command;
139
154
  events.forEach((event) => {
@@ -150,6 +165,7 @@ class AggregateES extends Aggregate {
150
165
  return {
151
166
  metadata: {
152
167
  id: this.id,
168
+ aggregateType: this.aggregateType,
153
169
  version: this.version,
154
170
  },
155
171
  props: this.getProps(),
@@ -1,33 +1,21 @@
1
1
  import { Class } from "../types/class";
2
2
  import { ClassStatic } from "../types/class-static";
3
- import { Id } from "./id";
4
- import { PropsEnvelope, PropsOf } from "./props-envelope";
5
- export interface ICommandMetadata {
6
- readonly id: Id;
7
- readonly timestamp: number;
8
- correlationId?: string;
9
- causationId?: string;
3
+ import { IMessageMetadata, Message } from "./message";
4
+ import { PropsOf } from "./props-envelope";
5
+ export interface ICommandMetadata extends IMessageMetadata {
6
+ readonly commandType: string;
10
7
  }
11
- export type NewCommandMetadataOptions = Partial<Omit<ICommandMetadata, "timestamp">>;
12
- export declare class Command<P extends object> extends PropsEnvelope<P> implements ICommandMetadata {
13
- private readonly _id;
14
- private readonly _timestamp;
15
- private _correlationId?;
16
- private _causationId?;
8
+ export type NewCommandMetadataOptions = Omit<ICommandMetadata, "aggregateType" | "timestamp">;
9
+ export declare class Command<P extends object> extends Message<P> implements ICommandMetadata {
10
+ private readonly _commandType;
17
11
  constructor(metadata: ICommandMetadata, props: P);
18
12
  static commandType(): string;
19
13
  static newCommand<T extends AnyCommand>(this: CommandClass<T>, props: PropsOf<T>, metadata?: NewCommandMetadataOptions): T;
20
- get id(): Id;
21
- get timestamp(): number;
22
- get correlationId(): string | undefined;
23
- get causationId(): string | undefined;
24
- setCorrelationId(correlationId: string): void;
25
- setCausationId(causationId: string): void;
26
- commandType(): string;
14
+ get commandType(): string;
27
15
  }
28
16
  export type AnyCommand = Command<any>;
29
17
  export type CommandConstructorParamsWithProps<P extends object> = ConstructorParameters<typeof Command<P>>;
30
18
  export type CommandClassWithProps<P extends object> = Class<Command<P>, CommandConstructorParamsWithProps<P>> & ClassStatic<typeof Command<P>>;
31
19
  export type CommandConstructorParams<T extends AnyCommand> = CommandConstructorParamsWithProps<PropsOf<T>>;
32
20
  export type CommandClass<T extends AnyCommand> = Class<T, CommandConstructorParams<T>> & ClassStatic<typeof Command<PropsOf<T>>>;
33
- export type AnyCommandClass = CommandClass<AnyCommand>;
21
+ export type AnyCommandClass = Class<AnyCommand>;
@@ -3,44 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Command = void 0;
4
4
  const command_1 = require("../metadata/command");
5
5
  const id_1 = require("./id");
6
- const props_envelope_1 = require("./props-envelope");
7
- class Command extends props_envelope_1.PropsEnvelope {
6
+ const message_1 = require("./message");
7
+ class Command extends message_1.Message {
8
8
  constructor(metadata, props) {
9
- super(props);
10
- this._id = metadata.id;
11
- this._timestamp = metadata.timestamp;
12
- this._correlationId = metadata === null || metadata === void 0 ? void 0 : metadata.correlationId;
13
- this._causationId = metadata === null || metadata === void 0 ? void 0 : metadata.causationId;
9
+ super(metadata, props);
10
+ this._commandType = metadata.commandType;
14
11
  }
15
12
  static commandType() {
16
13
  return (0, command_1.getCommandType)(this.prototype);
17
14
  }
18
15
  static newCommand(props, metadata) {
19
- return new this(Object.assign({ id: id_1.Id.unique(), timestamp: Date.now() }, metadata), props);
16
+ return new this(Object.assign({ commandType: this.commandType(), id: id_1.Id.unique(), timestamp: Date.now(), aggregateType: this.aggregateType() }, metadata), props);
20
17
  }
21
- get id() {
22
- return this._id;
23
- }
24
- get timestamp() {
25
- return this._timestamp;
26
- }
27
- get correlationId() {
28
- return this._correlationId;
29
- }
30
- get causationId() {
31
- return this._causationId;
32
- }
33
- setCorrelationId(correlationId) {
34
- if (!this.correlationId)
35
- this._correlationId = correlationId;
36
- }
37
- setCausationId(causationId) {
38
- if (!this.causationId)
39
- this._causationId = causationId;
40
- }
41
- commandType() {
42
- const prototype = Object.getPrototypeOf(this);
43
- return (0, command_1.getCommandType)(prototype);
18
+ get commandType() {
19
+ return this._commandType;
44
20
  }
45
21
  }
46
22
  exports.Command = Command;
@@ -20,4 +20,4 @@ export type EntityConstructorParamsWithProps<P extends object> = ConstructorPara
20
20
  export type EntityClassWithProps<P extends object> = Class<Entity<P>, EntityConstructorParamsWithProps<P>> & ClassStatic<typeof Entity<P>>;
21
21
  export type EntityConstructorParams<T extends AnyEntity> = EntityConstructorParamsWithProps<PropsOf<T>>;
22
22
  export type EntityClass<T extends AnyEntity> = Class<T, EntityConstructorParams<T>> & ClassStatic<typeof Entity<PropsOf<T>>>;
23
- export type AnyEntityClass = EntityClass<AnyEntity>;
23
+ export type AnyEntityClass = Class<AnyEntity>;
@@ -16,3 +16,6 @@ export declare class InvalidEventAggregateIdError extends Error {
16
16
  export declare class InvalidEventAggregateVersionError extends Error {
17
17
  constructor();
18
18
  }
19
+ export declare class InvalidCommandAggregateTypeError extends Error {
20
+ constructor();
21
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidEventAggregateVersionError = exports.InvalidEventAggregateIdError = exports.InvalidEventAggregateTypeError = exports.CommandHandlerNotFoundError = exports.EventApplierNotFoundError = exports.PastEventCannotBeAddedError = void 0;
3
+ exports.InvalidCommandAggregateTypeError = exports.InvalidEventAggregateVersionError = exports.InvalidEventAggregateIdError = exports.InvalidEventAggregateTypeError = exports.CommandHandlerNotFoundError = exports.EventApplierNotFoundError = exports.PastEventCannotBeAddedError = void 0;
4
4
  class PastEventCannotBeAddedError extends Error {
5
5
  constructor() {
6
6
  super("Past event cannot be added once a new event has been added");
@@ -21,19 +21,25 @@ class CommandHandlerNotFoundError extends Error {
21
21
  exports.CommandHandlerNotFoundError = CommandHandlerNotFoundError;
22
22
  class InvalidEventAggregateTypeError extends Error {
23
23
  constructor() {
24
- super("The event must have the aggregateType is equal to the type of the aggregate that applied that event");
24
+ super("The event must have the aggregate type equal to the type of the aggregate instance");
25
25
  }
26
26
  }
27
27
  exports.InvalidEventAggregateTypeError = InvalidEventAggregateTypeError;
28
28
  class InvalidEventAggregateIdError extends Error {
29
29
  constructor() {
30
- super("The event must have the aggregateId is equal to the id of the aggregate that applied that event");
30
+ super("The event must have the aggregate id equal to the id of the aggregate instance");
31
31
  }
32
32
  }
33
33
  exports.InvalidEventAggregateIdError = InvalidEventAggregateIdError;
34
34
  class InvalidEventAggregateVersionError extends Error {
35
35
  constructor() {
36
- super("The event must have the aggregateVersion is equal to the next version of the aggregate that applied that event");
36
+ super("The event must have the aggregate version equal to the next version of the aggregate instance");
37
37
  }
38
38
  }
39
39
  exports.InvalidEventAggregateVersionError = InvalidEventAggregateVersionError;
40
+ class InvalidCommandAggregateTypeError extends Error {
41
+ constructor() {
42
+ super("The command must have the aggregate type equal to the type of the aggregate instance");
43
+ }
44
+ }
45
+ exports.InvalidCommandAggregateTypeError = InvalidCommandAggregateTypeError;
@@ -1,41 +1,29 @@
1
1
  import { Class } from "../types/class";
2
2
  import { ClassStatic } from "../types/class-static";
3
3
  import { Id } from "./id";
4
- import { PropsEnvelope, PropsOf } from "./props-envelope";
4
+ import { IMessageMetadata, Message } from "./message";
5
+ import { PropsOf } from "./props-envelope";
5
6
  export interface IEventAggregate {
6
7
  readonly id: Id;
7
- readonly type: string;
8
8
  readonly version: number;
9
9
  }
10
- export interface IEventMetadata {
11
- readonly id: Id;
12
- readonly timestamp: number;
10
+ export interface IEventMetadata extends IMessageMetadata {
11
+ readonly eventType: string;
13
12
  readonly aggregate: IEventAggregate;
14
- correlationId?: string;
15
- causationId?: string;
16
13
  }
17
- export type NewEventMetadataOptions = Partial<Omit<IEventMetadata, "aggregate" | "timestamp">>;
18
- export declare class Event<P extends object> extends PropsEnvelope<P> implements IEventMetadata {
19
- private readonly _id;
20
- private readonly _timestamp;
14
+ export type NewEventMetadataOptions = Omit<IEventMetadata, "aggregateType" | "aggregate" | "timestamp">;
15
+ export declare class Event<P extends object> extends Message<P> implements IEventMetadata {
16
+ private readonly _eventType;
21
17
  private readonly _aggregate;
22
- private _correlationId?;
23
- private _causationId?;
24
18
  constructor(metadata: IEventMetadata, props: P);
25
19
  static eventType(): string;
26
20
  static newEvent<T extends AnyEvent>(this: EventClass<T>, aggregate: IEventAggregate, props: PropsOf<T>, metadata?: NewEventMetadataOptions): T;
27
- get id(): Id;
28
- get timestamp(): number;
21
+ get eventType(): string;
29
22
  get aggregate(): IEventAggregate;
30
- get correlationId(): string | undefined;
31
- get causationId(): string | undefined;
32
- setCorrelationId(correlationId: string): void;
33
- setCausationId(causationId: string): void;
34
- eventType(): string;
35
23
  }
36
24
  export type AnyEvent = Event<any>;
37
25
  export type EventConstructorParamsWithProps<P extends object> = ConstructorParameters<typeof Event<P>>;
38
26
  export type EventClassWithProps<P extends object> = Class<Event<P>, EventConstructorParamsWithProps<P>> & ClassStatic<typeof Event<P>>;
39
27
  export type EventConstructorParams<T extends AnyEvent> = EventConstructorParamsWithProps<PropsOf<T>>;
40
28
  export type EventClass<T extends AnyEvent> = Class<T, EventConstructorParams<T>> & ClassStatic<typeof Event<PropsOf<T>>>;
41
- export type AnyEventClass = EventClass<AnyEvent>;
29
+ export type AnyEventClass = Class<AnyEvent>;
@@ -3,48 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Event = void 0;
4
4
  const event_1 = require("../metadata/event");
5
5
  const id_1 = require("./id");
6
- const props_envelope_1 = require("./props-envelope");
7
- class Event extends props_envelope_1.PropsEnvelope {
6
+ const message_1 = require("./message");
7
+ class Event extends message_1.Message {
8
8
  constructor(metadata, props) {
9
- super(props);
10
- this._id = metadata.id;
11
- this._timestamp = metadata.timestamp;
9
+ super(metadata, props);
10
+ this._eventType = metadata.eventType;
12
11
  this._aggregate = metadata.aggregate;
13
- this._correlationId = metadata === null || metadata === void 0 ? void 0 : metadata.correlationId;
14
- this._causationId = metadata === null || metadata === void 0 ? void 0 : metadata.causationId;
15
12
  }
16
13
  static eventType() {
17
14
  return (0, event_1.getEventType)(this.prototype);
18
15
  }
19
16
  static newEvent(aggregate, props, metadata) {
20
- return new this(Object.assign({ id: id_1.Id.unique(), timestamp: Date.now(), aggregate }, metadata), props);
17
+ return new this(Object.assign({ eventType: this.eventType(), id: id_1.Id.unique(), timestamp: Date.now(), aggregateType: this.aggregateType(), aggregate }, metadata), props);
21
18
  }
22
- get id() {
23
- return this._id;
24
- }
25
- get timestamp() {
26
- return this._timestamp;
19
+ get eventType() {
20
+ return this._eventType;
27
21
  }
28
22
  get aggregate() {
29
23
  return this._aggregate;
30
24
  }
31
- get correlationId() {
32
- return this._correlationId;
33
- }
34
- get causationId() {
35
- return this._causationId;
36
- }
37
- setCorrelationId(correlationId) {
38
- if (!this.correlationId)
39
- this._correlationId = correlationId;
40
- }
41
- setCausationId(causationId) {
42
- if (!this.causationId)
43
- this._causationId = causationId;
44
- }
45
- eventType() {
46
- const prototype = Object.getPrototypeOf(this);
47
- return (0, event_1.getEventType)(prototype);
48
- }
49
25
  }
50
26
  exports.Event = Event;
@@ -1,4 +1,5 @@
1
1
  export * from "./aggregate";
2
+ export * from "./message";
2
3
  export * from "./command";
3
4
  export * from "./entity";
4
5
  export * from "./event";
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./aggregate"), exports);
18
+ __exportStar(require("./message"), exports);
18
19
  __exportStar(require("./command"), exports);
19
20
  __exportStar(require("./entity"), exports);
20
21
  __exportStar(require("./event"), exports);
@@ -0,0 +1,33 @@
1
+ import { Class } from "../types/class";
2
+ import { ClassStatic } from "../types/class-static";
3
+ import { Id } from "./id";
4
+ import { PropsEnvelope, PropsOf } from "./props-envelope";
5
+ export interface IMessageMetadata {
6
+ readonly id: Id;
7
+ readonly timestamp: number;
8
+ readonly aggregateType: string;
9
+ correlationId?: string;
10
+ causationId?: string;
11
+ }
12
+ export declare class Message<P extends object> extends PropsEnvelope<P> implements IMessageMetadata {
13
+ private readonly _id;
14
+ private readonly _timestamp;
15
+ private readonly _aggregateType;
16
+ private _correlationId?;
17
+ private _causationId?;
18
+ constructor(metadata: IMessageMetadata, props: P);
19
+ static aggregateType(): string;
20
+ get id(): Id;
21
+ get timestamp(): number;
22
+ get aggregateType(): string;
23
+ get correlationId(): string | undefined;
24
+ get causationId(): string | undefined;
25
+ setCorrelationId(correlationId: string): void;
26
+ setCausationId(causationId: string): void;
27
+ }
28
+ export type AnyMessage = Message<any>;
29
+ export type MessageConstructorParamsWithProps<P extends object> = ConstructorParameters<typeof Message<P>>;
30
+ export type MessageClassWithProps<P extends object> = Class<Message<P>, MessageConstructorParamsWithProps<P>> & ClassStatic<typeof Message<P>>;
31
+ export type MessageConstructorParams<T extends AnyMessage> = MessageConstructorParamsWithProps<PropsOf<T>>;
32
+ export type MessageClass<T extends AnyMessage> = Class<T, MessageConstructorParams<T>> & ClassStatic<typeof Message<PropsOf<T>>>;
33
+ export type AnyMessageClass = Class<AnyMessage>;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Message = void 0;
4
+ const message_1 = require("../metadata/message");
5
+ const props_envelope_1 = require("./props-envelope");
6
+ class Message extends props_envelope_1.PropsEnvelope {
7
+ constructor(metadata, props) {
8
+ super(props);
9
+ this._id = metadata.id;
10
+ this._timestamp = metadata.timestamp;
11
+ this._aggregateType = metadata.aggregateType;
12
+ this._correlationId = metadata === null || metadata === void 0 ? void 0 : metadata.correlationId;
13
+ this._causationId = metadata === null || metadata === void 0 ? void 0 : metadata.causationId;
14
+ }
15
+ static aggregateType() {
16
+ return (0, message_1.getMessageAggregateType)(this.prototype);
17
+ }
18
+ get id() {
19
+ return this._id;
20
+ }
21
+ get timestamp() {
22
+ return this._timestamp;
23
+ }
24
+ get aggregateType() {
25
+ return this._aggregateType;
26
+ }
27
+ get correlationId() {
28
+ return this._correlationId;
29
+ }
30
+ get causationId() {
31
+ return this._causationId;
32
+ }
33
+ setCorrelationId(correlationId) {
34
+ if (!this.correlationId)
35
+ this._correlationId = correlationId;
36
+ }
37
+ setCausationId(causationId) {
38
+ if (!this.causationId)
39
+ this._causationId = causationId;
40
+ }
41
+ }
42
+ exports.Message = Message;
@@ -13,4 +13,4 @@ export type ValueObjectConstructorParamsWithProps<P extends object> = Constructo
13
13
  export type ValueObjectClassWithProps<P extends object> = Class<ValueObject<P>, ValueObjectConstructorParamsWithProps<P>> & ClassStatic<typeof ValueObject<P>>;
14
14
  export type ValueObjectConstructorParams<T extends AnyValueObject> = ValueObjectConstructorParamsWithProps<PropsOf<T>>;
15
15
  export type ValueObjectClass<T extends AnyValueObject> = Class<T, ValueObjectConstructorParams<T>> & ClassStatic<typeof ValueObject<PropsOf<T>>>;
16
- export type AnyValueObjectClass = ValueObjectClass<AnyValueObject>;
16
+ export type AnyValueObjectClass = Class<AnyValueObject>;
@@ -1,2 +1,2 @@
1
1
  import { AnyCommand, CommandClass } from "../base/command";
2
- export declare const TypeCommand: <T extends AnyCommand>(commandType?: string) => <U extends CommandClass<T>>(target: U) => void;
2
+ export declare const TypeCommand: <T extends AnyCommand>(aggregateType: string, commandType?: string) => <U extends CommandClass<T>>(target: U) => void;
@@ -2,8 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypeCommand = void 0;
4
4
  const command_1 = require("../metadata/command");
5
- const TypeCommand = (commandType) => {
5
+ const message_1 = require("./message");
6
+ const TypeCommand = (aggregateType, commandType) => {
6
7
  return (target) => {
8
+ (0, message_1.TypeMessage)(aggregateType)(target);
7
9
  commandType = commandType !== null && commandType !== void 0 ? commandType : target.name;
8
10
  (0, command_1.defineCommandType)(target.prototype, commandType);
9
11
  command_1.CommandRegistry.register(commandType, target);
@@ -1,2 +1,2 @@
1
1
  import { AnyEvent, EventClass } from "../base/event";
2
- export declare const TypeEvent: <T extends AnyEvent>(eventType?: string) => <U extends EventClass<T>>(target: U) => void;
2
+ export declare const TypeEvent: <T extends AnyEvent>(aggregateType: string, eventType?: string) => <U extends EventClass<T>>(target: U) => void;
@@ -2,8 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypeEvent = void 0;
4
4
  const event_1 = require("../metadata/event");
5
- const TypeEvent = (eventType) => {
5
+ const message_1 = require("./message");
6
+ const TypeEvent = (aggregateType, eventType) => {
6
7
  return (target) => {
8
+ (0, message_1.TypeMessage)(aggregateType)(target);
7
9
  eventType = eventType !== null && eventType !== void 0 ? eventType : target.name;
8
10
  (0, event_1.defineEventType)(target.prototype, eventType !== null && eventType !== void 0 ? eventType : target.name);
9
11
  event_1.EventRegistry.register(eventType, target);
@@ -1,4 +1,5 @@
1
1
  export * from "./aggregate";
2
+ export * from "./message";
2
3
  export * from "./entity";
3
4
  export * from "./command";
4
5
  export * from "./event";
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./aggregate"), exports);
18
+ __exportStar(require("./message"), exports);
18
19
  __exportStar(require("./entity"), exports);
19
20
  __exportStar(require("./command"), exports);
20
21
  __exportStar(require("./event"), exports);
@@ -0,0 +1,2 @@
1
+ import { AnyMessageClass } from "../base/message";
2
+ export declare const TypeMessage: (aggregateType: string) => <U extends AnyMessageClass>(target: U) => void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypeMessage = void 0;
4
+ const message_1 = require("../metadata/message");
5
+ const TypeMessage = (aggregateType) => {
6
+ return (target) => {
7
+ (0, message_1.defineMessageAggregateType)(target.prototype, aggregateType);
8
+ };
9
+ };
10
+ exports.TypeMessage = TypeMessage;
@@ -3,5 +3,6 @@ export declare const ENTITY_TYPE = "__entity_type__";
3
3
  export declare const VALUE_OBJECT_TYPE = "__value_object_type__";
4
4
  export declare const EVENT_TYPE = "__event_type__";
5
5
  export declare const COMMAND_TYPE = "__command_type__";
6
+ export declare const MESSAGE_AGGREGATE_TYPE = "__message_aggregate_type__";
6
7
  export declare const AGGREGATE_EVENTS_APPLIERS = "__aggregate_events_appliers__";
7
8
  export declare const AGGREGATE_COMMANDS_HANDLERS = "__aggregate_commands_handlers__";
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AGGREGATE_COMMANDS_HANDLERS = exports.AGGREGATE_EVENTS_APPLIERS = exports.COMMAND_TYPE = exports.EVENT_TYPE = exports.VALUE_OBJECT_TYPE = exports.ENTITY_TYPE = exports.AGGREGATE_TYPE = void 0;
3
+ exports.AGGREGATE_COMMANDS_HANDLERS = exports.AGGREGATE_EVENTS_APPLIERS = exports.MESSAGE_AGGREGATE_TYPE = exports.COMMAND_TYPE = exports.EVENT_TYPE = exports.VALUE_OBJECT_TYPE = exports.ENTITY_TYPE = exports.AGGREGATE_TYPE = void 0;
4
4
  exports.AGGREGATE_TYPE = "__aggregate_type__";
5
5
  exports.ENTITY_TYPE = "__entity_type__";
6
6
  exports.VALUE_OBJECT_TYPE = "__value_object_type__";
7
7
  exports.EVENT_TYPE = "__event_type__";
8
8
  exports.COMMAND_TYPE = "__command_type__";
9
+ exports.MESSAGE_AGGREGATE_TYPE = "__message_aggregate_type__";
9
10
  exports.AGGREGATE_EVENTS_APPLIERS = "__aggregate_events_appliers__";
10
11
  exports.AGGREGATE_COMMANDS_HANDLERS = "__aggregate_commands_handlers__";
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getEventType = exports.defineEventType = exports.EventRegistry = void 0;
4
4
  require("reflect-metadata");
5
5
  const constants_1 = require("./constants");
6
- const registry_1 = require("./registry");
7
6
  const errors_1 = require("./errors");
7
+ const registry_1 = require("./registry");
8
8
  exports.EventRegistry = new registry_1.Registry();
9
9
  const defineEventType = (target, eventType) => {
10
10
  Reflect.defineMetadata(constants_1.EVENT_TYPE, eventType, target);
@@ -1,5 +1,6 @@
1
1
  export * from "./constants";
2
2
  export * from "./aggregate";
3
+ export * from "./message";
3
4
  export * from "./command";
4
5
  export * from "./event";
5
6
  export * from "./entity";
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./constants"), exports);
18
18
  __exportStar(require("./aggregate"), exports);
19
+ __exportStar(require("./message"), exports);
19
20
  __exportStar(require("./command"), exports);
20
21
  __exportStar(require("./event"), exports);
21
22
  __exportStar(require("./entity"), exports);
@@ -0,0 +1,3 @@
1
+ import "reflect-metadata";
2
+ export declare const defineMessageAggregateType: (target: object, aggregateType: string) => void;
3
+ export declare const getMessageAggregateType: (target: object) => string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMessageAggregateType = exports.defineMessageAggregateType = void 0;
4
+ require("reflect-metadata");
5
+ const constants_1 = require("./constants");
6
+ const defineMessageAggregateType = (target, aggregateType) => {
7
+ Reflect.defineMetadata(constants_1.MESSAGE_AGGREGATE_TYPE, aggregateType, target);
8
+ };
9
+ exports.defineMessageAggregateType = defineMessageAggregateType;
10
+ const getMessageAggregateType = (target) => {
11
+ const aggregateType = Reflect.getMetadata(constants_1.MESSAGE_AGGREGATE_TYPE, target);
12
+ if (!aggregateType)
13
+ throw new Error();
14
+ return aggregateType;
15
+ };
16
+ exports.getMessageAggregateType = getMessageAggregateType;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "ddd-node",
3
- "version": "4.0.1",
3
+ "version": "5.0.0",
4
4
  "description": "Domain Driven Design base for NodeJs",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "build": "rm -rf dist && tsc --project tsconfig.build.json && tsc-alias -p tsconfig.build.json",
8
- "example": "ts-node -r tsconfig-paths/register test/example/index.ts"
8
+ "example": "ts-node -r tsconfig-paths/register test/example/main.ts"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",