ddd-node 4.0.0 → 4.0.1

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.
@@ -8,14 +8,12 @@ import { PropsOf } from "./props-envelope";
8
8
  export interface IAggregateMetadata extends IEntityMetadata {
9
9
  readonly version: number;
10
10
  }
11
- export type NewAggregateMetadataOptions = Partial<Omit<IAggregateMetadata, "version">>;
12
11
  export declare class Aggregate<P extends object> extends Entity<P> implements IAggregateMetadata {
13
12
  protected readonly _version: number;
14
13
  protected events: AnyEvent[];
15
14
  constructor(metadata: IAggregateMetadata, props?: P);
16
- static agggregateType(): string;
17
- static newAggregate<T extends AnyAggregate>(this: AggregateClass<T>, props?: PropsOf<T>, metadata?: NewAggregateMetadataOptions): T;
18
- static loadAggregate<T extends AnyAggregate>(this: AggregateClass<T>, id: Id, version: number, props: PropsOf<T>): T;
15
+ static aggregateType(): string;
16
+ static newAggregate<T extends AnyAggregate>(this: AggregateClass<T>, props?: PropsOf<T>): T;
19
17
  get version(): number;
20
18
  aggregateType(): string;
21
19
  getEvents(): AnyEvent[];
@@ -24,10 +22,17 @@ export declare class Aggregate<P extends object> extends Entity<P> implements IA
24
22
  protected newEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>, metadata?: NewEventMetadataOptions): E;
25
23
  protected addEvent<E extends AnyEvent>(event: E): void;
26
24
  }
25
+ export interface SnapshotWithProps<P extends object> {
26
+ metadata: IAggregateMetadata;
27
+ props: P;
28
+ }
29
+ export interface Snapshot<T extends AnyAggregateES> extends SnapshotWithProps<PropsOf<T>> {
30
+ }
27
31
  export declare class AggregateES<P extends object> extends Aggregate<P> {
28
32
  protected handledCommands: AnyCommand[];
29
33
  protected pastEvents: AnyEvent[];
30
- static loadAggregate<T extends AnyAggregateES>(this: AggregateESClass<T>, id: Id, version: number, props: PropsOf<T>, pastEvents?: AnyEvent[]): T;
34
+ static stream<T extends AnyAggregateES>(this: AggregateESClass<T>, id: Id, events: AnyEvent[]): T;
35
+ static snapshot<T extends AnyAggregateES>(this: AggregateESClass<T>, snapshot: Snapshot<T>, eventsAfterSnapshot: AnyEvent[]): T;
31
36
  getHandledCommands(): AnyCommand[];
32
37
  getPastEvents(): AnyEvent[];
33
38
  lastEvent(): AnyEvent | undefined;
@@ -40,6 +45,7 @@ export declare class AggregateES<P extends object> extends Aggregate<P> {
40
45
  applyEvents(events: AnyEvent[], fromHistory?: boolean): void;
41
46
  getCommandHandler(commandType: string): AggregateCommandHandler<AnyCommand> | null;
42
47
  processCommand<C extends AnyCommand>(command: C): AnyEvent[];
48
+ snap(): SnapshotWithProps<P>;
43
49
  }
44
50
  export type AnyAggregate = Aggregate<any>;
45
51
  export type AnyAggregateES = AggregateES<any>;
@@ -12,15 +12,14 @@ class Aggregate extends entity_1.Entity {
12
12
  this.events = [];
13
13
  this._version = metadata.version;
14
14
  }
15
- static agggregateType() {
15
+ static aggregateType() {
16
16
  return (0, aggregate_1.getAggregateType)(this.prototype);
17
17
  }
18
- static newAggregate(props, metadata) {
19
- return new this(Object.assign({ id: id_1.Id.unique(), version: 0 }, metadata), props);
20
- }
21
- static loadAggregate(id, version, props) {
22
- const aggregate = new this({ id, version }, props);
23
- return aggregate;
18
+ static newAggregate(props) {
19
+ return new this({
20
+ id: id_1.Id.unique(),
21
+ version: 0,
22
+ }, props);
24
23
  }
25
24
  get version() {
26
25
  return this._version;
@@ -56,10 +55,15 @@ class AggregateES extends Aggregate {
56
55
  this.handledCommands = [];
57
56
  this.pastEvents = [];
58
57
  }
59
- static loadAggregate(id, version, props, pastEvents) {
60
- const aggregate = new this({ id, version }, props);
61
- if (pastEvents)
62
- aggregate.applyEvents(pastEvents, true);
58
+ static stream(id, events) {
59
+ const aggregate = new this({ id, version: 0 });
60
+ aggregate.applyEvents(events, true);
61
+ return aggregate;
62
+ }
63
+ static snapshot(snapshot, eventsAfterSnapshot) {
64
+ const { metadata, props } = snapshot;
65
+ const aggregate = new this(metadata, props);
66
+ aggregate.applyEvents(eventsAfterSnapshot, true);
63
67
  return aggregate;
64
68
  }
65
69
  getHandledCommands() {
@@ -142,5 +146,14 @@ class AggregateES extends Aggregate {
142
146
  this.handledCommands.push(command);
143
147
  return events;
144
148
  }
149
+ snap() {
150
+ return {
151
+ metadata: {
152
+ id: this.id,
153
+ version: this.version,
154
+ },
155
+ props: this.getProps(),
156
+ };
157
+ }
145
158
  }
146
159
  exports.AggregateES = AggregateES;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddd-node",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Domain Driven Design base for NodeJs",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {