ddd-node 3.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.
- package/dist/base/aggregate.d.ts +15 -10
- package/dist/base/aggregate.js +32 -29
- package/package.json +1 -1
package/dist/base/aggregate.d.ts
CHANGED
|
@@ -2,39 +2,42 @@ import { Class } from "../types/class";
|
|
|
2
2
|
import { ClassStatic } from "../types/class-static";
|
|
3
3
|
import { AnyCommand } from "./command";
|
|
4
4
|
import { Entity, IEntityMetadata } from "./entity";
|
|
5
|
-
import { AnyEvent, EventClass,
|
|
5
|
+
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
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
|
|
17
|
-
static newAggregate<T extends AnyAggregate>(this: AggregateClass<T>, props?: PropsOf<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[];
|
|
22
20
|
hasEvents(): boolean;
|
|
23
21
|
nextVersion(): number;
|
|
24
|
-
nextEventAggregate(): IEventAggregate;
|
|
25
22
|
protected newEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>, metadata?: NewEventMetadataOptions): E;
|
|
26
23
|
protected addEvent<E extends AnyEvent>(event: E): void;
|
|
27
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
|
+
}
|
|
28
31
|
export declare class AggregateES<P extends object> extends Aggregate<P> {
|
|
29
32
|
protected handledCommands: AnyCommand[];
|
|
30
33
|
protected pastEvents: AnyEvent[];
|
|
31
|
-
static
|
|
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;
|
|
32
36
|
getHandledCommands(): AnyCommand[];
|
|
33
37
|
getPastEvents(): AnyEvent[];
|
|
34
38
|
lastEvent(): AnyEvent | undefined;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
nextEventAggregate(): IEventAggregate;
|
|
39
|
+
currentVersion(): number;
|
|
40
|
+
nextVersion(): number;
|
|
38
41
|
private addPastEvent;
|
|
39
42
|
private validateEventBeforeApply;
|
|
40
43
|
getEventApplier(eventType: string): AggregateEventApplier<AnyEvent> | null;
|
|
@@ -42,6 +45,7 @@ export declare class AggregateES<P extends object> extends Aggregate<P> {
|
|
|
42
45
|
applyEvents(events: AnyEvent[], fromHistory?: boolean): void;
|
|
43
46
|
getCommandHandler(commandType: string): AggregateCommandHandler<AnyCommand> | null;
|
|
44
47
|
processCommand<C extends AnyCommand>(command: C): AnyEvent[];
|
|
48
|
+
snap(): SnapshotWithProps<P>;
|
|
45
49
|
}
|
|
46
50
|
export type AnyAggregate = Aggregate<any>;
|
|
47
51
|
export type AnyAggregateES = AggregateES<any>;
|
|
@@ -53,4 +57,5 @@ export type AggregateESClassWithProps<P extends object> = Class<AggregateES<P>,
|
|
|
53
57
|
export type AggregateConstructorParams<T extends AnyAggregate> = AggregateConstructorParamsWithProps<PropsOf<T>>;
|
|
54
58
|
export type AggregateClass<T extends AnyAggregate> = Class<T, AggregateConstructorParams<T>> & ClassStatic<typeof Aggregate<PropsOf<T>>>;
|
|
55
59
|
export type AggregateESClass<T extends AnyAggregateES> = Class<T, AggregateConstructorParams<T>> & ClassStatic<typeof AggregateES<PropsOf<T>>>;
|
|
60
|
+
export type InferAggregateClass<T extends AnyAggregate> = T extends AnyAggregateES ? AggregateESClass<T> : AggregateClass<T>;
|
|
56
61
|
export type AnyAggregateClass = AggregateClass<AnyAggregate>;
|
package/dist/base/aggregate.js
CHANGED
|
@@ -12,15 +12,14 @@ class Aggregate extends entity_1.Entity {
|
|
|
12
12
|
this.events = [];
|
|
13
13
|
this._version = metadata.version;
|
|
14
14
|
}
|
|
15
|
-
static
|
|
15
|
+
static aggregateType() {
|
|
16
16
|
return (0, aggregate_1.getAggregateType)(this.prototype);
|
|
17
17
|
}
|
|
18
|
-
static newAggregate(props
|
|
19
|
-
return new this(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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;
|
|
@@ -38,15 +37,12 @@ class Aggregate extends entity_1.Entity {
|
|
|
38
37
|
nextVersion() {
|
|
39
38
|
return this.version + 1;
|
|
40
39
|
}
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
type: this.aggregateType(),
|
|
40
|
+
newEvent(eventClass, props, metadata) {
|
|
41
|
+
return eventClass.newEvent({
|
|
44
42
|
id: this.id,
|
|
43
|
+
type: this.aggregateType(),
|
|
45
44
|
version: this.nextVersion(),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
newEvent(eventClass, props, metadata) {
|
|
49
|
-
return eventClass.newEvent(this.nextEventAggregate(), props, metadata);
|
|
45
|
+
}, props, metadata);
|
|
50
46
|
}
|
|
51
47
|
addEvent(event) {
|
|
52
48
|
this.events.push(event);
|
|
@@ -59,10 +55,15 @@ class AggregateES extends Aggregate {
|
|
|
59
55
|
this.handledCommands = [];
|
|
60
56
|
this.pastEvents = [];
|
|
61
57
|
}
|
|
62
|
-
static
|
|
63
|
-
const aggregate = new this({ id, version }
|
|
64
|
-
|
|
65
|
-
|
|
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);
|
|
66
67
|
return aggregate;
|
|
67
68
|
}
|
|
68
69
|
getHandledCommands() {
|
|
@@ -74,21 +75,14 @@ class AggregateES extends Aggregate {
|
|
|
74
75
|
lastEvent() {
|
|
75
76
|
return this.hasEvents() ? this.events.at(-1) : this.pastEvents.at(-1);
|
|
76
77
|
}
|
|
77
|
-
|
|
78
|
+
currentVersion() {
|
|
78
79
|
const lastEvent = this.lastEvent();
|
|
79
80
|
if (lastEvent)
|
|
80
81
|
return lastEvent.aggregate.version;
|
|
81
82
|
return this.version;
|
|
82
83
|
}
|
|
83
|
-
|
|
84
|
-
return this.
|
|
85
|
-
}
|
|
86
|
-
nextEventAggregate() {
|
|
87
|
-
return {
|
|
88
|
-
type: this.aggregateType(),
|
|
89
|
-
id: this.id,
|
|
90
|
-
version: this.nextEventVersion(),
|
|
91
|
-
};
|
|
84
|
+
nextVersion() {
|
|
85
|
+
return this.currentVersion() + 1;
|
|
92
86
|
}
|
|
93
87
|
addPastEvent(event) {
|
|
94
88
|
if (this.hasEvents())
|
|
@@ -101,7 +95,7 @@ class AggregateES extends Aggregate {
|
|
|
101
95
|
throw new aggregate_error_1.InvalidEventAggregateTypeError();
|
|
102
96
|
if (id !== this.id)
|
|
103
97
|
throw new aggregate_error_1.InvalidEventAggregateIdError();
|
|
104
|
-
if (version !== this.
|
|
98
|
+
if (version !== this.nextVersion())
|
|
105
99
|
throw new aggregate_error_1.InvalidEventAggregateVersionError();
|
|
106
100
|
}
|
|
107
101
|
getEventApplier(eventType) {
|
|
@@ -152,5 +146,14 @@ class AggregateES extends Aggregate {
|
|
|
152
146
|
this.handledCommands.push(command);
|
|
153
147
|
return events;
|
|
154
148
|
}
|
|
149
|
+
snap() {
|
|
150
|
+
return {
|
|
151
|
+
metadata: {
|
|
152
|
+
id: this.id,
|
|
153
|
+
version: this.version,
|
|
154
|
+
},
|
|
155
|
+
props: this.getProps(),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
155
158
|
}
|
|
156
159
|
exports.AggregateES = AggregateES;
|