ddd-node 13.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.js → aggregate/event-sourced-aggregate.js} +28 -77
- 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 +4 -4
- package/dist/core/entity.js +2 -6
- package/dist/core/enum.d.ts +3 -2
- package/dist/core/enum.js +9 -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 +8 -5
- package/dist/core/message/command.js +10 -9
- package/dist/core/message/event.d.ts +9 -7
- package/dist/core/message/event.js +9 -11
- package/dist/core/message/index.js +3 -19
- package/dist/core/message/message.d.ts +6 -2
- 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 +7 -7
- package/dist/core/model/model-with-id.js +8 -12
- package/dist/core/model/model.d.ts +16 -14
- package/dist/core/model/model.js +24 -33
- package/dist/core/repository.d.ts +2 -2
- package/dist/core/repository.js +1 -2
- package/dist/core/value-object.d.ts +4 -3
- package/dist/core/value-object.js +8 -12
- package/dist/decorators/{aggregate → aggregate-es}/handle.d.ts +1 -1
- package/dist/decorators/aggregate-es/handle.js +9 -0
- package/dist/decorators/aggregate-es/index.js +2 -0
- package/dist/decorators/{aggregate → aggregate-es}/when.d.ts +1 -1
- 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 -2
- package/dist/decorators/model/enum.js +3 -7
- package/dist/decorators/model/index.js +5 -21
- package/dist/decorators/model/model-name.d.ts +2 -2
- package/dist/decorators/model/model-name.js +3 -7
- package/dist/decorators/model/prop.d.ts +1 -1
- package/dist/decorators/model/prop.js +3 -7
- package/dist/decorators/model/static.d.ts +3 -3
- package/dist/decorators/model/static.js +4 -8
- package/dist/decorators/model/validator.d.ts +3 -3
- package/dist/decorators/model/validator.js +3 -7
- 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.js +1 -17
- package/dist/meta/helpers/static-value.d.ts +1 -1
- package/dist/meta/helpers/static-value.js +1 -5
- package/dist/meta/id.metadata.d.ts +4 -4
- package/dist/meta/id.metadata.js +10 -10
- package/dist/meta/index.js +4 -20
- package/dist/meta/model.metadata.d.ts +1 -1
- package/dist/meta/model.metadata.js +22 -37
- 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 -71
- 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.js +0 -13
- package/dist/decorators/aggregate/index.js +0 -18
- package/dist/decorators/aggregate/when.js +0 -13
- /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;
|
|
@@ -1,57 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const entity_1 = require("./entity");
|
|
6
|
-
const _meta_1 = require("../meta/index");
|
|
7
|
-
class AggregateCore extends entity_1.EntityBase {
|
|
8
|
-
constructor(metadata, props) {
|
|
9
|
-
super(metadata, props);
|
|
10
|
-
this._version = metadata.version;
|
|
11
|
-
}
|
|
12
|
-
newEvent(eventClass, props) {
|
|
13
|
-
const eventSource = {
|
|
14
|
-
aggregate: this.modelName(),
|
|
15
|
-
id: this.id(),
|
|
16
|
-
version: this.getVersion(),
|
|
17
|
-
};
|
|
18
|
-
return eventClass.newEvent(eventSource, props);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
exports.AggregateCore = AggregateCore;
|
|
22
|
-
class AggregateBase extends AggregateCore {
|
|
23
|
-
constructor(metadata, props) {
|
|
24
|
-
super(metadata, props);
|
|
25
|
-
this._events = [];
|
|
26
|
-
}
|
|
27
|
-
static newAggregate(props, id) {
|
|
28
|
-
return new this({
|
|
29
|
-
id: this.id(id),
|
|
30
|
-
version: 0,
|
|
31
|
-
}, props);
|
|
32
|
-
}
|
|
33
|
-
getVersion() {
|
|
34
|
-
return this._version;
|
|
35
|
-
}
|
|
36
|
-
getEvents() {
|
|
37
|
-
return this._events;
|
|
38
|
-
}
|
|
39
|
-
recordEvent(param1, param2) {
|
|
40
|
-
let event;
|
|
41
|
-
if (typeof param1 === "function" && param2) {
|
|
42
|
-
event = this.newEvent(param1, param2);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
event = param1;
|
|
46
|
-
}
|
|
47
|
-
this._events.push(event);
|
|
48
|
-
}
|
|
49
|
-
clearEvents() {
|
|
50
|
-
this._events = [];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
exports.AggregateBase = AggregateBase;
|
|
54
|
-
class AggregateESBase extends AggregateCore {
|
|
1
|
+
import { getCommandHandlerMap, getEventApplierMap } from "../../meta";
|
|
2
|
+
import { toArray } from "../../utils";
|
|
3
|
+
import { AggregateBase } from "./base";
|
|
4
|
+
export class EventSourcedAggregateBase extends AggregateBase {
|
|
55
5
|
constructor(metadata, props) {
|
|
56
6
|
super(metadata, props);
|
|
57
7
|
this._handledCommands = [];
|
|
@@ -64,44 +14,44 @@ class AggregateESBase extends AggregateCore {
|
|
|
64
14
|
version: 0,
|
|
65
15
|
});
|
|
66
16
|
}
|
|
67
|
-
static fromStream(id,
|
|
17
|
+
static fromStream(id, pastEvents = []) {
|
|
68
18
|
const instance = this.newStream(id);
|
|
69
|
-
instance.applyPastEvents(
|
|
19
|
+
instance.applyPastEvents(pastEvents);
|
|
70
20
|
return instance;
|
|
71
21
|
}
|
|
72
|
-
static fromSnapshot(snapshot,
|
|
22
|
+
static fromSnapshot(snapshot, pastEventsAfterSnapshot = []) {
|
|
73
23
|
const { metadata, props } = snapshot;
|
|
74
24
|
const instance = new this(metadata, props);
|
|
75
|
-
instance.applyPastEvents(
|
|
25
|
+
instance.applyPastEvents(pastEventsAfterSnapshot);
|
|
76
26
|
return instance;
|
|
77
27
|
}
|
|
78
|
-
static
|
|
79
|
-
return (
|
|
28
|
+
static eventApplierMap() {
|
|
29
|
+
return getEventApplierMap(this.prototype);
|
|
80
30
|
}
|
|
81
|
-
static
|
|
82
|
-
return (
|
|
31
|
+
static commandHandlerMap() {
|
|
32
|
+
return getCommandHandlerMap(this.prototype);
|
|
83
33
|
}
|
|
84
34
|
getVersion() {
|
|
85
35
|
return this._version + this._pastEvents.length + this._events.length;
|
|
86
36
|
}
|
|
87
37
|
getPastEvents() {
|
|
88
|
-
return this._pastEvents;
|
|
38
|
+
return [...this._pastEvents];
|
|
89
39
|
}
|
|
90
40
|
getEvents() {
|
|
91
|
-
return this._events;
|
|
41
|
+
return [...this._events];
|
|
92
42
|
}
|
|
93
43
|
getHandledCommands() {
|
|
94
|
-
return this._handledCommands;
|
|
44
|
+
return [...this._handledCommands];
|
|
95
45
|
}
|
|
96
|
-
|
|
97
|
-
return this.constructor.
|
|
46
|
+
eventApplierMap() {
|
|
47
|
+
return this.constructor.eventApplierMap();
|
|
98
48
|
}
|
|
99
|
-
|
|
49
|
+
hasNewEvent() {
|
|
100
50
|
return Boolean(this._events.length);
|
|
101
51
|
}
|
|
102
52
|
getApplierForEvent(event) {
|
|
103
53
|
const eventName = event.modelName();
|
|
104
|
-
const applier = this.
|
|
54
|
+
const applier = this.eventApplierMap().get(eventName);
|
|
105
55
|
if (!applier)
|
|
106
56
|
throw new Error("Event applier not found");
|
|
107
57
|
return applier;
|
|
@@ -121,7 +71,7 @@ class AggregateESBase extends AggregateCore {
|
|
|
121
71
|
applier.call(this, event);
|
|
122
72
|
}
|
|
123
73
|
applyPastEvent(event) {
|
|
124
|
-
if (this.
|
|
74
|
+
if (this.hasNewEvent())
|
|
125
75
|
throw new Error();
|
|
126
76
|
this._applyEvent(event);
|
|
127
77
|
this._pastEvents.push(event);
|
|
@@ -140,23 +90,25 @@ class AggregateESBase extends AggregateCore {
|
|
|
140
90
|
this.applyEvent(event);
|
|
141
91
|
});
|
|
142
92
|
}
|
|
143
|
-
|
|
144
|
-
|
|
93
|
+
applyNewEvent(eventClass, props) {
|
|
94
|
+
this.applyEvent(this.newEvent(eventClass, props));
|
|
95
|
+
}
|
|
96
|
+
commandHandlerMap() {
|
|
97
|
+
return this.constructor.commandHandlerMap();
|
|
145
98
|
}
|
|
146
99
|
getHandlerForCommand(command) {
|
|
147
100
|
const commandName = command.modelName();
|
|
148
|
-
const handler = this.
|
|
101
|
+
const handler = this.commandHandlerMap().get(commandName);
|
|
149
102
|
if (!handler)
|
|
150
103
|
throw new Error("Command handler not found");
|
|
151
104
|
return handler;
|
|
152
105
|
}
|
|
153
106
|
handleCommand(command) {
|
|
154
107
|
const handler = this.getHandlerForCommand(command);
|
|
155
|
-
const events =
|
|
108
|
+
const events = toArray(handler.call(this, command));
|
|
156
109
|
events.forEach((event) => {
|
|
157
|
-
var _a;
|
|
158
110
|
event.setContext({
|
|
159
|
-
correlationId:
|
|
111
|
+
correlationId: command.getContext()?.correlationId,
|
|
160
112
|
causationId: command.id().value,
|
|
161
113
|
});
|
|
162
114
|
});
|
|
@@ -176,4 +128,3 @@ class AggregateESBase extends AggregateCore {
|
|
|
176
128
|
};
|
|
177
129
|
}
|
|
178
130
|
}
|
|
179
|
-
exports.AggregateESBase = AggregateESBase;
|
|
@@ -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,19 +1,19 @@
|
|
|
1
1
|
import { Class } from "type-fest";
|
|
2
2
|
import { Id } from "./id";
|
|
3
3
|
import { ModelWithId, Props, PropsOf } from "./model";
|
|
4
|
-
import { ClassStatic } from "../types
|
|
4
|
+
import { ClassStatic } from "../types";
|
|
5
5
|
export interface EntityMetadata {
|
|
6
6
|
readonly id: Id;
|
|
7
7
|
}
|
|
8
|
-
export declare
|
|
8
|
+
export declare class EntityBase<P extends Props> extends ModelWithId<P> {
|
|
9
9
|
constructor(metadata: EntityMetadata, props?: P);
|
|
10
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<
|
|
13
|
+
export type AnyEntity = EntityBase<Props>;
|
|
14
14
|
export interface EntityClass<T extends AnyEntity = AnyEntity, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof EntityBase<PropsOf<T>>> {
|
|
15
15
|
}
|
|
16
16
|
export interface EntityClassWithTypedConstructor<T extends AnyEntity = AnyEntity> extends EntityClass<T, ConstructorParameters<typeof EntityBase<PropsOf<T>>>> {
|
|
17
17
|
}
|
|
18
|
-
export interface EntityClassWithProps<P extends
|
|
18
|
+
export interface EntityClassWithProps<P extends Props> extends EntityClass<EntityBase<P>> {
|
|
19
19
|
}
|
package/dist/core/entity.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
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)
|
|
@@ -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,4 +1,4 @@
|
|
|
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;
|
|
@@ -9,7 +9,8 @@ export declare class EnumBase extends ModelBase<EnumProps> {
|
|
|
9
9
|
static values(): EnumBase[];
|
|
10
10
|
static parse<T extends EnumBase>(this: EnumClass<T>, providedValue: EnumValue): T | undefined;
|
|
11
11
|
constructor(value: EnumValue);
|
|
12
|
-
|
|
12
|
+
props(): EnumProps;
|
|
13
|
+
get value(): EnumValue;
|
|
13
14
|
equals<T extends EnumBase>(other: T): boolean;
|
|
14
15
|
}
|
|
15
16
|
export interface EnumClass<T extends EnumBase = EnumBase> extends Class<T>, ClassStatic<typeof EnumBase> {
|
package/dist/core/enum.js
CHANGED
|
@@ -1,23 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.EnumBase = void 0;
|
|
13
|
-
const _decorators_1 = require("../decorators/index");
|
|
14
|
-
const model_1 = require("./model");
|
|
15
|
-
class EnumBase extends model_1.ModelBase {
|
|
1
|
+
import { ModelBase } from "./model";
|
|
2
|
+
export class EnumBase extends ModelBase {
|
|
16
3
|
static values() {
|
|
17
4
|
return Array.from(this.ownStaticValues().values()).map((staticValue) => staticValue.value);
|
|
18
5
|
}
|
|
19
6
|
static parse(providedValue) {
|
|
20
|
-
var _a;
|
|
21
7
|
let key;
|
|
22
8
|
this.ownStaticValues().forEach((staticValue, staticValueKey) => {
|
|
23
9
|
if (staticValue.value instanceof this) {
|
|
@@ -28,21 +14,22 @@ class EnumBase extends model_1.ModelBase {
|
|
|
28
14
|
}
|
|
29
15
|
});
|
|
30
16
|
if (key)
|
|
31
|
-
return
|
|
17
|
+
return this.ownStaticValues().get(key)?.value;
|
|
32
18
|
return undefined;
|
|
33
19
|
}
|
|
34
20
|
constructor(value) {
|
|
35
21
|
super();
|
|
36
22
|
this.initializeProps({ value });
|
|
37
23
|
}
|
|
24
|
+
props() {
|
|
25
|
+
return super.props();
|
|
26
|
+
}
|
|
27
|
+
get value() {
|
|
28
|
+
return this._props.value;
|
|
29
|
+
}
|
|
38
30
|
equals(other) {
|
|
39
31
|
const equalType = other instanceof this.constructor;
|
|
40
32
|
const equalValue = other.value === this.value;
|
|
41
33
|
return equalType && equalValue;
|
|
42
34
|
}
|
|
43
35
|
}
|
|
44
|
-
exports.EnumBase = EnumBase;
|
|
45
|
-
__decorate([
|
|
46
|
-
(0, _decorators_1.Prop)(),
|
|
47
|
-
__metadata("design:type", Object)
|
|
48
|
-
], 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";
|