ddd-node 9.0.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.
@@ -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) {
@@ -27,7 +26,7 @@ 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
  }
@@ -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
  }
@@ -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,14 +1,15 @@
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
+ export declare class Entity<Props extends object> extends ModelWithId<Props> {
9
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
+ static newEntity<T extends AnyEntity>(this: EntityClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
12
13
  getId(): Id;
13
14
  hasId(id: Id): boolean;
14
15
  }
@@ -1,16 +1,15 @@
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
15
  getId() {
@@ -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,7 +9,7 @@ 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,
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,7 +9,7 @@ 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?;
@@ -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);
@@ -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;
@@ -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": "9.0.0",
3
+ "version": "9.1.0",
4
4
  "description": "Domain Driven Design base for NodeJs",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {