ddd-node 5.0.1 → 6.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.
- package/dist/core/aggregate.d.ts +67 -0
- package/dist/core/aggregate.js +178 -0
- package/dist/core/command.d.ts +13 -0
- package/dist/core/command.js +18 -0
- package/dist/core/entity.d.ts +17 -0
- package/dist/core/entity.js +23 -0
- package/dist/core/event.d.ts +27 -0
- package/dist/core/event.js +23 -0
- package/dist/{base → core}/id.d.ts +2 -1
- package/dist/{base → core}/id.js +6 -0
- package/dist/{base → core}/index.d.ts +5 -3
- package/dist/{base → core}/index.js +5 -3
- package/dist/core/message.d.ts +21 -0
- package/dist/core/message.js +30 -0
- package/dist/core/metadata.d.ts +14 -0
- package/dist/core/metadata.js +43 -0
- package/dist/core/model-type.d.ts +86 -0
- package/dist/core/model-type.js +55 -0
- package/dist/core/model.d.ts +14 -0
- package/dist/{base/props-envelope.js → core/model.js} +17 -14
- package/dist/core/registry.d.ts +12 -0
- package/dist/core/registry.js +28 -0
- package/dist/core/value-object.d.ts +10 -0
- package/dist/{base → core}/value-object.js +3 -11
- package/dist/decorators/aggregate.d.ts +6 -7
- package/dist/decorators/aggregate.js +16 -19
- package/dist/decorators/command.d.ts +2 -2
- package/dist/decorators/command.js +7 -11
- package/dist/decorators/entity.d.ts +2 -2
- package/dist/decorators/entity.js +7 -9
- package/dist/decorators/event.d.ts +2 -2
- package/dist/decorators/event.js +7 -11
- package/dist/decorators/index.d.ts +2 -2
- package/dist/decorators/index.js +2 -2
- package/dist/decorators/model.d.ts +3 -0
- package/dist/decorators/model.js +12 -0
- package/dist/decorators/value-object.d.ts +2 -2
- package/dist/decorators/value-object.js +7 -9
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/types/class.d.ts +0 -5
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/package.json +1 -1
- package/tsconfig.json +1 -2
- package/dist/base/aggregate.d.ts +0 -70
- package/dist/base/aggregate.js +0 -175
- package/dist/base/command.d.ts +0 -21
- package/dist/base/command.js +0 -22
- package/dist/base/entity.d.ts +0 -25
- package/dist/base/entity.js +0 -31
- package/dist/base/errors/aggregate.error.d.ts +0 -21
- package/dist/base/errors/aggregate.error.js +0 -45
- package/dist/base/event.d.ts +0 -29
- package/dist/base/event.js +0 -26
- package/dist/base/message.d.ts +0 -33
- package/dist/base/message.js +0 -42
- package/dist/base/props-envelope.d.ts +0 -11
- package/dist/base/repository.d.ts +0 -6
- package/dist/base/repository.js +0 -2
- package/dist/base/value-object.d.ts +0 -16
- package/dist/decorators/message.d.ts +0 -2
- package/dist/decorators/message.js +0 -10
- package/dist/metadata/aggregate.d.ts +0 -14
- package/dist/metadata/aggregate.js +0 -70
- package/dist/metadata/command.d.ts +0 -6
- package/dist/metadata/command.js +0 -19
- package/dist/metadata/constants.d.ts +0 -8
- package/dist/metadata/constants.js +0 -11
- package/dist/metadata/entity.d.ts +0 -6
- package/dist/metadata/entity.js +0 -19
- package/dist/metadata/errors.d.ts +0 -18
- package/dist/metadata/errors.js +0 -39
- package/dist/metadata/event.d.ts +0 -6
- package/dist/metadata/event.js +0 -19
- package/dist/metadata/index.d.ts +0 -7
- package/dist/metadata/index.js +0 -23
- package/dist/metadata/message.d.ts +0 -3
- package/dist/metadata/message.js +0 -16
- package/dist/metadata/registry.d.ts +0 -9
- package/dist/metadata/registry.js +0 -23
- package/dist/metadata/value-object.d.ts +0 -6
- package/dist/metadata/value-object.js +0 -19
- package/dist/utils/id.d.ts +0 -1
- package/dist/utils/id.js +0 -8
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Class } from "../types/class";
|
|
2
|
+
import { ClassStatic } from "../types/class-static";
|
|
3
|
+
import { AnyCommand } from "./command";
|
|
4
|
+
import { Entity, EntityMetadata } from "./entity";
|
|
5
|
+
import { AnyEvent, EventClass, EventClassWithTypedConstructor } from "./event";
|
|
6
|
+
import { Id } from "./id";
|
|
7
|
+
import { PropsOf } from "./model";
|
|
8
|
+
export interface AggregateBaseMetadata extends EntityMetadata {
|
|
9
|
+
version: number;
|
|
10
|
+
}
|
|
11
|
+
export declare abstract class AggregateBase<Props extends object> extends Entity<Props> {
|
|
12
|
+
protected readonly _version: number;
|
|
13
|
+
constructor(metadata: AggregateBaseMetadata, props?: Props);
|
|
14
|
+
abstract version: number;
|
|
15
|
+
protected newEvent<E extends AnyEvent>(eventClass: EventClassWithTypedConstructor<E>, props: PropsOf<E>): E;
|
|
16
|
+
}
|
|
17
|
+
export declare class Aggregate<Props extends object> extends AggregateBase<Props> {
|
|
18
|
+
private _events;
|
|
19
|
+
constructor(metadata: AggregateBaseMetadata, props: Props);
|
|
20
|
+
static newAggregate<T extends AnyAggregate>(this: AggregateClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
|
|
21
|
+
get version(): number;
|
|
22
|
+
get events(): AnyEvent[];
|
|
23
|
+
recordEvent<E extends AnyEvent>(event: E): void;
|
|
24
|
+
recordEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>): void;
|
|
25
|
+
clearEvents(): void;
|
|
26
|
+
}
|
|
27
|
+
export interface SnapshotMetadata extends AggregateBaseMetadata {
|
|
28
|
+
}
|
|
29
|
+
export interface Snapshot<T extends AnyAggregateES> {
|
|
30
|
+
metadata: SnapshotMetadata;
|
|
31
|
+
props: PropsOf<T>;
|
|
32
|
+
}
|
|
33
|
+
export declare class AggregateES<Props extends object> extends AggregateBase<Props> {
|
|
34
|
+
private _handledCommands;
|
|
35
|
+
private _pastEvents;
|
|
36
|
+
private _events;
|
|
37
|
+
constructor(metadata: AggregateBaseMetadata, props?: Props);
|
|
38
|
+
static newStream<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, id?: Id): T;
|
|
39
|
+
static fromStream<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, id: Id, events?: AnyEvent[]): T;
|
|
40
|
+
static fromSnapshot<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, snapshot: Snapshot<T>, events?: AnyEvent[]): T;
|
|
41
|
+
static eventAppliersMap<T extends AnyAggregateES>(this: AggregateESClass<T>): Map<string, EventApplier>;
|
|
42
|
+
static commandHandlersMap<T extends AnyAggregateES>(this: AggregateESClass<T>): Map<string, CommandHandler>;
|
|
43
|
+
get version(): number;
|
|
44
|
+
get events(): AnyEvent[];
|
|
45
|
+
get handledCommands(): AnyCommand[];
|
|
46
|
+
eventAppliersMap(): Map<string, EventApplier>;
|
|
47
|
+
hasEvent(): boolean;
|
|
48
|
+
getApplierForEvent<E extends AnyEvent>(event: E): EventApplier<E>;
|
|
49
|
+
private validateEventBeforeApply;
|
|
50
|
+
private _applyEvent;
|
|
51
|
+
private applyPastEvent;
|
|
52
|
+
private applyPastEvents;
|
|
53
|
+
applyEvent<E extends AnyEvent>(event: E): void;
|
|
54
|
+
applyEvents(events: AnyEvent[]): void;
|
|
55
|
+
commandHandlersMap(): Map<string, CommandHandler>;
|
|
56
|
+
getHandlerForCommand<C extends AnyCommand>(command: C): CommandHandler<C, AnyEvent>;
|
|
57
|
+
handleCommand<C extends AnyCommand>(command: C): AnyEvent[];
|
|
58
|
+
snap(): Snapshot<this>;
|
|
59
|
+
}
|
|
60
|
+
export type AnyAggregate = Aggregate<any>;
|
|
61
|
+
export type AggregateClass<T extends AnyAggregate = AnyAggregate, Arguments extends unknown[] = any[]> = Class<T, Arguments> & ClassStatic<typeof Aggregate<PropsOf<T>>>;
|
|
62
|
+
export type AggregateClassWithTypedConstructor<T extends AnyAggregate = AnyAggregate> = AggregateClass<T, ConstructorParameters<typeof Aggregate<PropsOf<T>>>>;
|
|
63
|
+
export type AnyAggregateES = AggregateES<any>;
|
|
64
|
+
export type AggregateESClass<T extends AnyAggregateES = AnyAggregateES, Arguments extends unknown[] = any[]> = Class<T, Arguments> & ClassStatic<typeof AggregateES<PropsOf<T>>>;
|
|
65
|
+
export type AggregateESClassWithTypedConstructor<T extends AnyAggregateES = AnyAggregateES> = AggregateESClass<T, ConstructorParameters<typeof AggregateES<PropsOf<T>>>>;
|
|
66
|
+
export type EventApplier<T extends AnyEvent = AnyEvent> = (event: T) => void;
|
|
67
|
+
export type CommandHandler<T extends AnyCommand = AnyCommand, U extends AnyEvent | AnyEvent[] = AnyEvent> = (command: T) => U;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AggregateES = exports.Aggregate = exports.AggregateBase = void 0;
|
|
4
|
+
const to_array_1 = require("../utils/to-array");
|
|
5
|
+
const entity_1 = require("./entity");
|
|
6
|
+
const id_1 = require("./id");
|
|
7
|
+
const metadata_1 = require("./metadata");
|
|
8
|
+
class AggregateBase extends entity_1.Entity {
|
|
9
|
+
constructor(metadata, props) {
|
|
10
|
+
super(metadata, props);
|
|
11
|
+
this._version = metadata.version;
|
|
12
|
+
}
|
|
13
|
+
newEvent(eventClass, props) {
|
|
14
|
+
const eventSource = {
|
|
15
|
+
type: this.type,
|
|
16
|
+
id: this.id,
|
|
17
|
+
version: this.version,
|
|
18
|
+
};
|
|
19
|
+
return eventClass.newEvent(eventSource, props);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.AggregateBase = AggregateBase;
|
|
23
|
+
class Aggregate extends AggregateBase {
|
|
24
|
+
constructor(metadata, props) {
|
|
25
|
+
super(metadata, props);
|
|
26
|
+
this._events = [];
|
|
27
|
+
}
|
|
28
|
+
static newAggregate(props, id) {
|
|
29
|
+
return new this({
|
|
30
|
+
id: id !== null && id !== void 0 ? id : id_1.Id.unique(),
|
|
31
|
+
version: 0,
|
|
32
|
+
}, props);
|
|
33
|
+
}
|
|
34
|
+
get version() {
|
|
35
|
+
return this._version;
|
|
36
|
+
}
|
|
37
|
+
get events() {
|
|
38
|
+
return this._events;
|
|
39
|
+
}
|
|
40
|
+
recordEvent(param1, param2) {
|
|
41
|
+
let event;
|
|
42
|
+
if (typeof param1 === "function" && param2) {
|
|
43
|
+
event = this.newEvent(param1, param2);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
event = param1;
|
|
47
|
+
}
|
|
48
|
+
this._events.push(event);
|
|
49
|
+
}
|
|
50
|
+
clearEvents() {
|
|
51
|
+
this._events = [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.Aggregate = Aggregate;
|
|
55
|
+
class AggregateES extends AggregateBase {
|
|
56
|
+
constructor(metadata, props) {
|
|
57
|
+
super(metadata, props);
|
|
58
|
+
this._handledCommands = [];
|
|
59
|
+
this._events = [];
|
|
60
|
+
this._pastEvents = [];
|
|
61
|
+
}
|
|
62
|
+
static newStream(id) {
|
|
63
|
+
return new this({
|
|
64
|
+
id: id !== null && id !== void 0 ? id : id_1.Id.unique(),
|
|
65
|
+
version: 0,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
static fromStream(id, events = []) {
|
|
69
|
+
const instance = this.newStream(id);
|
|
70
|
+
instance.applyPastEvents(events);
|
|
71
|
+
return instance;
|
|
72
|
+
}
|
|
73
|
+
static fromSnapshot(snapshot, events = []) {
|
|
74
|
+
const { metadata, props } = snapshot;
|
|
75
|
+
const instance = new this(metadata, props);
|
|
76
|
+
instance.applyPastEvents(events);
|
|
77
|
+
return instance;
|
|
78
|
+
}
|
|
79
|
+
static eventAppliersMap() {
|
|
80
|
+
return (0, metadata_1.getEventAppliersMap)(this.prototype);
|
|
81
|
+
}
|
|
82
|
+
static commandHandlersMap() {
|
|
83
|
+
return (0, metadata_1.getCommandHandlersMap)(this.prototype);
|
|
84
|
+
}
|
|
85
|
+
get version() {
|
|
86
|
+
return (this._version +
|
|
87
|
+
(this.hasEvent() ? this._events.length : this._pastEvents.length));
|
|
88
|
+
}
|
|
89
|
+
get events() {
|
|
90
|
+
return this._events;
|
|
91
|
+
}
|
|
92
|
+
get handledCommands() {
|
|
93
|
+
return this._handledCommands;
|
|
94
|
+
}
|
|
95
|
+
eventAppliersMap() {
|
|
96
|
+
return this.constructor.eventAppliersMap();
|
|
97
|
+
}
|
|
98
|
+
hasEvent() {
|
|
99
|
+
return Boolean(this._events.length);
|
|
100
|
+
}
|
|
101
|
+
getApplierForEvent(event) {
|
|
102
|
+
const { type } = event;
|
|
103
|
+
const applier = this.eventAppliersMap().get(type);
|
|
104
|
+
if (!applier)
|
|
105
|
+
throw new Error("Event applier not found");
|
|
106
|
+
return applier;
|
|
107
|
+
}
|
|
108
|
+
validateEventBeforeApply(event) {
|
|
109
|
+
const { source } = event;
|
|
110
|
+
if (source.type !== this.type)
|
|
111
|
+
throw new Error("Invalid source type");
|
|
112
|
+
if (!source.id.equals(this.id))
|
|
113
|
+
throw new Error("Invalid source id");
|
|
114
|
+
if (source.version !== this.version)
|
|
115
|
+
throw new Error("Invalid source version");
|
|
116
|
+
}
|
|
117
|
+
_applyEvent(event) {
|
|
118
|
+
const applier = this.getApplierForEvent(event);
|
|
119
|
+
this.validateEventBeforeApply(event);
|
|
120
|
+
applier.call(this, event);
|
|
121
|
+
}
|
|
122
|
+
applyPastEvent(event) {
|
|
123
|
+
if (this.hasEvent())
|
|
124
|
+
throw new Error();
|
|
125
|
+
this._applyEvent(event);
|
|
126
|
+
this._pastEvents.push(event);
|
|
127
|
+
}
|
|
128
|
+
applyPastEvents(pastEvents) {
|
|
129
|
+
pastEvents.forEach((pastEvent) => {
|
|
130
|
+
this.applyPastEvent(pastEvent);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
applyEvent(event) {
|
|
134
|
+
this._applyEvent(event);
|
|
135
|
+
this._events.push(event);
|
|
136
|
+
}
|
|
137
|
+
applyEvents(events) {
|
|
138
|
+
events.forEach((event) => {
|
|
139
|
+
this.applyEvent(event);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
commandHandlersMap() {
|
|
143
|
+
return this.constructor.commandHandlersMap();
|
|
144
|
+
}
|
|
145
|
+
getHandlerForCommand(command) {
|
|
146
|
+
const { type } = command;
|
|
147
|
+
const handler = this.commandHandlersMap().get(type);
|
|
148
|
+
if (!handler)
|
|
149
|
+
throw new Error("Command handler not found");
|
|
150
|
+
return handler;
|
|
151
|
+
}
|
|
152
|
+
handleCommand(command) {
|
|
153
|
+
const handler = this.getHandlerForCommand(command);
|
|
154
|
+
const events = (0, to_array_1.toArray)(handler.call(this, command));
|
|
155
|
+
events.forEach((event) => {
|
|
156
|
+
var _a;
|
|
157
|
+
event.setContext({
|
|
158
|
+
correlationId: (_a = command.context) === null || _a === void 0 ? void 0 : _a.correlationId,
|
|
159
|
+
causationId: command.id.value,
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
this.applyEvents(events);
|
|
163
|
+
this._handledCommands.push(command);
|
|
164
|
+
return events;
|
|
165
|
+
}
|
|
166
|
+
snap() {
|
|
167
|
+
if (!this._props)
|
|
168
|
+
throw new Error();
|
|
169
|
+
return {
|
|
170
|
+
metadata: {
|
|
171
|
+
id: this.id,
|
|
172
|
+
version: this.version,
|
|
173
|
+
},
|
|
174
|
+
props: this.props(),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
exports.AggregateES = AggregateES;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Class } from "../types/class";
|
|
2
|
+
import { ClassStatic } from "../types/class-static";
|
|
3
|
+
import { Message, MessageContext, MessageMetadata } from "./message";
|
|
4
|
+
import { PropsOf } from "./model";
|
|
5
|
+
export interface CommandMetadata extends MessageMetadata {
|
|
6
|
+
}
|
|
7
|
+
export declare class Command<Props extends object> extends Message<Props> {
|
|
8
|
+
constructor(metadata: CommandMetadata, props: Props);
|
|
9
|
+
static newCommand<T extends AnyCommand>(this: CommandClassWithTypedConstructor<T>, props: PropsOf<T>, context?: MessageContext): T;
|
|
10
|
+
}
|
|
11
|
+
export type AnyCommand = Command<any>;
|
|
12
|
+
export type CommandClass<T extends AnyCommand = AnyCommand, Arguments extends unknown[] = any[]> = Class<T, Arguments> & ClassStatic<typeof Command<PropsOf<T>>>;
|
|
13
|
+
export type CommandClassWithTypedConstructor<T extends AnyCommand = AnyCommand> = CommandClass<T, ConstructorParameters<typeof Command<PropsOf<T>>>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Command = void 0;
|
|
4
|
+
const id_1 = require("./id");
|
|
5
|
+
const message_1 = require("./message");
|
|
6
|
+
class Command extends message_1.Message {
|
|
7
|
+
constructor(metadata, props) {
|
|
8
|
+
super(metadata, props);
|
|
9
|
+
}
|
|
10
|
+
static newCommand(props, context) {
|
|
11
|
+
return new this({
|
|
12
|
+
id: id_1.Id.unique(),
|
|
13
|
+
timestamp: Date.now(),
|
|
14
|
+
context,
|
|
15
|
+
}, props);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.Command = Command;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Class } from "../types/class";
|
|
2
|
+
import { ClassStatic } from "../types/class-static";
|
|
3
|
+
import { Id } from "./id";
|
|
4
|
+
import { Model, PropsOf } from "./model";
|
|
5
|
+
export interface EntityMetadata {
|
|
6
|
+
readonly id: Id;
|
|
7
|
+
}
|
|
8
|
+
export declare class Entity<Props extends object> extends Model<Props> {
|
|
9
|
+
private readonly _id;
|
|
10
|
+
constructor(metadata: EntityMetadata, props?: Props);
|
|
11
|
+
static newEntity<T extends AnyEntity>(this: EntityClassWithTypedConstructor<T>, props: PropsOf<T>): T;
|
|
12
|
+
get id(): Id;
|
|
13
|
+
hasId(id: Id): boolean;
|
|
14
|
+
}
|
|
15
|
+
export type AnyEntity = Entity<object>;
|
|
16
|
+
export type EntityClass<T extends AnyEntity = AnyEntity, Arguments extends unknown[] = any[]> = Class<T, Arguments> & ClassStatic<typeof Entity<PropsOf<T>>>;
|
|
17
|
+
export type EntityClassWithTypedConstructor<T extends AnyEntity = AnyEntity> = EntityClass<T, ConstructorParameters<typeof Entity<PropsOf<T>>>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Entity = void 0;
|
|
4
|
+
const id_1 = require("./id");
|
|
5
|
+
const model_1 = require("./model");
|
|
6
|
+
class Entity extends model_1.Model {
|
|
7
|
+
constructor(metadata, props) {
|
|
8
|
+
super(props);
|
|
9
|
+
this._id = metadata.id;
|
|
10
|
+
}
|
|
11
|
+
static newEntity(props) {
|
|
12
|
+
return new this({
|
|
13
|
+
id: id_1.Id.unique(),
|
|
14
|
+
}, props);
|
|
15
|
+
}
|
|
16
|
+
get id() {
|
|
17
|
+
return this._id;
|
|
18
|
+
}
|
|
19
|
+
hasId(id) {
|
|
20
|
+
return this.id.equals(id);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Entity = Entity;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Class } from "../types/class";
|
|
2
|
+
import { ClassStatic } from "../types/class-static";
|
|
3
|
+
import { Id } from "./id";
|
|
4
|
+
import { Message, MessageContext, MessageMetadata } from "./message";
|
|
5
|
+
import { PropsOf } from "./model";
|
|
6
|
+
import { ModelTypePattern } from "./model-type";
|
|
7
|
+
export type EventSource = Readonly<{
|
|
8
|
+
type: ModelTypePattern;
|
|
9
|
+
id: Id;
|
|
10
|
+
version: number;
|
|
11
|
+
}>;
|
|
12
|
+
export interface EventMetadata extends MessageMetadata {
|
|
13
|
+
source: EventSource;
|
|
14
|
+
}
|
|
15
|
+
export declare class Event<Props extends object> extends Message<Props> {
|
|
16
|
+
private readonly _source;
|
|
17
|
+
constructor(metadata: EventMetadata, props: Props);
|
|
18
|
+
static newEvent<T extends AnyEvent>(this: EventClassWithTypedConstructor<T>, source: EventSource, props: PropsOf<T>, context?: MessageContext): T;
|
|
19
|
+
get source(): Readonly<{
|
|
20
|
+
type: `${string}#${string}`;
|
|
21
|
+
id: Id;
|
|
22
|
+
version: number;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
export type AnyEvent = Event<any>;
|
|
26
|
+
export type EventClass<T extends AnyEvent = AnyEvent, Arguments extends unknown[] = any[]> = Class<T, Arguments> & ClassStatic<typeof Event<PropsOf<T>>>;
|
|
27
|
+
export type EventClassWithTypedConstructor<T extends AnyEvent = AnyEvent> = EventClass<T, ConstructorParameters<typeof Event<PropsOf<T>>>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Event = void 0;
|
|
4
|
+
const id_1 = require("./id");
|
|
5
|
+
const message_1 = require("./message");
|
|
6
|
+
class Event extends message_1.Message {
|
|
7
|
+
constructor(metadata, props) {
|
|
8
|
+
super(metadata, props);
|
|
9
|
+
this._source = metadata.source;
|
|
10
|
+
}
|
|
11
|
+
static newEvent(source, props, context) {
|
|
12
|
+
return new this({
|
|
13
|
+
id: id_1.Id.unique(),
|
|
14
|
+
timestamp: Date.now(),
|
|
15
|
+
source,
|
|
16
|
+
context,
|
|
17
|
+
}, props);
|
|
18
|
+
}
|
|
19
|
+
get source() {
|
|
20
|
+
return this._source;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Event = Event;
|
package/dist/{base → core}/id.js
RENAMED
|
@@ -10,6 +10,12 @@ class Id {
|
|
|
10
10
|
const value = (0, uuid_1.v4)();
|
|
11
11
|
return new Id(value);
|
|
12
12
|
}
|
|
13
|
+
static from(value) {
|
|
14
|
+
const isUUID4 = (0, uuid_1.validate)(value) && (0, uuid_1.version)(value) === 4;
|
|
15
|
+
if (!isUUID4)
|
|
16
|
+
throw new Error("Invalid uuid version 4 value");
|
|
17
|
+
return new Id(value);
|
|
18
|
+
}
|
|
13
19
|
get value() {
|
|
14
20
|
return this._value;
|
|
15
21
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export * from "./aggregate";
|
|
2
|
-
export * from "./message";
|
|
3
2
|
export * from "./command";
|
|
4
3
|
export * from "./entity";
|
|
5
4
|
export * from "./event";
|
|
6
5
|
export * from "./id";
|
|
7
|
-
export * from "./
|
|
8
|
-
export * from "./
|
|
6
|
+
export * from "./message";
|
|
7
|
+
export * from "./metadata";
|
|
8
|
+
export * from "./model";
|
|
9
|
+
export * from "./model-type";
|
|
10
|
+
export * from "./registry";
|
|
9
11
|
export * from "./value-object";
|
|
@@ -15,11 +15,13 @@ 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);
|
|
19
18
|
__exportStar(require("./command"), exports);
|
|
20
19
|
__exportStar(require("./entity"), exports);
|
|
21
20
|
__exportStar(require("./event"), exports);
|
|
22
21
|
__exportStar(require("./id"), exports);
|
|
23
|
-
__exportStar(require("./
|
|
24
|
-
__exportStar(require("./
|
|
22
|
+
__exportStar(require("./message"), exports);
|
|
23
|
+
__exportStar(require("./metadata"), exports);
|
|
24
|
+
__exportStar(require("./model"), exports);
|
|
25
|
+
__exportStar(require("./model-type"), exports);
|
|
26
|
+
__exportStar(require("./registry"), exports);
|
|
25
27
|
__exportStar(require("./value-object"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Id } from "./id";
|
|
2
|
+
import { Model } from "./model";
|
|
3
|
+
export interface MessageContext {
|
|
4
|
+
correlationId?: string;
|
|
5
|
+
causationId?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface MessageMetadata {
|
|
8
|
+
readonly id: Id;
|
|
9
|
+
readonly timestamp: number;
|
|
10
|
+
context?: MessageContext;
|
|
11
|
+
}
|
|
12
|
+
export declare class Message<Props extends object> extends Model<Props> {
|
|
13
|
+
private readonly _id;
|
|
14
|
+
private readonly _timestamp;
|
|
15
|
+
private _context?;
|
|
16
|
+
protected constructor(metadata: MessageMetadata, props: Props);
|
|
17
|
+
get id(): Id;
|
|
18
|
+
get timestamp(): number;
|
|
19
|
+
get context(): MessageContext | undefined;
|
|
20
|
+
setContext(context: Partial<MessageContext>): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Message = void 0;
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
const model_1 = require("./model");
|
|
9
|
+
class Message extends model_1.Model {
|
|
10
|
+
constructor(metadata, props) {
|
|
11
|
+
var _a;
|
|
12
|
+
super(props);
|
|
13
|
+
this._id = metadata.id;
|
|
14
|
+
this._timestamp = metadata.timestamp;
|
|
15
|
+
this._context = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.context) !== null && _a !== void 0 ? _a : {};
|
|
16
|
+
}
|
|
17
|
+
get id() {
|
|
18
|
+
return this._id;
|
|
19
|
+
}
|
|
20
|
+
get timestamp() {
|
|
21
|
+
return this._timestamp;
|
|
22
|
+
}
|
|
23
|
+
get context() {
|
|
24
|
+
return this._context;
|
|
25
|
+
}
|
|
26
|
+
setContext(context) {
|
|
27
|
+
this._context = lodash_1.default.merge(this.context, context);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Message = Message;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { CommandHandler, EventApplier } from "./aggregate";
|
|
3
|
+
import { AnyCommand } from "./command";
|
|
4
|
+
import { AnyEvent } from "./event";
|
|
5
|
+
import { ModelTypePattern } from "./model-type";
|
|
6
|
+
export declare const MODEL_TYPE = "MODEL_TYPE";
|
|
7
|
+
export declare const defineModelType: (target: object, type: ModelTypePattern) => void;
|
|
8
|
+
export declare const getModelType: (target: object) => ModelTypePattern;
|
|
9
|
+
export declare const EVENT_APPLIERS = "EVENT_APPLIERS";
|
|
10
|
+
export declare const getEventAppliersMap: (target: object) => Map<string, EventApplier>;
|
|
11
|
+
export declare const defineEventApplier: <T extends AnyEvent>(target: object, eventType: string, applier: EventApplier<T>) => void;
|
|
12
|
+
export declare const COMMAND_HANDLERS = "COMMAND_HANDLERS";
|
|
13
|
+
export declare const getCommandHandlersMap: (target: object) => Map<string, CommandHandler>;
|
|
14
|
+
export declare const defineCommandHandler: <T extends AnyCommand>(target: object, commandType: string, handler: CommandHandler<T>) => void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
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;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
// Model type
|
|
6
|
+
exports.MODEL_TYPE = "MODEL_TYPE";
|
|
7
|
+
const defineModelType = (target, type) => {
|
|
8
|
+
Reflect.defineMetadata(exports.MODEL_TYPE, type, target);
|
|
9
|
+
};
|
|
10
|
+
exports.defineModelType = defineModelType;
|
|
11
|
+
const getModelType = (target) => {
|
|
12
|
+
const type = Reflect.getMetadata(exports.MODEL_TYPE, target);
|
|
13
|
+
if (!type)
|
|
14
|
+
throw new Error("The type has not been defined");
|
|
15
|
+
return type;
|
|
16
|
+
};
|
|
17
|
+
exports.getModelType = getModelType;
|
|
18
|
+
// Event applier map
|
|
19
|
+
exports.EVENT_APPLIERS = "EVENT_APPLIERS";
|
|
20
|
+
const getEventAppliersMap = (target) => {
|
|
21
|
+
return (Reflect.getMetadata(exports.EVENT_APPLIERS, target) ||
|
|
22
|
+
new Map());
|
|
23
|
+
};
|
|
24
|
+
exports.getEventAppliersMap = getEventAppliersMap;
|
|
25
|
+
const defineEventApplier = (target, eventType, applier) => {
|
|
26
|
+
const eventAppliersMap = (0, exports.getEventAppliersMap)(target);
|
|
27
|
+
eventAppliersMap.set(eventType, applier);
|
|
28
|
+
Reflect.defineMetadata(exports.EVENT_APPLIERS, eventAppliersMap, target);
|
|
29
|
+
};
|
|
30
|
+
exports.defineEventApplier = defineEventApplier;
|
|
31
|
+
// Command handler map
|
|
32
|
+
exports.COMMAND_HANDLERS = "COMMAND_HANDLERS";
|
|
33
|
+
const getCommandHandlersMap = (target) => {
|
|
34
|
+
return (Reflect.getMetadata(exports.COMMAND_HANDLERS, target) ||
|
|
35
|
+
new Map());
|
|
36
|
+
};
|
|
37
|
+
exports.getCommandHandlersMap = getCommandHandlersMap;
|
|
38
|
+
const defineCommandHandler = (target, commandType, handler) => {
|
|
39
|
+
const commandHandlersMap = (0, exports.getCommandHandlersMap)(target);
|
|
40
|
+
commandHandlersMap.set(commandType, handler);
|
|
41
|
+
Reflect.defineMetadata(exports.COMMAND_HANDLERS, commandHandlersMap, target);
|
|
42
|
+
};
|
|
43
|
+
exports.defineCommandHandler = defineCommandHandler;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Class } from "../types/class";
|
|
2
|
+
import { ClassStatic } from "../types/class-static";
|
|
3
|
+
export declare const seperator: "#";
|
|
4
|
+
export type ModelTypePattern<Prefix extends string = string> = `${Prefix}${typeof seperator}${string}`;
|
|
5
|
+
export declare const ModelTypePatternRegex: RegExp;
|
|
6
|
+
export declare class ModelType<Prefix extends string = string> {
|
|
7
|
+
readonly prefix: Prefix;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
constructor(prefix: Prefix, name: string);
|
|
10
|
+
static from<Prefix extends string = string>(value: ModelTypePattern<Prefix>): ModelType<Prefix>;
|
|
11
|
+
static isPattern(type: string): type is ModelTypePattern;
|
|
12
|
+
get value(): ModelTypePattern<Prefix>;
|
|
13
|
+
}
|
|
14
|
+
export type PrefixOf<T extends ModelType> = T extends ModelType<infer Prefix> ? Prefix : never;
|
|
15
|
+
export type ModelTypeClass<T extends ModelType> = Class<T> & ClassStatic<typeof ModelType<PrefixOf<T>>>;
|
|
16
|
+
export type ModelTypePatternOf<T extends ModelType> = ModelTypePattern<PrefixOf<T>>;
|
|
17
|
+
export declare const createPrefixedModelTypeClass: <Prefix extends string = string>(prefix: Prefix) => {
|
|
18
|
+
new (name: string): {
|
|
19
|
+
readonly prefix: Prefix;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly value: `${Prefix}#${string}`;
|
|
22
|
+
};
|
|
23
|
+
from<Prefix_1 extends string = string>(value: `${Prefix_1}#${string}`): ModelType<Prefix_1>;
|
|
24
|
+
isPattern(type: string): type is `${string}#${string}`;
|
|
25
|
+
};
|
|
26
|
+
export declare const EntityTypePrefix: "Entity";
|
|
27
|
+
declare const EntityType_base: {
|
|
28
|
+
new (name: string): {
|
|
29
|
+
readonly prefix: "Entity";
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly value: `Entity#${string}`;
|
|
32
|
+
};
|
|
33
|
+
from<Prefix extends string = string>(value: `${Prefix}#${string}`): ModelType<Prefix>;
|
|
34
|
+
isPattern(type: string): type is `${string}#${string}`;
|
|
35
|
+
};
|
|
36
|
+
export declare class EntityType extends EntityType_base {
|
|
37
|
+
}
|
|
38
|
+
export declare const AggregateTypePrefix: "Aggregate";
|
|
39
|
+
declare const AggregateType_base: {
|
|
40
|
+
new (name: string): {
|
|
41
|
+
readonly prefix: "Aggregate";
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly value: `Aggregate#${string}`;
|
|
44
|
+
};
|
|
45
|
+
from<Prefix extends string = string>(value: `${Prefix}#${string}`): ModelType<Prefix>;
|
|
46
|
+
isPattern(type: string): type is `${string}#${string}`;
|
|
47
|
+
};
|
|
48
|
+
export declare class AggregateType extends AggregateType_base {
|
|
49
|
+
}
|
|
50
|
+
export declare const EventTypePrefix: "Event";
|
|
51
|
+
declare const EventType_base: {
|
|
52
|
+
new (name: string): {
|
|
53
|
+
readonly prefix: "Event";
|
|
54
|
+
readonly name: string;
|
|
55
|
+
readonly value: `Event#${string}`;
|
|
56
|
+
};
|
|
57
|
+
from<Prefix extends string = string>(value: `${Prefix}#${string}`): ModelType<Prefix>;
|
|
58
|
+
isPattern(type: string): type is `${string}#${string}`;
|
|
59
|
+
};
|
|
60
|
+
export declare class EventType extends EventType_base {
|
|
61
|
+
}
|
|
62
|
+
export declare const CommandTypePrefix: "Command";
|
|
63
|
+
declare const CommandType_base: {
|
|
64
|
+
new (name: string): {
|
|
65
|
+
readonly prefix: "Command";
|
|
66
|
+
readonly name: string;
|
|
67
|
+
readonly value: `Command#${string}`;
|
|
68
|
+
};
|
|
69
|
+
from<Prefix extends string = string>(value: `${Prefix}#${string}`): ModelType<Prefix>;
|
|
70
|
+
isPattern(type: string): type is `${string}#${string}`;
|
|
71
|
+
};
|
|
72
|
+
export declare class CommandType extends CommandType_base {
|
|
73
|
+
}
|
|
74
|
+
export declare const ValueObjectTypePrefix: "ValueObject";
|
|
75
|
+
declare const ValueObjectType_base: {
|
|
76
|
+
new (name: string): {
|
|
77
|
+
readonly prefix: "ValueObject";
|
|
78
|
+
readonly name: string;
|
|
79
|
+
readonly value: `ValueObject#${string}`;
|
|
80
|
+
};
|
|
81
|
+
from<Prefix extends string = string>(value: `${Prefix}#${string}`): ModelType<Prefix>;
|
|
82
|
+
isPattern(type: string): type is `${string}#${string}`;
|
|
83
|
+
};
|
|
84
|
+
export declare class ValueObjectType extends ValueObjectType_base {
|
|
85
|
+
}
|
|
86
|
+
export {};
|