ddd-node 13.0.0-0 → 14.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.
- package/.editorconfig +12 -0
- package/dist/core/aggregate/base.d.ts +12 -0
- package/dist/core/aggregate/base.js +15 -0
- package/dist/core/aggregate/event-sourced-aggregate.d.ts +48 -0
- package/dist/core/aggregate/event-sourced-aggregate.js +130 -0
- package/dist/core/aggregate/index.d.ts +3 -0
- package/dist/core/aggregate/index.js +3 -0
- package/dist/core/aggregate/state-aggregate.d.ts +22 -0
- package/dist/core/aggregate/state-aggregate.js +35 -0
- package/dist/core/aggregate/types.d.ts +4 -0
- package/dist/core/aggregate/types.js +1 -0
- package/dist/core/entity.d.ts +12 -9
- package/dist/core/entity.js +3 -7
- package/dist/core/enum.d.ts +12 -10
- package/dist/core/enum.js +30 -22
- package/dist/core/id/common/index.d.ts +2 -0
- package/dist/core/id/common/index.js +2 -0
- package/dist/core/id/common/snowflake.d.ts +9 -0
- package/dist/core/id/common/snowflake.js +13 -0
- package/dist/core/id/common/uuid4.d.ts +6 -0
- package/dist/core/id/common/uuid4.js +17 -0
- package/dist/core/id/id.d.ts +7 -5
- package/dist/core/id/id.js +13 -8
- package/dist/core/id/index.d.ts +2 -2
- package/dist/core/id/index.js +3 -19
- package/dist/core/id/interface.d.ts +10 -0
- package/dist/core/id/interface.js +1 -0
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +8 -25
- package/dist/core/message/command.d.ts +14 -9
- package/dist/core/message/command.js +10 -9
- package/dist/core/message/event.d.ts +15 -11
- package/dist/core/message/event.js +9 -11
- package/dist/core/message/index.js +3 -19
- package/dist/core/message/message.d.ts +8 -4
- package/dist/core/message/message.js +13 -14
- package/dist/core/model/errors.d.ts +3 -0
- package/dist/core/model/errors.js +5 -0
- package/dist/core/model/index.js +2 -18
- package/dist/core/model/model-with-id.d.ts +8 -8
- package/dist/core/model/model-with-id.js +9 -13
- package/dist/core/model/model.d.ts +30 -19
- package/dist/core/model/model.js +50 -41
- package/dist/core/repository.d.ts +2 -2
- package/dist/core/repository.js +1 -2
- package/dist/core/value-object.d.ts +11 -8
- package/dist/core/value-object.js +8 -12
- package/dist/decorators/aggregate-es/handle.d.ts +2 -0
- package/dist/decorators/aggregate-es/handle.js +9 -0
- package/dist/decorators/aggregate-es/index.js +2 -0
- package/dist/decorators/aggregate-es/when.d.ts +2 -0
- package/dist/decorators/aggregate-es/when.js +9 -0
- package/dist/decorators/id.d.ts +2 -2
- package/dist/decorators/id.js +3 -7
- package/dist/decorators/index.d.ts +1 -1
- package/dist/decorators/index.js +3 -19
- package/dist/decorators/model/enum.d.ts +2 -0
- package/dist/decorators/model/enum.js +6 -0
- package/dist/decorators/model/index.d.ts +3 -3
- package/dist/decorators/model/index.js +5 -21
- package/dist/decorators/model/model-name.d.ts +2 -0
- package/dist/decorators/model/model-name.js +6 -0
- package/dist/decorators/model/prop.d.ts +2 -1
- package/dist/decorators/model/prop.js +5 -7
- package/dist/decorators/model/static.d.ts +3 -3
- package/dist/decorators/model/static.js +7 -30
- package/dist/decorators/model/validator.d.ts +3 -0
- package/dist/decorators/model/validator.js +7 -0
- package/dist/decorators/type.js +1 -2
- package/dist/index.js +5 -21
- package/dist/meta/aggregate.metadata.d.ts +5 -5
- package/dist/meta/aggregate.metadata.js +19 -22
- package/dist/meta/helpers/index.d.ts +1 -0
- package/dist/meta/helpers/index.js +1 -0
- package/dist/meta/helpers/static-value.d.ts +7 -0
- package/dist/meta/helpers/static-value.js +11 -0
- package/dist/meta/id.metadata.d.ts +4 -4
- package/dist/meta/id.metadata.js +10 -10
- package/dist/meta/index.d.ts +1 -0
- package/dist/meta/index.js +4 -19
- package/dist/meta/model.metadata.d.ts +14 -12
- package/dist/meta/model.metadata.js +79 -43
- package/dist/types/class-static.js +1 -2
- package/dist/types/class.js +1 -2
- package/dist/types/index.js +2 -18
- package/dist/utils/index.js +1 -17
- package/dist/utils/to-array.js +2 -8
- package/package.json +27 -3
- package/ts-loader.js +4 -0
- package/tsconfig.build.json +4 -4
- package/tsconfig.json +29 -22
- package/tsconfig.spec.json +5 -0
- package/dist/core/aggregate.d.ts +0 -67
- package/dist/core/aggregate.js +0 -179
- package/dist/core/error.d.ts +0 -4
- package/dist/core/error.js +0 -9
- package/dist/core/id/built/index.d.ts +0 -2
- package/dist/core/id/built/index.js +0 -18
- package/dist/core/id/built/snowflake.generator.d.ts +0 -8
- package/dist/core/id/built/snowflake.generator.js +0 -16
- package/dist/core/id/built/uuid4.generator.d.ts +0 -5
- package/dist/core/id/built/uuid4.generator.js +0 -16
- package/dist/core/id/id-generator.d.ts +0 -8
- package/dist/core/id/id-generator.js +0 -17
- package/dist/decorators/aggregate/handle.d.ts +0 -2
- package/dist/decorators/aggregate/handle.js +0 -13
- package/dist/decorators/aggregate/index.js +0 -18
- package/dist/decorators/aggregate/when.d.ts +0 -2
- package/dist/decorators/aggregate/when.js +0 -13
- package/dist/decorators/model/helpers/static-values.d.ts +0 -12
- package/dist/decorators/model/helpers/static-values.js +0 -30
- package/dist/decorators/model/model.d.ts +0 -10
- package/dist/decorators/model/model.js +0 -26
- package/dist/decorators/model/name.d.ts +0 -2
- package/dist/decorators/model/name.js +0 -10
- package/dist/decorators/model/types/static-typed-target.d.ts +0 -5
- package/dist/decorators/model/types/static-typed-target.js +0 -3
- package/dist/decorators/model/types/static-value-builder.d.ts +0 -2
- package/dist/decorators/model/types/static-value-builder.js +0 -2
- package/dist/decorators/model/validate.d.ts +0 -3
- package/dist/decorators/model/validate.js +0 -11
- /package/dist/decorators/{aggregate → aggregate-es}/index.d.ts +0 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EntityBase, EntityMetadata } from "../entity";
|
|
2
|
+
import { AnyEvent, EventClassWithTypedConstructor } from "../message";
|
|
3
|
+
import { Props, PropsOf } from "../model";
|
|
4
|
+
export interface AggregateMetadata extends EntityMetadata {
|
|
5
|
+
version: number;
|
|
6
|
+
}
|
|
7
|
+
export declare abstract class AggregateBase<P extends Props> extends EntityBase<P> {
|
|
8
|
+
protected readonly _version: number;
|
|
9
|
+
constructor(metadata: AggregateMetadata, props?: P);
|
|
10
|
+
abstract getVersion(): number;
|
|
11
|
+
protected newEvent<E extends AnyEvent>(eventClass: EventClassWithTypedConstructor<E>, props: PropsOf<E>): E;
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EntityBase } from "../entity";
|
|
2
|
+
export class AggregateBase extends EntityBase {
|
|
3
|
+
constructor(metadata, props) {
|
|
4
|
+
super(metadata, props);
|
|
5
|
+
this._version = metadata.version;
|
|
6
|
+
}
|
|
7
|
+
newEvent(eventClass, props) {
|
|
8
|
+
const eventSource = {
|
|
9
|
+
aggregate: this.modelName(),
|
|
10
|
+
id: this.id(),
|
|
11
|
+
version: this.getVersion(),
|
|
12
|
+
};
|
|
13
|
+
return eventClass.newEvent(eventSource, props);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Class } from "type-fest";
|
|
2
|
+
import { ClassStatic } from "../../types";
|
|
3
|
+
import { Id } from "../id";
|
|
4
|
+
import { AnyCommand, AnyEvent, EventClassWithTypedConstructor } from "../message";
|
|
5
|
+
import { Props, PropsOf } from "../model";
|
|
6
|
+
import { AggregateBase, AggregateMetadata } from "./base";
|
|
7
|
+
export interface SnapshotMetadata extends AggregateMetadata {
|
|
8
|
+
}
|
|
9
|
+
export interface Snapshot<T extends AnyEventSourcedAggregate> {
|
|
10
|
+
metadata: SnapshotMetadata;
|
|
11
|
+
props: PropsOf<T>;
|
|
12
|
+
}
|
|
13
|
+
export declare class EventSourcedAggregateBase<P extends Props> extends AggregateBase<P> {
|
|
14
|
+
private _handledCommands;
|
|
15
|
+
private _pastEvents;
|
|
16
|
+
private _events;
|
|
17
|
+
constructor(metadata: AggregateMetadata, props?: P);
|
|
18
|
+
static newStream<T extends AnyEventSourcedAggregate>(this: EventSourcedAggregateClassWithTypedConstructor<T>, id?: Id): T;
|
|
19
|
+
static fromStream<T extends AnyEventSourcedAggregate>(this: EventSourcedAggregateClassWithTypedConstructor<T>, id: Id, pastEvents?: AnyEvent[]): T;
|
|
20
|
+
static fromSnapshot<T extends AnyEventSourcedAggregate>(this: EventSourcedAggregateClassWithTypedConstructor<T>, snapshot: Snapshot<T>, pastEventsAfterSnapshot?: AnyEvent[]): T;
|
|
21
|
+
static eventApplierMap<T extends AnyEventSourcedAggregate>(this: EventSourcedAggregateClass<T>): Map<string, EventApplier>;
|
|
22
|
+
static commandHandlerMap<T extends AnyEventSourcedAggregate>(this: EventSourcedAggregateClass<T>): Map<string, CommandHandler>;
|
|
23
|
+
getVersion(): number;
|
|
24
|
+
getPastEvents(): AnyEvent[];
|
|
25
|
+
getEvents(): AnyEvent[];
|
|
26
|
+
getHandledCommands(): AnyCommand[];
|
|
27
|
+
eventApplierMap(): Map<string, EventApplier>;
|
|
28
|
+
hasNewEvent(): boolean;
|
|
29
|
+
getApplierForEvent<E extends AnyEvent>(event: E): EventApplier<E>;
|
|
30
|
+
private validateEventBeforeApply;
|
|
31
|
+
private _applyEvent;
|
|
32
|
+
private applyPastEvent;
|
|
33
|
+
private applyPastEvents;
|
|
34
|
+
applyEvent<E extends AnyEvent>(event: E): void;
|
|
35
|
+
applyEvents(events: AnyEvent[]): void;
|
|
36
|
+
applyNewEvent<E extends AnyEvent>(eventClass: EventClassWithTypedConstructor<E>, props: PropsOf<E>): void;
|
|
37
|
+
commandHandlerMap(): Map<string, CommandHandler>;
|
|
38
|
+
getHandlerForCommand<C extends AnyCommand>(command: C): CommandHandler<C, AnyEvent | AnyEvent[]>;
|
|
39
|
+
handleCommand<C extends AnyCommand>(command: C): AnyEvent[];
|
|
40
|
+
snap(): Snapshot<this>;
|
|
41
|
+
}
|
|
42
|
+
export type AnyEventSourcedAggregate = EventSourcedAggregateBase<Props>;
|
|
43
|
+
export interface EventSourcedAggregateClass<T extends AnyEventSourcedAggregate = AnyEventSourcedAggregate, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof EventSourcedAggregateBase<PropsOf<T>>> {
|
|
44
|
+
}
|
|
45
|
+
export interface EventSourcedAggregateClassWithTypedConstructor<T extends AnyEventSourcedAggregate = AnyEventSourcedAggregate> extends EventSourcedAggregateClass<T, ConstructorParameters<typeof EventSourcedAggregateBase<PropsOf<T>>>> {
|
|
46
|
+
}
|
|
47
|
+
export type EventApplier<T extends AnyEvent = AnyEvent> = (event: T) => void;
|
|
48
|
+
export type CommandHandler<T extends AnyCommand = AnyCommand, U extends AnyEvent | AnyEvent[] = AnyEvent | AnyEvent[]> = (command: T) => U;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { getCommandHandlerMap, getEventApplierMap } from "../../meta";
|
|
2
|
+
import { toArray } from "../../utils";
|
|
3
|
+
import { AggregateBase } from "./base";
|
|
4
|
+
export class EventSourcedAggregateBase extends AggregateBase {
|
|
5
|
+
constructor(metadata, props) {
|
|
6
|
+
super(metadata, props);
|
|
7
|
+
this._handledCommands = [];
|
|
8
|
+
this._events = [];
|
|
9
|
+
this._pastEvents = [];
|
|
10
|
+
}
|
|
11
|
+
static newStream(id) {
|
|
12
|
+
return new this({
|
|
13
|
+
id: this.id(id),
|
|
14
|
+
version: 0,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
static fromStream(id, pastEvents = []) {
|
|
18
|
+
const instance = this.newStream(id);
|
|
19
|
+
instance.applyPastEvents(pastEvents);
|
|
20
|
+
return instance;
|
|
21
|
+
}
|
|
22
|
+
static fromSnapshot(snapshot, pastEventsAfterSnapshot = []) {
|
|
23
|
+
const { metadata, props } = snapshot;
|
|
24
|
+
const instance = new this(metadata, props);
|
|
25
|
+
instance.applyPastEvents(pastEventsAfterSnapshot);
|
|
26
|
+
return instance;
|
|
27
|
+
}
|
|
28
|
+
static eventApplierMap() {
|
|
29
|
+
return getEventApplierMap(this.prototype);
|
|
30
|
+
}
|
|
31
|
+
static commandHandlerMap() {
|
|
32
|
+
return getCommandHandlerMap(this.prototype);
|
|
33
|
+
}
|
|
34
|
+
getVersion() {
|
|
35
|
+
return this._version + this._pastEvents.length + this._events.length;
|
|
36
|
+
}
|
|
37
|
+
getPastEvents() {
|
|
38
|
+
return [...this._pastEvents];
|
|
39
|
+
}
|
|
40
|
+
getEvents() {
|
|
41
|
+
return [...this._events];
|
|
42
|
+
}
|
|
43
|
+
getHandledCommands() {
|
|
44
|
+
return [...this._handledCommands];
|
|
45
|
+
}
|
|
46
|
+
eventApplierMap() {
|
|
47
|
+
return this.constructor.eventApplierMap();
|
|
48
|
+
}
|
|
49
|
+
hasNewEvent() {
|
|
50
|
+
return Boolean(this._events.length);
|
|
51
|
+
}
|
|
52
|
+
getApplierForEvent(event) {
|
|
53
|
+
const eventName = event.modelName();
|
|
54
|
+
const applier = this.eventApplierMap().get(eventName);
|
|
55
|
+
if (!applier)
|
|
56
|
+
throw new Error("Event applier not found");
|
|
57
|
+
return applier;
|
|
58
|
+
}
|
|
59
|
+
validateEventBeforeApply(event) {
|
|
60
|
+
const eventSource = event.getSource();
|
|
61
|
+
if (eventSource.aggregate !== this.modelName())
|
|
62
|
+
throw new Error("Invalid source type");
|
|
63
|
+
if (!eventSource.id.equals(this._id))
|
|
64
|
+
throw new Error("Invalid source id");
|
|
65
|
+
if (eventSource.version !== this.getVersion())
|
|
66
|
+
throw new Error("Invalid source version");
|
|
67
|
+
}
|
|
68
|
+
_applyEvent(event) {
|
|
69
|
+
const applier = this.getApplierForEvent(event);
|
|
70
|
+
this.validateEventBeforeApply(event);
|
|
71
|
+
applier.call(this, event);
|
|
72
|
+
}
|
|
73
|
+
applyPastEvent(event) {
|
|
74
|
+
if (this.hasNewEvent())
|
|
75
|
+
throw new Error();
|
|
76
|
+
this._applyEvent(event);
|
|
77
|
+
this._pastEvents.push(event);
|
|
78
|
+
}
|
|
79
|
+
applyPastEvents(pastEvents) {
|
|
80
|
+
pastEvents.forEach((pastEvent) => {
|
|
81
|
+
this.applyPastEvent(pastEvent);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
applyEvent(event) {
|
|
85
|
+
this._applyEvent(event);
|
|
86
|
+
this._events.push(event);
|
|
87
|
+
}
|
|
88
|
+
applyEvents(events) {
|
|
89
|
+
events.forEach((event) => {
|
|
90
|
+
this.applyEvent(event);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
applyNewEvent(eventClass, props) {
|
|
94
|
+
this.applyEvent(this.newEvent(eventClass, props));
|
|
95
|
+
}
|
|
96
|
+
commandHandlerMap() {
|
|
97
|
+
return this.constructor.commandHandlerMap();
|
|
98
|
+
}
|
|
99
|
+
getHandlerForCommand(command) {
|
|
100
|
+
const commandName = command.modelName();
|
|
101
|
+
const handler = this.commandHandlerMap().get(commandName);
|
|
102
|
+
if (!handler)
|
|
103
|
+
throw new Error("Command handler not found");
|
|
104
|
+
return handler;
|
|
105
|
+
}
|
|
106
|
+
handleCommand(command) {
|
|
107
|
+
const handler = this.getHandlerForCommand(command);
|
|
108
|
+
const events = toArray(handler.call(this, command));
|
|
109
|
+
events.forEach((event) => {
|
|
110
|
+
event.setContext({
|
|
111
|
+
correlationId: command.getContext()?.correlationId,
|
|
112
|
+
causationId: command.id().value,
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
this.applyEvents(events);
|
|
116
|
+
this._handledCommands.push(command);
|
|
117
|
+
return events;
|
|
118
|
+
}
|
|
119
|
+
snap() {
|
|
120
|
+
if (!this._props)
|
|
121
|
+
throw new Error();
|
|
122
|
+
return {
|
|
123
|
+
metadata: {
|
|
124
|
+
id: this.id(),
|
|
125
|
+
version: this.getVersion(),
|
|
126
|
+
},
|
|
127
|
+
props: this.props(),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Class } from "type-fest";
|
|
2
|
+
import { ClassStatic } from "../../types";
|
|
3
|
+
import { Id } from "../id";
|
|
4
|
+
import { AnyEvent, EventClass } from "../message";
|
|
5
|
+
import { Props, PropsOf } from "../model";
|
|
6
|
+
import { AggregateBase, AggregateMetadata } from "./base";
|
|
7
|
+
export declare class StateAggregateBase<P extends Props> extends AggregateBase<P> {
|
|
8
|
+
private _events;
|
|
9
|
+
constructor(metadata: AggregateMetadata, props: P);
|
|
10
|
+
static newAggregate<T extends AnyStateAggregate>(this: StateAggregateClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
|
|
11
|
+
props(): P;
|
|
12
|
+
getVersion(): number;
|
|
13
|
+
getEvents(): AnyEvent[];
|
|
14
|
+
protected recordEvent<E extends AnyEvent>(event: E): void;
|
|
15
|
+
protected recordEvent<E extends AnyEvent>(eventClass: EventClass<E>, props: PropsOf<E>): void;
|
|
16
|
+
clearEvents(): void;
|
|
17
|
+
}
|
|
18
|
+
export type AnyStateAggregate = StateAggregateBase<Props>;
|
|
19
|
+
export interface StateAggregateClass<T extends AnyStateAggregate = AnyStateAggregate, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof StateAggregateBase<PropsOf<T>>> {
|
|
20
|
+
}
|
|
21
|
+
export interface StateAggregateClassWithTypedConstructor<T extends AnyStateAggregate = AnyStateAggregate> extends StateAggregateClass<T, ConstructorParameters<typeof StateAggregateBase<PropsOf<T>>>> {
|
|
22
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AggregateBase } from "./base";
|
|
2
|
+
export class StateAggregateBase extends AggregateBase {
|
|
3
|
+
constructor(metadata, props) {
|
|
4
|
+
super(metadata, props);
|
|
5
|
+
this._events = [];
|
|
6
|
+
}
|
|
7
|
+
static newAggregate(props, id) {
|
|
8
|
+
return new this({
|
|
9
|
+
id: this.id(id),
|
|
10
|
+
version: 0,
|
|
11
|
+
}, props);
|
|
12
|
+
}
|
|
13
|
+
props() {
|
|
14
|
+
return super.props();
|
|
15
|
+
}
|
|
16
|
+
getVersion() {
|
|
17
|
+
return this._version;
|
|
18
|
+
}
|
|
19
|
+
getEvents() {
|
|
20
|
+
return this._events;
|
|
21
|
+
}
|
|
22
|
+
recordEvent(param1, param2) {
|
|
23
|
+
let event;
|
|
24
|
+
if (typeof param1 === "function" && param2) {
|
|
25
|
+
event = this.newEvent(param1, param2);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
event = param1;
|
|
29
|
+
}
|
|
30
|
+
this._events.push(event);
|
|
31
|
+
}
|
|
32
|
+
clearEvents() {
|
|
33
|
+
this._events = [];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AnyEventSourcedAggregate, EventSourcedAggregateClass } from "./event-sourced-aggregate";
|
|
2
|
+
import { AnyStateAggregate, StateAggregateClass } from "./state-aggregate";
|
|
3
|
+
export type AnyAggregate = AnyStateAggregate | AnyEventSourcedAggregate;
|
|
4
|
+
export type AnyAggregateClass = StateAggregateClass | EventSourcedAggregateClass;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/core/entity.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { Class } from "type-fest";
|
|
2
2
|
import { Id } from "./id";
|
|
3
|
-
import { ModelWithId, PropsOf } from "./model";
|
|
4
|
-
import { ClassStatic } from "../types
|
|
3
|
+
import { ModelWithId, Props, PropsOf } from "./model";
|
|
4
|
+
import { ClassStatic } from "../types";
|
|
5
5
|
export interface EntityMetadata {
|
|
6
6
|
readonly id: Id;
|
|
7
7
|
}
|
|
8
|
-
export declare
|
|
9
|
-
constructor(metadata: EntityMetadata, props?:
|
|
10
|
-
static
|
|
8
|
+
export declare class EntityBase<P extends Props> extends ModelWithId<P> {
|
|
9
|
+
constructor(metadata: EntityMetadata, props?: P);
|
|
10
|
+
static mutable(): boolean;
|
|
11
11
|
static newEntity<T extends AnyEntity>(this: EntityClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
|
|
12
12
|
}
|
|
13
|
-
export type AnyEntity = EntityBase<
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
export
|
|
13
|
+
export type AnyEntity = EntityBase<Props>;
|
|
14
|
+
export interface EntityClass<T extends AnyEntity = AnyEntity, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof EntityBase<PropsOf<T>>> {
|
|
15
|
+
}
|
|
16
|
+
export interface EntityClassWithTypedConstructor<T extends AnyEntity = AnyEntity> extends EntityClass<T, ConstructorParameters<typeof EntityBase<PropsOf<T>>>> {
|
|
17
|
+
}
|
|
18
|
+
export interface EntityClassWithProps<P extends Props> extends EntityClass<EntityBase<P>> {
|
|
19
|
+
}
|
package/dist/core/entity.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.EntityBase = void 0;
|
|
4
|
-
const model_1 = require("./model");
|
|
5
|
-
class EntityBase extends model_1.ModelWithId {
|
|
1
|
+
import { ModelWithId } from "./model";
|
|
2
|
+
export class EntityBase extends ModelWithId {
|
|
6
3
|
constructor(metadata, props) {
|
|
7
4
|
super(metadata.id);
|
|
8
5
|
if (props)
|
|
9
6
|
this.initializeProps(props);
|
|
10
7
|
}
|
|
11
|
-
static
|
|
8
|
+
static mutable() {
|
|
12
9
|
return true;
|
|
13
10
|
}
|
|
14
11
|
static newEntity(props, id) {
|
|
@@ -17,4 +14,3 @@ class EntityBase extends model_1.ModelWithId {
|
|
|
17
14
|
}, props);
|
|
18
15
|
}
|
|
19
16
|
}
|
|
20
|
-
exports.EntityBase = EntityBase;
|
package/dist/core/enum.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import { ClassStatic } from "../types
|
|
1
|
+
import { ClassStatic } from "../types";
|
|
2
2
|
import { Class } from "type-fest";
|
|
3
3
|
import { ModelBase } from "./model";
|
|
4
4
|
export type EnumValue = string | number;
|
|
5
|
-
export interface EnumProps
|
|
6
|
-
value:
|
|
5
|
+
export interface EnumProps {
|
|
6
|
+
value: EnumValue;
|
|
7
7
|
}
|
|
8
|
-
export declare class EnumBase
|
|
9
|
-
static values
|
|
10
|
-
static parse<T extends EnumBase>(this: EnumClass<T>,
|
|
11
|
-
constructor(value:
|
|
12
|
-
|
|
8
|
+
export declare class EnumBase extends ModelBase<EnumProps> {
|
|
9
|
+
static values(): EnumBase[];
|
|
10
|
+
static parse<T extends EnumBase>(this: EnumClass<T>, providedValue: EnumValue): T | undefined;
|
|
11
|
+
constructor(value: EnumValue);
|
|
12
|
+
props(): EnumProps;
|
|
13
|
+
get value(): EnumValue;
|
|
14
|
+
equals<T extends EnumBase>(other: T): boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface EnumClass<T extends EnumBase = EnumBase> extends Class<T>, ClassStatic<typeof EnumBase> {
|
|
13
17
|
}
|
|
14
|
-
export type ValueOf<T extends EnumBase> = T extends EnumBase<infer V extends EnumValue> ? V : never;
|
|
15
|
-
export type EnumClass<T extends EnumBase = EnumBase> = Class<T> & ClassStatic<typeof EnumBase>;
|
package/dist/core/enum.js
CHANGED
|
@@ -1,27 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { ModelBase } from "./model";
|
|
2
|
+
export class EnumBase extends ModelBase {
|
|
3
|
+
static values() {
|
|
4
|
+
return Array.from(this.ownStaticValues().values()).map((staticValue) => staticValue.value);
|
|
5
|
+
}
|
|
6
|
+
static parse(providedValue) {
|
|
7
|
+
let key;
|
|
8
|
+
this.ownStaticValues().forEach((staticValue, staticValueKey) => {
|
|
9
|
+
if (staticValue.value instanceof this) {
|
|
10
|
+
const staticEnum = staticValue.value;
|
|
11
|
+
if (staticEnum.value === providedValue && !key) {
|
|
12
|
+
key = staticValueKey;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
if (key)
|
|
17
|
+
return this.ownStaticValues().get(key)?.value;
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
18
20
|
constructor(value) {
|
|
19
21
|
super();
|
|
20
22
|
this.initializeProps({ value });
|
|
21
23
|
}
|
|
24
|
+
props() {
|
|
25
|
+
return super.props();
|
|
26
|
+
}
|
|
27
|
+
get value() {
|
|
28
|
+
return this._props.value;
|
|
29
|
+
}
|
|
30
|
+
equals(other) {
|
|
31
|
+
const equalType = other instanceof this.constructor;
|
|
32
|
+
const equalValue = other.value === this.value;
|
|
33
|
+
return equalType && equalValue;
|
|
34
|
+
}
|
|
22
35
|
}
|
|
23
|
-
exports.EnumBase = EnumBase;
|
|
24
|
-
__decorate([
|
|
25
|
-
_decorators_1.Prop,
|
|
26
|
-
__metadata("design:type", Object)
|
|
27
|
-
], EnumBase.prototype, "value", void 0);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SnowflakeOpts } from "nodejs-snowflake";
|
|
2
|
+
import { IIdService } from "../interface";
|
|
3
|
+
export declare class SnowflakeIdService implements IIdService<string> {
|
|
4
|
+
private _snowflake;
|
|
5
|
+
constructor(options?: SnowflakeOpts);
|
|
6
|
+
validateValue(value: string): void;
|
|
7
|
+
generateValue(): string;
|
|
8
|
+
compareValue(v1: string, v2: string): boolean;
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Snowflake } from "nodejs-snowflake";
|
|
2
|
+
export class SnowflakeIdService {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this._snowflake = new Snowflake(options);
|
|
5
|
+
}
|
|
6
|
+
validateValue(value) { }
|
|
7
|
+
generateValue() {
|
|
8
|
+
return this._snowflake.getUniqueID().toString(16);
|
|
9
|
+
}
|
|
10
|
+
compareValue(v1, v2) {
|
|
11
|
+
return v1 === v2;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { v4, validate, version } from "uuid";
|
|
2
|
+
export class Uuid4Service {
|
|
3
|
+
validateValue(value) {
|
|
4
|
+
const isUuid = validate(value);
|
|
5
|
+
const isV4 = version(value) === 4;
|
|
6
|
+
if (!isUuid)
|
|
7
|
+
throw new Error("Id value is invalid for uuid type");
|
|
8
|
+
if (!isV4)
|
|
9
|
+
throw new Error("Version of uuid value must be 4");
|
|
10
|
+
}
|
|
11
|
+
generateValue() {
|
|
12
|
+
return v4();
|
|
13
|
+
}
|
|
14
|
+
compareValue(v1, v2) {
|
|
15
|
+
return v1 === v2;
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/core/id/id.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { IId, IIdService } from "./interface";
|
|
2
|
+
export declare class Id<T = any> implements IId<T> {
|
|
3
|
+
private _service;
|
|
4
|
+
private _value;
|
|
5
|
+
constructor(service: IIdService<T>, idOrValue?: Id<T> | T);
|
|
6
|
+
get value(): T;
|
|
7
|
+
equals(id: Id<T>): boolean;
|
|
6
8
|
}
|
package/dist/core/id/id.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export class Id {
|
|
2
|
+
constructor(service, idOrValue) {
|
|
3
|
+
this._service = service;
|
|
4
|
+
if (idOrValue) {
|
|
5
|
+
if (idOrValue instanceof Id)
|
|
6
|
+
this._value = idOrValue.value;
|
|
7
|
+
else
|
|
8
|
+
this._value = idOrValue;
|
|
9
|
+
}
|
|
10
|
+
else
|
|
11
|
+
this._value = this._service.generateValue();
|
|
12
|
+
this._service.validateValue(this._value);
|
|
7
13
|
}
|
|
8
14
|
get value() {
|
|
9
15
|
return this._value;
|
|
10
16
|
}
|
|
11
17
|
equals(id) {
|
|
12
|
-
return this._value
|
|
18
|
+
return this._service.compareValue(id._value, this._value);
|
|
13
19
|
}
|
|
14
20
|
}
|
|
15
|
-
exports.Id = Id;
|
package/dist/core/id/index.d.ts
CHANGED
package/dist/core/id/index.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./id"), exports);
|
|
18
|
-
__exportStar(require("./id-generator"), exports);
|
|
19
|
-
__exportStar(require("./built"), exports);
|
|
1
|
+
export * from "./interface";
|
|
2
|
+
export * from "./id";
|
|
3
|
+
export * from "./common";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface IId<T = any> {
|
|
2
|
+
value: T;
|
|
3
|
+
equals(id: IId<T>): boolean;
|
|
4
|
+
}
|
|
5
|
+
export type IdValueOf<T extends IId> = T extends IId<infer V> ? V : never;
|
|
6
|
+
export interface IIdService<T = any> {
|
|
7
|
+
validateValue(value: T): void;
|
|
8
|
+
generateValue(): T;
|
|
9
|
+
compareValue(v1: T, v2: T): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./model"), exports);
|
|
18
|
-
__exportStar(require("./id"), exports);
|
|
19
|
-
__exportStar(require("./error"), exports);
|
|
20
|
-
__exportStar(require("./aggregate"), exports);
|
|
21
|
-
__exportStar(require("./entity"), exports);
|
|
22
|
-
__exportStar(require("./enum"), exports);
|
|
23
|
-
__exportStar(require("./message"), exports);
|
|
24
|
-
__exportStar(require("./value-object"), exports);
|
|
25
|
-
__exportStar(require("./repository"), exports);
|
|
1
|
+
export * from "./model";
|
|
2
|
+
export * from "./id";
|
|
3
|
+
export * from "./aggregate";
|
|
4
|
+
export * from "./entity";
|
|
5
|
+
export * from "./enum";
|
|
6
|
+
export * from "./message";
|
|
7
|
+
export * from "./value-object";
|
|
8
|
+
export * from "./repository";
|