ddd-node 12.2.2 → 13.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/dist/core/aggregate.d.ts +15 -11
- package/dist/core/aggregate.js +6 -6
- package/dist/core/entity.d.ts +10 -7
- package/dist/core/entity.js +1 -1
- package/dist/core/enum.d.ts +12 -9
- package/dist/core/enum.js +28 -4
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +0 -1
- package/dist/core/message/command.d.ts +8 -6
- package/dist/core/message/event.d.ts +9 -7
- package/dist/core/message/message.d.ts +3 -3
- package/dist/core/model/model-with-id.d.ts +4 -4
- package/dist/core/model/model-with-id.js +5 -5
- package/dist/core/model/model.d.ts +23 -13
- package/dist/core/model/model.js +45 -26
- package/dist/core/value-object.d.ts +8 -6
- package/dist/decorators/aggregate/handle.d.ts +2 -0
- package/dist/decorators/aggregate/handle.js +13 -0
- package/dist/decorators/aggregate/index.d.ts +2 -0
- package/dist/decorators/aggregate/index.js +18 -0
- package/dist/decorators/aggregate/when.d.ts +2 -0
- package/dist/decorators/aggregate/when.js +13 -0
- package/dist/decorators/id.d.ts +1 -1
- package/dist/decorators/id.js +3 -3
- package/dist/decorators/model/enum.d.ts +2 -0
- package/dist/decorators/model/enum.js +10 -0
- package/dist/decorators/model/index.d.ts +5 -0
- package/dist/decorators/model/index.js +21 -0
- package/dist/decorators/model/model-name.d.ts +2 -0
- package/dist/decorators/model/model-name.js +10 -0
- package/dist/decorators/model/prop.d.ts +2 -0
- package/dist/decorators/model/prop.js +10 -0
- package/dist/decorators/model/static.d.ts +3 -0
- package/dist/decorators/model/static.js +20 -0
- package/dist/decorators/model/validator.d.ts +3 -0
- package/dist/decorators/model/validator.js +11 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/{core/metadata → meta}/aggregate.metadata.d.ts +1 -2
- package/dist/{core/metadata → meta}/aggregate.metadata.js +1 -0
- package/dist/meta/helpers/index.d.ts +1 -0
- package/dist/meta/helpers/index.js +17 -0
- package/dist/meta/helpers/static-value.d.ts +7 -0
- package/dist/meta/helpers/static-value.js +15 -0
- package/dist/{core/metadata → meta}/id.metadata.d.ts +1 -1
- package/dist/{core/metadata → meta}/index.d.ts +1 -0
- package/dist/{core/metadata → meta}/index.js +1 -0
- package/dist/meta/model.metadata.d.ts +15 -0
- package/dist/meta/model.metadata.js +97 -0
- package/package.json +1 -1
- package/tsconfig.json +1 -0
- package/dist/core/metadata/model.metadata.d.ts +0 -13
- package/dist/core/metadata/model.metadata.js +0 -46
- package/dist/decorators/aggregate.d.ts +0 -3
- package/dist/decorators/aggregate.js +0 -22
- package/dist/decorators/model.d.ts +0 -13
- package/dist/decorators/model.js +0 -73
- /package/dist/{core/metadata → meta}/id.metadata.js +0 -0
package/dist/core/aggregate.d.ts
CHANGED
|
@@ -3,19 +3,19 @@ import { Class } from "type-fest";
|
|
|
3
3
|
import { EntityBase, EntityMetadata } from "./entity";
|
|
4
4
|
import { Id } from "./id";
|
|
5
5
|
import { AnyCommand, AnyEvent, EventClass, EventClassWithTypedConstructor } from "./message";
|
|
6
|
-
import { PropsOf } from "./model
|
|
6
|
+
import { Props, PropsOf } from "./model";
|
|
7
7
|
export interface AggregateBaseMetadata extends EntityMetadata {
|
|
8
8
|
version: number;
|
|
9
9
|
}
|
|
10
|
-
export declare abstract class AggregateCore<
|
|
10
|
+
export declare abstract class AggregateCore<P extends Props> extends EntityBase<P> {
|
|
11
11
|
protected readonly _version: number;
|
|
12
|
-
constructor(metadata: AggregateBaseMetadata, props?:
|
|
12
|
+
constructor(metadata: AggregateBaseMetadata, props?: P);
|
|
13
13
|
abstract getVersion(): number;
|
|
14
14
|
protected newEvent<E extends AnyEvent>(eventClass: EventClassWithTypedConstructor<E>, props: PropsOf<E>): E;
|
|
15
15
|
}
|
|
16
|
-
export declare abstract class AggregateBase<
|
|
16
|
+
export declare abstract class AggregateBase<P extends Props> extends AggregateCore<P> {
|
|
17
17
|
private _events;
|
|
18
|
-
constructor(metadata: AggregateBaseMetadata, props:
|
|
18
|
+
constructor(metadata: AggregateBaseMetadata, props: P);
|
|
19
19
|
static newAggregate<T extends AnyAggregate>(this: AggregateClassWithTypedConstructor<T>, props: PropsOf<T>, id?: Id): T;
|
|
20
20
|
getVersion(): number;
|
|
21
21
|
getEvents(): AnyEvent[];
|
|
@@ -29,11 +29,11 @@ export interface Snapshot<T extends AnyAggregateES> {
|
|
|
29
29
|
metadata: SnapshotMetadata;
|
|
30
30
|
props: PropsOf<T>;
|
|
31
31
|
}
|
|
32
|
-
export declare abstract class AggregateESBase<
|
|
32
|
+
export declare abstract class AggregateESBase<P extends Props> extends AggregateCore<P> {
|
|
33
33
|
private _handledCommands;
|
|
34
34
|
private _pastEvents;
|
|
35
35
|
private _events;
|
|
36
|
-
constructor(metadata: AggregateBaseMetadata, props?:
|
|
36
|
+
constructor(metadata: AggregateBaseMetadata, props?: P);
|
|
37
37
|
static newStream<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, id?: Id): T;
|
|
38
38
|
static fromStream<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, id: Id, events?: AnyEvent[]): T;
|
|
39
39
|
static fromSnapshot<T extends AnyAggregateES>(this: AggregateESClassWithTypedConstructor<T>, snapshot: Snapshot<T>, events?: AnyEvent[]): T;
|
|
@@ -58,10 +58,14 @@ export declare abstract class AggregateESBase<Props extends object> extends Aggr
|
|
|
58
58
|
snap(): Snapshot<this>;
|
|
59
59
|
}
|
|
60
60
|
export type AnyAggregate = AggregateBase<any>;
|
|
61
|
-
export
|
|
62
|
-
|
|
61
|
+
export interface AggregateClass<T extends AnyAggregate = AnyAggregate, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof AggregateBase<PropsOf<T>>> {
|
|
62
|
+
}
|
|
63
|
+
export interface AggregateClassWithTypedConstructor<T extends AnyAggregate = AnyAggregate> extends AggregateClass<T, ConstructorParameters<typeof AggregateBase<PropsOf<T>>>> {
|
|
64
|
+
}
|
|
63
65
|
export type AnyAggregateES = AggregateESBase<any>;
|
|
64
|
-
export
|
|
65
|
-
|
|
66
|
+
export interface AggregateESClass<T extends AnyAggregateES = AnyAggregateES, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof AggregateESBase<PropsOf<T>>> {
|
|
67
|
+
}
|
|
68
|
+
export interface AggregateESClassWithTypedConstructor<T extends AnyAggregateES = AnyAggregateES> extends AggregateESClass<T, ConstructorParameters<typeof AggregateESBase<PropsOf<T>>>> {
|
|
69
|
+
}
|
|
66
70
|
export type EventApplier<T extends AnyEvent = AnyEvent> = (event: T) => void;
|
|
67
71
|
export type CommandHandler<T extends AnyCommand = AnyCommand, U extends AnyEvent | AnyEvent[] = AnyEvent> = (command: T) => U;
|
package/dist/core/aggregate.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AggregateESBase = exports.AggregateBase = exports.AggregateCore = void 0;
|
|
4
4
|
const _utils_1 = require("../utils/index");
|
|
5
5
|
const entity_1 = require("./entity");
|
|
6
|
-
const
|
|
6
|
+
const _meta_1 = require("../meta/index");
|
|
7
7
|
class AggregateCore extends entity_1.EntityBase {
|
|
8
8
|
constructor(metadata, props) {
|
|
9
9
|
super(metadata, props);
|
|
@@ -12,7 +12,7 @@ class AggregateCore extends entity_1.EntityBase {
|
|
|
12
12
|
newEvent(eventClass, props) {
|
|
13
13
|
const eventSource = {
|
|
14
14
|
aggregate: this.modelName(),
|
|
15
|
-
id: this.
|
|
15
|
+
id: this.id(),
|
|
16
16
|
version: this.getVersion(),
|
|
17
17
|
};
|
|
18
18
|
return eventClass.newEvent(eventSource, props);
|
|
@@ -76,10 +76,10 @@ class AggregateESBase extends AggregateCore {
|
|
|
76
76
|
return instance;
|
|
77
77
|
}
|
|
78
78
|
static eventAppliersMap() {
|
|
79
|
-
return (0,
|
|
79
|
+
return (0, _meta_1.getEventAppliersMap)(this.prototype);
|
|
80
80
|
}
|
|
81
81
|
static commandHandlersMap() {
|
|
82
|
-
return (0,
|
|
82
|
+
return (0, _meta_1.getCommandHandlersMap)(this.prototype);
|
|
83
83
|
}
|
|
84
84
|
getVersion() {
|
|
85
85
|
return this._version + this._pastEvents.length + this._events.length;
|
|
@@ -157,7 +157,7 @@ class AggregateESBase extends AggregateCore {
|
|
|
157
157
|
var _a;
|
|
158
158
|
event.setContext({
|
|
159
159
|
correlationId: (_a = command.getContext()) === null || _a === void 0 ? void 0 : _a.correlationId,
|
|
160
|
-
causationId: command.
|
|
160
|
+
causationId: command.id().value,
|
|
161
161
|
});
|
|
162
162
|
});
|
|
163
163
|
this.applyEvents(events);
|
|
@@ -169,7 +169,7 @@ class AggregateESBase extends AggregateCore {
|
|
|
169
169
|
throw new Error();
|
|
170
170
|
return {
|
|
171
171
|
metadata: {
|
|
172
|
-
id: this.
|
|
172
|
+
id: this.id(),
|
|
173
173
|
version: this.getVersion(),
|
|
174
174
|
},
|
|
175
175
|
props: this.props(),
|
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";
|
|
3
|
+
import { ModelWithId, Props, PropsOf } from "./model";
|
|
4
4
|
import { ClassStatic } from "../types/index";
|
|
5
5
|
export interface EntityMetadata {
|
|
6
6
|
readonly id: Id;
|
|
7
7
|
}
|
|
8
|
-
export declare abstract class EntityBase<
|
|
9
|
-
constructor(metadata: EntityMetadata, props?:
|
|
10
|
-
static
|
|
8
|
+
export declare abstract 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
13
|
export type AnyEntity = EntityBase<object>;
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
export
|
|
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 object> extends EntityClass<EntityBase<P>> {
|
|
19
|
+
}
|
package/dist/core/entity.js
CHANGED
package/dist/core/enum.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { Class } from "type-fest";
|
|
2
|
-
import { ValueObjectBase } from "./value-object";
|
|
3
1
|
import { ClassStatic } from "../types/index";
|
|
2
|
+
import { Class } from "type-fest";
|
|
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
|
+
}
|
|
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
|
+
value: EnumValue;
|
|
13
|
+
equals<T extends EnumBase>(other: T): boolean;
|
|
7
14
|
}
|
|
8
|
-
export
|
|
9
|
-
constructor(value: V);
|
|
10
|
-
value: V;
|
|
15
|
+
export interface EnumClass<T extends EnumBase = EnumBase> extends Class<T>, ClassStatic<typeof EnumBase> {
|
|
11
16
|
}
|
|
12
|
-
export type ValueOf<T extends EnumBase> = T extends EnumBase<infer V extends EnumValue> ? V : never;
|
|
13
|
-
export type EnumClass<T extends EnumBase = EnumBase> = Class<T> & ClassStatic<typeof EnumBase>;
|
package/dist/core/enum.js
CHANGED
|
@@ -11,14 +11,38 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.EnumBase = void 0;
|
|
13
13
|
const _decorators_1 = require("../decorators/index");
|
|
14
|
-
const
|
|
15
|
-
class EnumBase extends
|
|
14
|
+
const model_1 = require("./model");
|
|
15
|
+
class EnumBase extends model_1.ModelBase {
|
|
16
|
+
static values() {
|
|
17
|
+
return Array.from(this.ownStaticValues().values()).map((staticValue) => staticValue.value);
|
|
18
|
+
}
|
|
19
|
+
static parse(providedValue) {
|
|
20
|
+
var _a;
|
|
21
|
+
let key;
|
|
22
|
+
this.ownStaticValues().forEach((staticValue, staticValueKey) => {
|
|
23
|
+
if (staticValue.value instanceof this) {
|
|
24
|
+
const staticEnum = staticValue.value;
|
|
25
|
+
if (staticEnum.value === providedValue && !key) {
|
|
26
|
+
key = staticValueKey;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
if (key)
|
|
31
|
+
return (_a = this.ownStaticValues().get(key)) === null || _a === void 0 ? void 0 : _a.value;
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
16
34
|
constructor(value) {
|
|
17
|
-
super(
|
|
35
|
+
super();
|
|
36
|
+
this.initializeProps({ value });
|
|
37
|
+
}
|
|
38
|
+
equals(other) {
|
|
39
|
+
const equalType = other instanceof this.constructor;
|
|
40
|
+
const equalValue = other.value === this.value;
|
|
41
|
+
return equalType && equalValue;
|
|
18
42
|
}
|
|
19
43
|
}
|
|
20
44
|
exports.EnumBase = EnumBase;
|
|
21
45
|
__decorate([
|
|
22
|
-
_decorators_1.Prop,
|
|
46
|
+
(0, _decorators_1.Prop)(),
|
|
23
47
|
__metadata("design:type", Object)
|
|
24
48
|
], EnumBase.prototype, "value", void 0);
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./metadata"), exports);
|
|
18
17
|
__exportStar(require("./model"), exports);
|
|
19
18
|
__exportStar(require("./id"), exports);
|
|
20
19
|
__exportStar(require("./error"), exports);
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
import { ClassStatic } from "../../types/index";
|
|
1
2
|
import { Class } from "type-fest";
|
|
2
|
-
import { PropsOf } from "../model";
|
|
3
|
+
import { Props, PropsOf } from "../model";
|
|
3
4
|
import { MessageBase, MessageContext, MessageMetadata } from "./message";
|
|
4
|
-
import { ClassStatic } from "../../types/index";
|
|
5
5
|
export interface CommandMetadata extends MessageMetadata {
|
|
6
6
|
}
|
|
7
|
-
export declare abstract class CommandBase<
|
|
8
|
-
constructor(metadata: CommandMetadata, props:
|
|
7
|
+
export declare abstract class CommandBase<P extends Props> extends MessageBase<P> {
|
|
8
|
+
constructor(metadata: CommandMetadata, props: P);
|
|
9
9
|
static newCommand<T extends AnyCommand>(this: CommandClassWithTypedConstructor<T>, props: PropsOf<T>, context?: MessageContext): T;
|
|
10
10
|
}
|
|
11
11
|
export type AnyCommand = CommandBase<any>;
|
|
12
|
-
export
|
|
13
|
-
|
|
12
|
+
export interface CommandClass<T extends AnyCommand = AnyCommand, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof CommandBase<PropsOf<T>>> {
|
|
13
|
+
}
|
|
14
|
+
export interface CommandClassWithTypedConstructor<T extends AnyCommand = AnyCommand> extends CommandClass<T, ConstructorParameters<typeof CommandBase<PropsOf<T>>>> {
|
|
15
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { ClassStatic } from "../../types/index";
|
|
1
2
|
import { Class } from "type-fest";
|
|
2
3
|
import { Id } from "../id";
|
|
3
|
-
import { PropsOf } from "../model";
|
|
4
|
+
import { Props, PropsOf } from "../model";
|
|
4
5
|
import { MessageBase, MessageContext, MessageMetadata } from "./message";
|
|
5
|
-
import { ClassStatic } from "../../types/index";
|
|
6
6
|
export type EventSource = Readonly<{
|
|
7
7
|
aggregate: string;
|
|
8
8
|
id: Id;
|
|
@@ -11,17 +11,19 @@ export type EventSource = Readonly<{
|
|
|
11
11
|
export interface EventMetadata extends MessageMetadata {
|
|
12
12
|
source: EventSource;
|
|
13
13
|
}
|
|
14
|
-
export declare abstract class EventBase<
|
|
14
|
+
export declare abstract class EventBase<P extends Props, R = any> extends MessageBase<P> {
|
|
15
15
|
private readonly _source;
|
|
16
|
-
constructor(metadata: EventMetadata, props:
|
|
16
|
+
constructor(metadata: EventMetadata, props: P);
|
|
17
17
|
static newEvent<T extends AnyEvent>(this: EventClassWithTypedConstructor<T>, source: EventSource, props: PropsOf<T>, context?: MessageContext): T;
|
|
18
18
|
getSource(): Readonly<{
|
|
19
19
|
aggregate: string;
|
|
20
20
|
id: Id;
|
|
21
21
|
version: number;
|
|
22
22
|
}>;
|
|
23
|
-
getRaw():
|
|
23
|
+
getRaw(): R | null;
|
|
24
24
|
}
|
|
25
25
|
export type AnyEvent = EventBase<any>;
|
|
26
|
-
export
|
|
27
|
-
|
|
26
|
+
export interface EventClass<T extends AnyEvent = AnyEvent, Arguments extends unknown[] = any[]> extends Class<T, Arguments>, ClassStatic<typeof EventBase<PropsOf<T>>> {
|
|
27
|
+
}
|
|
28
|
+
export interface EventClassWithTypedConstructor<T extends AnyEvent = AnyEvent> extends EventClass<T, ConstructorParameters<typeof EventBase<PropsOf<T>>>> {
|
|
29
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Id } from "../id";
|
|
2
|
-
import { ModelWithId } from "../model";
|
|
2
|
+
import { ModelWithId, Props } from "../model";
|
|
3
3
|
export interface MessageContext {
|
|
4
4
|
correlationId?: string;
|
|
5
5
|
causationId?: string;
|
|
@@ -9,10 +9,10 @@ export interface MessageMetadata {
|
|
|
9
9
|
readonly timestamp: number;
|
|
10
10
|
context?: MessageContext;
|
|
11
11
|
}
|
|
12
|
-
export declare abstract class MessageBase<
|
|
12
|
+
export declare abstract class MessageBase<P extends Props> extends ModelWithId<P> {
|
|
13
13
|
private readonly _timestamp;
|
|
14
14
|
private _context?;
|
|
15
|
-
protected constructor(metadata: MessageMetadata, props:
|
|
15
|
+
protected constructor(metadata: MessageMetadata, props: P);
|
|
16
16
|
getTimestamp(): number;
|
|
17
17
|
getContext(): MessageContext | undefined;
|
|
18
18
|
setContext(context: Partial<MessageContext>): void;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Id, IdGeneratorBase } from "../id";
|
|
2
|
-
import { ModelBase } from "./model";
|
|
3
|
-
export declare abstract class ModelWithId<
|
|
2
|
+
import { ModelBase, Props } from "./model";
|
|
3
|
+
export declare abstract class ModelWithId<P extends Props> extends ModelBase<P> {
|
|
4
4
|
protected readonly _id: Id;
|
|
5
5
|
constructor(id: Id);
|
|
6
|
-
static
|
|
6
|
+
static idGenerator(this: any): IdGeneratorBase;
|
|
7
7
|
static id(id?: Id): Id;
|
|
8
|
-
|
|
8
|
+
id(): Id;
|
|
9
9
|
hasId(id: Id): boolean;
|
|
10
10
|
rawId(): string;
|
|
11
11
|
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ModelWithId = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
4
5
|
const id_1 = require("../id");
|
|
5
|
-
const id_metadata_1 = require("../metadata/id.metadata");
|
|
6
6
|
const model_1 = require("./model");
|
|
7
7
|
class ModelWithId extends model_1.ModelBase {
|
|
8
8
|
constructor(id) {
|
|
9
9
|
super();
|
|
10
10
|
this._id = id;
|
|
11
11
|
}
|
|
12
|
-
static
|
|
13
|
-
return (0,
|
|
12
|
+
static idGenerator() {
|
|
13
|
+
return (0, _meta_1.getIdGenerator)(this) || new id_1.Uuid4Generator();
|
|
14
14
|
}
|
|
15
15
|
static id(id) {
|
|
16
|
-
const generator = this.
|
|
16
|
+
const generator = this.idGenerator();
|
|
17
17
|
return id ? generator.fromId(id) : generator.newId();
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
id() {
|
|
20
20
|
return this._id;
|
|
21
21
|
}
|
|
22
22
|
hasId(id) {
|
|
@@ -1,25 +1,35 @@
|
|
|
1
1
|
import { ClassStatic } from "../../types/index";
|
|
2
2
|
import { Class } from "type-fest";
|
|
3
|
-
|
|
4
|
-
export
|
|
3
|
+
export type PropKey = string | symbol;
|
|
4
|
+
export interface Props {
|
|
5
|
+
[key: PropKey]: any;
|
|
6
|
+
}
|
|
7
|
+
export declare class ModelBase<P extends Props> {
|
|
5
8
|
static readonly EMPTY_PROPS: {};
|
|
6
|
-
|
|
7
|
-
private _propKeys;
|
|
8
|
-
protected _props: Props;
|
|
9
|
-
static isMutable(): boolean;
|
|
9
|
+
protected _props: P;
|
|
10
10
|
static isModel(model: any): model is AnyModel;
|
|
11
|
+
static mutable(): boolean;
|
|
12
|
+
static modelName<T extends AnyModel>(this: ModelClass<T>): string;
|
|
13
|
+
static propsValidator<T extends AnyModel>(this: ModelClass<T>): import("../../meta/index").PropsValidator<T> | undefined;
|
|
14
|
+
static ownStaticValues<T extends AnyModel>(this: ModelClass<T>): Map<string | symbol, import("../../meta/index").StaticValue<T>>;
|
|
15
|
+
static ownPropsMap(): import("../../meta/index").PropsMap<ModelBase<any>>;
|
|
16
|
+
static propsMap(): import("../../meta/index").PropsMap<ModelBase<any>>;
|
|
11
17
|
constructor();
|
|
12
18
|
private redefineModel;
|
|
19
|
+
protected get _prototype(): object;
|
|
20
|
+
protected get _type(): ModelClass<this>;
|
|
13
21
|
isMutable(): boolean;
|
|
14
|
-
getPropKeys(): Set<string | symbol>;
|
|
15
|
-
validateProps(props: Props): void;
|
|
16
|
-
validate(): void;
|
|
17
|
-
getModelMetadata(): ModelMetadata<this>;
|
|
18
22
|
modelName(): string;
|
|
23
|
+
propsValidator(): import("../../meta/index").PropsValidator<this> | undefined;
|
|
24
|
+
ownPropsMap(): import("../../meta/index").PropsMap<ModelBase<any>>;
|
|
25
|
+
propsMap(): import("../../meta/index").PropsMap<ModelBase<any>>;
|
|
26
|
+
validateProps(props: P): void;
|
|
27
|
+
validate(): void;
|
|
19
28
|
props(): any;
|
|
20
|
-
protected initializeProps(props:
|
|
29
|
+
protected initializeProps(props: P): void;
|
|
21
30
|
}
|
|
22
31
|
export type AnyModel = ModelBase<object>;
|
|
23
|
-
export type PropsOf<T extends AnyModel> = T extends ModelBase<infer P extends
|
|
24
|
-
export
|
|
32
|
+
export type PropsOf<T extends AnyModel> = T extends ModelBase<infer P extends Props> ? P : never;
|
|
33
|
+
export interface ModelClass<T extends AnyModel = AnyModel> extends Class<T>, ClassStatic<typeof ModelBase<PropsOf<T>>> {
|
|
34
|
+
}
|
|
25
35
|
export type EmptyProps = {};
|
package/dist/core/model/model.js
CHANGED
|
@@ -4,60 +4,78 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ModelBase = void 0;
|
|
7
|
+
const _meta_1 = require("../../meta/index");
|
|
7
8
|
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
-
const model_metadata_1 = require("../metadata/model.metadata");
|
|
9
9
|
class ModelBase {
|
|
10
|
-
static isMutable() {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
10
|
static isModel(model) {
|
|
14
11
|
return model instanceof ModelBase;
|
|
15
12
|
}
|
|
13
|
+
static mutable() {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
static modelName() {
|
|
17
|
+
return (0, _meta_1.getModelName)(this);
|
|
18
|
+
}
|
|
19
|
+
static propsValidator() {
|
|
20
|
+
return (0, _meta_1.getPropsValidator)(this);
|
|
21
|
+
}
|
|
22
|
+
static ownStaticValues() {
|
|
23
|
+
return (0, _meta_1.getOwnStaticValues)(this);
|
|
24
|
+
}
|
|
25
|
+
static ownPropsMap() {
|
|
26
|
+
return (0, _meta_1.getOwnPropsMap)(this.prototype);
|
|
27
|
+
}
|
|
28
|
+
static propsMap() {
|
|
29
|
+
return (0, _meta_1.getPropsMap)(this.prototype);
|
|
30
|
+
}
|
|
16
31
|
constructor() {
|
|
17
32
|
this._props = ModelBase.EMPTY_PROPS;
|
|
18
33
|
this.redefineModel();
|
|
19
34
|
}
|
|
20
35
|
redefineModel() {
|
|
21
|
-
this.
|
|
22
|
-
Object.defineProperty(this,
|
|
36
|
+
this.propsMap().forEach((propKeyTarget, key) => {
|
|
37
|
+
Object.defineProperty(this, key, {
|
|
23
38
|
configurable: true,
|
|
24
39
|
enumerable: true,
|
|
25
40
|
get() {
|
|
26
41
|
var _a;
|
|
27
|
-
return (_a = this._props) === null || _a === void 0 ? void 0 : _a[
|
|
42
|
+
return (_a = this._props) === null || _a === void 0 ? void 0 : _a[propKeyTarget];
|
|
28
43
|
},
|
|
29
44
|
set() {
|
|
30
|
-
throw new Error(`
|
|
45
|
+
throw new Error(`Cannot set "${key.toString()}"`);
|
|
31
46
|
},
|
|
32
47
|
});
|
|
33
48
|
});
|
|
34
49
|
}
|
|
50
|
+
get _prototype() {
|
|
51
|
+
return Reflect.getPrototypeOf(this);
|
|
52
|
+
}
|
|
53
|
+
get _type() {
|
|
54
|
+
return this.constructor;
|
|
55
|
+
}
|
|
35
56
|
isMutable() {
|
|
36
|
-
return this.
|
|
57
|
+
return this._type.mutable();
|
|
37
58
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return this.
|
|
59
|
+
modelName() {
|
|
60
|
+
return this._type.modelName();
|
|
61
|
+
}
|
|
62
|
+
propsValidator() {
|
|
63
|
+
return this._type.propsValidator();
|
|
64
|
+
}
|
|
65
|
+
ownPropsMap() {
|
|
66
|
+
return this._type.ownPropsMap();
|
|
67
|
+
}
|
|
68
|
+
propsMap() {
|
|
69
|
+
return this._type.propsMap();
|
|
43
70
|
}
|
|
44
71
|
validateProps(props) {
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
47
|
-
|
|
72
|
+
const propsValidator = this.propsValidator();
|
|
73
|
+
if (propsValidator)
|
|
74
|
+
propsValidator(props);
|
|
48
75
|
}
|
|
49
76
|
validate() {
|
|
50
77
|
this.validateProps(this._props);
|
|
51
78
|
}
|
|
52
|
-
getModelMetadata() {
|
|
53
|
-
if (!this._metadata) {
|
|
54
|
-
this._metadata = (0, model_metadata_1.getModelMetadata)(this.constructor);
|
|
55
|
-
}
|
|
56
|
-
return this._metadata;
|
|
57
|
-
}
|
|
58
|
-
modelName() {
|
|
59
|
-
return this.getModelMetadata().name;
|
|
60
|
-
}
|
|
61
79
|
props() {
|
|
62
80
|
return lodash_1.default.cloneDeepWith(this._props, (value) => {
|
|
63
81
|
if (ModelBase.isModel(value)) {
|
|
@@ -66,6 +84,7 @@ class ModelBase {
|
|
|
66
84
|
}
|
|
67
85
|
});
|
|
68
86
|
}
|
|
87
|
+
// initializeProps must be call in leaf class, it cannot be call in a extendable model class
|
|
69
88
|
initializeProps(props) {
|
|
70
89
|
if (this._props !== ModelBase.EMPTY_PROPS)
|
|
71
90
|
throw new Error("Props is initialized");
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Class } from "type-fest";
|
|
2
|
-
import { ModelBase, PropsOf } from "./model";
|
|
2
|
+
import { ModelBase, Props, PropsOf } from "./model";
|
|
3
3
|
import { ClassStatic } from "../types/index";
|
|
4
|
-
export declare abstract class ValueObjectBase<
|
|
5
|
-
constructor(props:
|
|
4
|
+
export declare abstract class ValueObjectBase<P extends Props> extends ModelBase<P> {
|
|
5
|
+
constructor(props: P);
|
|
6
6
|
equals<V extends AnyValueObject>(vo: V): boolean;
|
|
7
|
-
with(props: Partial<
|
|
7
|
+
with(props: Partial<P>): this;
|
|
8
8
|
}
|
|
9
9
|
export type AnyValueObject = ValueObjectBase<any>;
|
|
10
|
-
export
|
|
11
|
-
|
|
10
|
+
export interface ValueObjectClass<T extends AnyValueObject = AnyValueObject> extends Class<T>, ClassStatic<typeof ValueObjectBase<PropsOf<T>>> {
|
|
11
|
+
}
|
|
12
|
+
export interface ValueObjectClassWithTypedConstructor<T extends AnyValueObject = AnyValueObject> extends Class<T, ConstructorParameters<typeof ValueObjectBase<PropsOf<T>>>> {
|
|
13
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { AnyCommand, CommandClass, CommandHandler } from "../../core/index";
|
|
2
|
+
export declare const Handle: <T extends AnyCommand>(commandClass: CommandClass<T, any[]>) => <U extends CommandHandler<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Handle = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
5
|
+
const Handle = (commandClass) => {
|
|
6
|
+
return (target, propertyKey, descriptor) => {
|
|
7
|
+
if (typeof descriptor.value === "function") {
|
|
8
|
+
const commandName = (0, _meta_1.getModelName)(commandClass);
|
|
9
|
+
(0, _meta_1.defineCommandHandler)(target, commandName, descriptor.value);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
exports.Handle = Handle;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
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("./handle"), exports);
|
|
18
|
+
__exportStar(require("./when"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.When = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
5
|
+
const When = (eventClass) => {
|
|
6
|
+
return (target, propertyKey, descriptor) => {
|
|
7
|
+
if (typeof descriptor.value === "function") {
|
|
8
|
+
const eventName = (0, _meta_1.getModelName)(eventClass);
|
|
9
|
+
(0, _meta_1.defineEventApplier)(target, eventName, descriptor.value);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
exports.When = When;
|
package/dist/decorators/id.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { AnyModelWithId, IdGeneratorBase, ModelClass } from "../core/index";
|
|
2
|
-
export declare const UseId: (
|
|
2
|
+
export declare const UseId: (generator: IdGeneratorBase) => <T extends AnyModelWithId>(target: ModelClass<T>) => void;
|
package/dist/decorators/id.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UseId = void 0;
|
|
4
|
-
const
|
|
5
|
-
const UseId = (
|
|
4
|
+
const _meta_1 = require("../meta/index");
|
|
5
|
+
const UseId = (generator) => {
|
|
6
6
|
return (target) => {
|
|
7
|
-
(0,
|
|
7
|
+
(0, _meta_1.defineIdGenerator)(target, generator);
|
|
8
8
|
};
|
|
9
9
|
};
|
|
10
10
|
exports.UseId = UseId;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Enum = void 0;
|
|
4
|
+
const static_1 = require("./static");
|
|
5
|
+
const Enum = (value) => {
|
|
6
|
+
return (target, key) => {
|
|
7
|
+
(0, static_1.Static)(() => new target(value))(target, key);
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
exports.Enum = Enum;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
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("./prop"), exports);
|
|
18
|
+
__exportStar(require("./model-name"), exports);
|
|
19
|
+
__exportStar(require("./validator"), exports);
|
|
20
|
+
__exportStar(require("./static"), exports);
|
|
21
|
+
__exportStar(require("./enum"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModelName = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
5
|
+
const ModelName = (name) => {
|
|
6
|
+
return (target) => {
|
|
7
|
+
(0, _meta_1.setModelName)(target, name);
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
exports.ModelName = ModelName;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Prop = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
5
|
+
const Prop = (propKey) => {
|
|
6
|
+
return (target, key) => {
|
|
7
|
+
(0, _meta_1.setProp)(target, key, propKey !== null && propKey !== void 0 ? propKey : key);
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
exports.Prop = Prop;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ModelClass } from "../../core/index";
|
|
2
|
+
import { StaticValueBuilder } from "../../meta/index";
|
|
3
|
+
export declare const Static: <T extends ModelClass<import("../../core/index").AnyModel>, I extends InstanceType<T> = InstanceType<T>>(builder: StaticValueBuilder<I>) => (target: T, key: string) => void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Static = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
5
|
+
const Static = (builder) => {
|
|
6
|
+
return (target, key) => {
|
|
7
|
+
(0, _meta_1.setStaticValue)(target, key, builder);
|
|
8
|
+
Object.defineProperty(target, key, {
|
|
9
|
+
configurable: false,
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get() {
|
|
12
|
+
return (0, _meta_1.getStaticValue)(target, key);
|
|
13
|
+
},
|
|
14
|
+
set() {
|
|
15
|
+
throw new Error("Static value is readonly");
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.Static = Static;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ModelClass } from "../../core/index";
|
|
2
|
+
import { PropsValidator } from "../../meta/index";
|
|
3
|
+
export declare const Validator: <T extends ModelClass<import("../../core/index").AnyModel>, I extends InstanceType<T> = InstanceType<T>>(validator?: PropsValidator<I> | undefined) => (target: T) => void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Validator = void 0;
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
5
|
+
const Validator = (validator) => {
|
|
6
|
+
return (target) => {
|
|
7
|
+
if (validator)
|
|
8
|
+
(0, _meta_1.setPropsValidator)(target, validator);
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
exports.Validator = Validator;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./meta"), exports);
|
|
17
18
|
__exportStar(require("./core"), exports);
|
|
18
19
|
__exportStar(require("./decorators"), exports);
|
|
19
20
|
__exportStar(require("./types"), exports);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { AnyCommand, AnyEvent, CommandHandler, EventApplier } from "../core/index";
|
|
1
2
|
import "reflect-metadata";
|
|
2
|
-
import { CommandHandler, EventApplier } from "../aggregate";
|
|
3
|
-
import { AnyCommand, AnyEvent } from "../message";
|
|
4
3
|
export declare const EventAppliersMetaKey: unique symbol;
|
|
5
4
|
export declare const getEventAppliersMap: (target: object) => Map<string, EventApplier>;
|
|
6
5
|
export declare const defineEventApplier: <T extends AnyEvent>(target: object, eventName: string, applier: EventApplier<T>) => void;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defineCommandHandler = exports.getCommandHandlersMap = exports.CommandHandlersMetaKey = exports.defineEventApplier = exports.getEventAppliersMap = exports.EventAppliersMetaKey = void 0;
|
|
4
4
|
require("reflect-metadata");
|
|
5
|
+
// Event applier map
|
|
5
6
|
exports.EventAppliersMetaKey = Symbol.for("EVENT_APPLIERS");
|
|
6
7
|
const getEventAppliersMap = (target) => {
|
|
7
8
|
return (Reflect.getMetadata(exports.EventAppliersMetaKey, target) ||
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./static-value";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
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("./static-value"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AnyModel } from "../../core/index";
|
|
2
|
+
export type StaticValueBuilder<T extends AnyModel = AnyModel> = () => T;
|
|
3
|
+
export declare class StaticValue<T extends AnyModel = AnyModel> {
|
|
4
|
+
private _value;
|
|
5
|
+
constructor(value: T | StaticValueBuilder<T>);
|
|
6
|
+
get value(): T;
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StaticValue = void 0;
|
|
4
|
+
class StaticValue {
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this._value = value;
|
|
7
|
+
}
|
|
8
|
+
get value() {
|
|
9
|
+
if (typeof this._value === "function") {
|
|
10
|
+
this._value = this._value();
|
|
11
|
+
}
|
|
12
|
+
return this._value;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.StaticValue = StaticValue;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IdGeneratorBase } from "../
|
|
1
|
+
import { IdGeneratorBase } from "../core/index";
|
|
2
2
|
export declare const IdGeneratorMetaKey: unique symbol;
|
|
3
3
|
export declare const defineIdGenerator: (target: object, idGenerator: IdGeneratorBase) => void;
|
|
4
4
|
export declare const getIdGenerator: (target: object) => any;
|
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./helpers"), exports);
|
|
17
18
|
__exportStar(require("./aggregate.metadata"), exports);
|
|
18
19
|
__exportStar(require("./id.metadata"), exports);
|
|
19
20
|
__exportStar(require("./model.metadata"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AnyModel, ModelClass, PropKey, PropsOf } from "../core/index";
|
|
2
|
+
import { StaticValue, StaticValueBuilder } from "./helpers/static-value";
|
|
3
|
+
export declare class PropsMap<T extends AnyModel = AnyModel> extends Map<PropKey, keyof PropsOf<T>> {
|
|
4
|
+
}
|
|
5
|
+
export declare const getOwnPropsMap: <T extends AnyModel = AnyModel>(target: T) => PropsMap<T>;
|
|
6
|
+
export declare const setProp: <T extends AnyModel = AnyModel>(target: T, key: PropKey, targetPropKey?: keyof PropsOf<T> | undefined) => void;
|
|
7
|
+
export declare const getPropsMap: <T extends AnyModel = AnyModel>(target: T) => PropsMap<T>;
|
|
8
|
+
export declare const setModelName: <T extends AnyModel>(target: ModelClass<T>, name?: string) => void;
|
|
9
|
+
export declare const getModelName: <T extends AnyModel>(target: ModelClass<T>) => string;
|
|
10
|
+
export type PropsValidator<T extends AnyModel = AnyModel> = (props: PropsOf<T>) => void;
|
|
11
|
+
export declare const setPropsValidator: <T extends AnyModel>(target: object, validator: PropsValidator<T>) => void;
|
|
12
|
+
export declare const getPropsValidator: <T extends AnyModel>(target: object) => PropsValidator<T> | undefined;
|
|
13
|
+
export declare const getOwnStaticValues: <T extends AnyModel>(target: object) => Map<string | symbol, StaticValue<T>>;
|
|
14
|
+
export declare const setStaticValue: <T extends AnyModel>(target: object, key: string, value: T | StaticValueBuilder<T>) => void;
|
|
15
|
+
export declare const getStaticValue: (target: object, key: string | symbol) => AnyModel | undefined;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStaticValue = exports.setStaticValue = exports.getOwnStaticValues = exports.getPropsValidator = exports.setPropsValidator = exports.getModelName = exports.setModelName = exports.getPropsMap = exports.setProp = exports.getOwnPropsMap = exports.PropsMap = void 0;
|
|
4
|
+
const _core_1 = require("../core/index");
|
|
5
|
+
const static_value_1 = require("./helpers/static-value");
|
|
6
|
+
const OwnPropsMapMetaKey = Symbol.for("OWN_PROPS_MAP");
|
|
7
|
+
// Prop keys map is a Map<Key, TargetPropKey>;
|
|
8
|
+
class PropsMap extends Map {
|
|
9
|
+
}
|
|
10
|
+
exports.PropsMap = PropsMap;
|
|
11
|
+
// target is prototype
|
|
12
|
+
const getOwnPropsMap = (target) => {
|
|
13
|
+
const ownPropsMap = () => Reflect.getOwnMetadata(OwnPropsMapMetaKey, target);
|
|
14
|
+
if (!ownPropsMap())
|
|
15
|
+
Reflect.defineMetadata(OwnPropsMapMetaKey, new PropsMap(), target);
|
|
16
|
+
return ownPropsMap();
|
|
17
|
+
};
|
|
18
|
+
exports.getOwnPropsMap = getOwnPropsMap;
|
|
19
|
+
const setProp = (target, key, targetPropKey) => {
|
|
20
|
+
const ownPropsMap = (0, exports.getOwnPropsMap)(target);
|
|
21
|
+
if (targetPropKey)
|
|
22
|
+
ownPropsMap.set(key, targetPropKey);
|
|
23
|
+
};
|
|
24
|
+
exports.setProp = setProp;
|
|
25
|
+
const PropsMapMetaKey = Symbol.for("PROPS_MAP");
|
|
26
|
+
const getPropsMap = (target) => {
|
|
27
|
+
const propsMap = () => Reflect.getOwnMetadata(PropsMapMetaKey, target);
|
|
28
|
+
if (propsMap())
|
|
29
|
+
return propsMap();
|
|
30
|
+
const buildPropsMap = (target) => {
|
|
31
|
+
let _target = target;
|
|
32
|
+
const result = new PropsMap();
|
|
33
|
+
const ownPropsMapList = [];
|
|
34
|
+
do {
|
|
35
|
+
if (_core_1.ModelBase.isModel(_target)) {
|
|
36
|
+
const ownPropsMap = (0, exports.getOwnPropsMap)(_target);
|
|
37
|
+
ownPropsMapList.unshift(ownPropsMap);
|
|
38
|
+
}
|
|
39
|
+
_target = Reflect.getPrototypeOf(_target);
|
|
40
|
+
} while (_target !== null);
|
|
41
|
+
ownPropsMapList.forEach((ownPropsMap) => {
|
|
42
|
+
ownPropsMap.forEach((targetPropKey, key) => result.set(key, targetPropKey));
|
|
43
|
+
});
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
Reflect.defineMetadata(PropsMapMetaKey, buildPropsMap(target), target);
|
|
47
|
+
return propsMap();
|
|
48
|
+
};
|
|
49
|
+
exports.getPropsMap = getPropsMap;
|
|
50
|
+
//
|
|
51
|
+
const ModelNameMetaKey = Symbol.for("MODEL_NAME");
|
|
52
|
+
const setModelName = (target, name) => {
|
|
53
|
+
Reflect.defineMetadata(ModelNameMetaKey, name !== null && name !== void 0 ? name : target.name, target);
|
|
54
|
+
};
|
|
55
|
+
exports.setModelName = setModelName;
|
|
56
|
+
const getModelName = (target) => {
|
|
57
|
+
const modelName = () => Reflect.getMetadata(ModelNameMetaKey, target);
|
|
58
|
+
if (!modelName())
|
|
59
|
+
(0, exports.setModelName)(target);
|
|
60
|
+
return modelName();
|
|
61
|
+
};
|
|
62
|
+
exports.getModelName = getModelName;
|
|
63
|
+
const PropsValidatorMetaKey = Symbol.for("PROPS_VALIDATOR");
|
|
64
|
+
const setPropsValidator = (target, validator) => {
|
|
65
|
+
Reflect.defineMetadata(PropsValidatorMetaKey, validator, target);
|
|
66
|
+
};
|
|
67
|
+
exports.setPropsValidator = setPropsValidator;
|
|
68
|
+
const getPropsValidator = (target) => {
|
|
69
|
+
return Reflect.getMetadata(PropsValidatorMetaKey, target);
|
|
70
|
+
};
|
|
71
|
+
exports.getPropsValidator = getPropsValidator;
|
|
72
|
+
//
|
|
73
|
+
const OwnStaticValuesMetaKey = Symbol.for("OWN_STATIC_VALUES");
|
|
74
|
+
const getOwnStaticValues = (target) => {
|
|
75
|
+
const ownStaticValues = () => Reflect.getOwnMetadata(OwnStaticValuesMetaKey, target);
|
|
76
|
+
if (!ownStaticValues())
|
|
77
|
+
Reflect.defineMetadata(OwnStaticValuesMetaKey, new Map(), target);
|
|
78
|
+
return ownStaticValues();
|
|
79
|
+
};
|
|
80
|
+
exports.getOwnStaticValues = getOwnStaticValues;
|
|
81
|
+
const setStaticValue = (target, key, value) => {
|
|
82
|
+
const staticValues = (0, exports.getOwnStaticValues)(target);
|
|
83
|
+
staticValues.set(key, new static_value_1.StaticValue(value));
|
|
84
|
+
};
|
|
85
|
+
exports.setStaticValue = setStaticValue;
|
|
86
|
+
const getStaticValue = (target, key) => {
|
|
87
|
+
var _a;
|
|
88
|
+
let _target = target;
|
|
89
|
+
do {
|
|
90
|
+
const staticValues = (0, exports.getOwnStaticValues)(_target);
|
|
91
|
+
if (staticValues.has(key))
|
|
92
|
+
return (_a = staticValues.get(key)) === null || _a === void 0 ? void 0 : _a.value;
|
|
93
|
+
_target = Reflect.getPrototypeOf(_target);
|
|
94
|
+
} while (_target !== null);
|
|
95
|
+
return undefined;
|
|
96
|
+
};
|
|
97
|
+
exports.getStaticValue = getStaticValue;
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
|
|
32
32
|
"paths": {
|
|
33
33
|
"#core": ["src/core/index"],
|
|
34
|
+
"#meta": ["src/meta/index"],
|
|
34
35
|
"#decorators": ["src/decorators/index"],
|
|
35
36
|
"#utils": ["src/utils/index"],
|
|
36
37
|
"#types": ["src/types/index"]
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { AnyModel, PropsOf } from "../model";
|
|
2
|
-
export declare const getPropKeySet: (target: object | null) => Set<string | symbol>;
|
|
3
|
-
export declare const setPropKey: (target: object, propKey: string) => void;
|
|
4
|
-
export declare const setModelName: (target: object, name: string) => void;
|
|
5
|
-
export declare const getModelName: (target: object) => string;
|
|
6
|
-
export type PropsValidator<T extends AnyModel = AnyModel> = (props: PropsOf<T>) => void;
|
|
7
|
-
export declare const setValidateProps: <T extends AnyModel>(target: object, validator: PropsValidator<T>) => void;
|
|
8
|
-
export declare const getValidateProps: <T extends AnyModel>(target: object) => PropsValidator<T> | undefined;
|
|
9
|
-
export type ModelMetadata<T extends AnyModel = AnyModel> = {
|
|
10
|
-
name: string;
|
|
11
|
-
validator?: PropsValidator<T>;
|
|
12
|
-
};
|
|
13
|
-
export declare const getModelMetadata: <T extends AnyModel = AnyModel>(target: object) => ModelMetadata<T>;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getModelMetadata = exports.getValidateProps = exports.setValidateProps = exports.getModelName = exports.setModelName = exports.setPropKey = exports.getPropKeySet = void 0;
|
|
4
|
-
const PropMetaKey = Symbol.for("PROP");
|
|
5
|
-
const getPropKeySet = (target) => {
|
|
6
|
-
var _a;
|
|
7
|
-
if (!target)
|
|
8
|
-
return new Set();
|
|
9
|
-
return (_a = Reflect.getMetadata(PropMetaKey, target)) !== null && _a !== void 0 ? _a : new Set();
|
|
10
|
-
};
|
|
11
|
-
exports.getPropKeySet = getPropKeySet;
|
|
12
|
-
const setPropKey = (target, propKey) => {
|
|
13
|
-
const propKeySet = (0, exports.getPropKeySet)(target);
|
|
14
|
-
propKeySet.add(propKey);
|
|
15
|
-
Reflect.defineMetadata(PropMetaKey, propKeySet, target);
|
|
16
|
-
};
|
|
17
|
-
exports.setPropKey = setPropKey;
|
|
18
|
-
//
|
|
19
|
-
const ModelNameMetaKey = Symbol.for("MODEL_NAME");
|
|
20
|
-
const setModelName = (target, name) => {
|
|
21
|
-
Reflect.defineMetadata(ModelNameMetaKey, name, target);
|
|
22
|
-
};
|
|
23
|
-
exports.setModelName = setModelName;
|
|
24
|
-
const getModelName = (target) => {
|
|
25
|
-
const modelName = Reflect.getMetadata(ModelNameMetaKey, target);
|
|
26
|
-
if (!modelName)
|
|
27
|
-
throw new Error();
|
|
28
|
-
return modelName;
|
|
29
|
-
};
|
|
30
|
-
exports.getModelName = getModelName;
|
|
31
|
-
const ValidatePropsMetaKey = Symbol.for("VALIDATE_PROPS");
|
|
32
|
-
const setValidateProps = (target, validator) => {
|
|
33
|
-
Reflect.defineMetadata(ValidatePropsMetaKey, validator, target);
|
|
34
|
-
};
|
|
35
|
-
exports.setValidateProps = setValidateProps;
|
|
36
|
-
const getValidateProps = (target) => {
|
|
37
|
-
return Reflect.getMetadata(ValidatePropsMetaKey, target);
|
|
38
|
-
};
|
|
39
|
-
exports.getValidateProps = getValidateProps;
|
|
40
|
-
const getModelMetadata = (target) => {
|
|
41
|
-
return {
|
|
42
|
-
name: (0, exports.getModelName)(target),
|
|
43
|
-
validator: (0, exports.getValidateProps)(target),
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
exports.getModelMetadata = getModelMetadata;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { AnyCommand, AnyEvent, CommandClass, CommandHandler, EventApplier, EventClass } from "../core/index";
|
|
2
|
-
export declare const ApplyEvent: <T extends AnyEvent>(eventClass: EventClass<T>) => <U extends EventApplier<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
3
|
-
export declare const HandleCommand: <T extends AnyCommand>(commandClass: CommandClass<T>) => <U extends CommandHandler<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HandleCommand = exports.ApplyEvent = void 0;
|
|
4
|
-
const _core_1 = require("../core/index");
|
|
5
|
-
const ApplyEvent = (eventClass) => {
|
|
6
|
-
return (target, propertyKey, descriptor) => {
|
|
7
|
-
if (typeof descriptor.value === "function") {
|
|
8
|
-
const eventName = (0, _core_1.getModelName)(eventClass);
|
|
9
|
-
(0, _core_1.defineEventApplier)(target, eventName, descriptor.value);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
exports.ApplyEvent = ApplyEvent;
|
|
14
|
-
const HandleCommand = (commandClass) => {
|
|
15
|
-
return (target, propertyKey, descriptor) => {
|
|
16
|
-
if (typeof descriptor.value === "function") {
|
|
17
|
-
const commandName = (0, _core_1.getModelName)(commandClass);
|
|
18
|
-
(0, _core_1.defineCommandHandler)(target, commandName, descriptor.value);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
exports.HandleCommand = HandleCommand;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { AnyModel, ModelClass, PropsValidator } from "../core/index";
|
|
2
|
-
import { TypedClassDecorator } from "./type";
|
|
3
|
-
export declare const Prop: (target: object, propKey: string) => void;
|
|
4
|
-
export declare const Name: (name?: string) => <T extends AnyModel>(target: ModelClass<T>) => void;
|
|
5
|
-
export declare const Validate: <T extends AnyModel>(validator?: PropsValidator<T> | undefined) => (target: ModelClass<T>) => void;
|
|
6
|
-
export type ModelOptions<T extends AnyModel> = {
|
|
7
|
-
name?: string;
|
|
8
|
-
validator?: PropsValidator<T>;
|
|
9
|
-
};
|
|
10
|
-
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(options?: ModelOptions<I>): TypedClassDecorator<T>;
|
|
11
|
-
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(name?: string, validator?: PropsValidator<I>): TypedClassDecorator<T>;
|
|
12
|
-
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(name?: string, options?: Omit<ModelOptions<I>, "name">): TypedClassDecorator<T>;
|
|
13
|
-
export declare const Static: <T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(builder: () => I) => (target: T, key: string) => void;
|
package/dist/decorators/model.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Static = exports.Model = exports.Validate = exports.Name = exports.Prop = void 0;
|
|
4
|
-
const _core_1 = require("../core/index");
|
|
5
|
-
const Prop = (target, propKey) => {
|
|
6
|
-
(0, _core_1.setPropKey)(target, propKey);
|
|
7
|
-
};
|
|
8
|
-
exports.Prop = Prop;
|
|
9
|
-
const Name = (name) => {
|
|
10
|
-
return (target) => {
|
|
11
|
-
(0, _core_1.setModelName)(target, name !== null && name !== void 0 ? name : target.name);
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
exports.Name = Name;
|
|
15
|
-
const Validate = (validator) => {
|
|
16
|
-
return (target) => {
|
|
17
|
-
if (validator)
|
|
18
|
-
(0, _core_1.setValidateProps)(target, validator);
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
exports.Validate = Validate;
|
|
22
|
-
function Model(p1, p2) {
|
|
23
|
-
return (target) => {
|
|
24
|
-
let options = {};
|
|
25
|
-
if (p1 && !p2) {
|
|
26
|
-
if (typeof p1 === "string")
|
|
27
|
-
options = { name: p1 };
|
|
28
|
-
else
|
|
29
|
-
options = p1;
|
|
30
|
-
}
|
|
31
|
-
else if (p1 && p2) {
|
|
32
|
-
p1 = p1;
|
|
33
|
-
if (typeof p2 === "function")
|
|
34
|
-
options = { name: p1, validator: p2 };
|
|
35
|
-
else
|
|
36
|
-
options = Object.assign({ name: p1 }, p2);
|
|
37
|
-
}
|
|
38
|
-
const { name, validator } = options;
|
|
39
|
-
(0, exports.Name)(name)(target);
|
|
40
|
-
(0, exports.Validate)(validator)(target);
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
exports.Model = Model;
|
|
44
|
-
// Lazy builder for static value (value of static prop is an instance of that model)
|
|
45
|
-
const Static = (builder) => {
|
|
46
|
-
return (target, key) => {
|
|
47
|
-
const type = Reflect.getMetadata("design:type", target, key);
|
|
48
|
-
const STATIC_VALUES_KEY = "__STATIC_VALUES__";
|
|
49
|
-
const typedTarget = target;
|
|
50
|
-
if (!typedTarget[STATIC_VALUES_KEY])
|
|
51
|
-
Object.defineProperty(target, STATIC_VALUES_KEY, {
|
|
52
|
-
configurable: false,
|
|
53
|
-
enumerable: false,
|
|
54
|
-
writable: false,
|
|
55
|
-
value: new Map(),
|
|
56
|
-
});
|
|
57
|
-
Object.defineProperty(target, key, {
|
|
58
|
-
configurable: false,
|
|
59
|
-
enumerable: true,
|
|
60
|
-
get() {
|
|
61
|
-
const staticValues = typedTarget[STATIC_VALUES_KEY];
|
|
62
|
-
if (!staticValues.has(key)) {
|
|
63
|
-
const value = builder();
|
|
64
|
-
if (!(value instanceof type))
|
|
65
|
-
throw new Error(`Invalid return type. The return of builder must be an instance of model ${type.modelName()} (class ${type.name})`);
|
|
66
|
-
staticValues.set(key, value);
|
|
67
|
-
}
|
|
68
|
-
return staticValues.get(key);
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
exports.Static = Static;
|
|
File without changes
|