ddd-node 13.0.0-0 → 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 +10 -9
- package/dist/core/enum.js +24 -3
- 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 +3 -3
- package/dist/core/model/model.d.ts +23 -14
- package/dist/core/model/model.js +44 -26
- package/dist/core/value-object.d.ts +8 -6
- package/dist/decorators/aggregate/handle.d.ts +1 -1
- package/dist/decorators/aggregate/when.d.ts +1 -1
- package/dist/decorators/model/enum.d.ts +2 -0
- package/dist/decorators/model/enum.js +10 -0
- package/dist/decorators/model/index.d.ts +3 -3
- package/dist/decorators/model/index.js +3 -3
- 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 -1
- package/dist/decorators/model/prop.js +4 -2
- package/dist/decorators/model/static.d.ts +2 -2
- package/dist/decorators/model/static.js +6 -25
- package/dist/decorators/model/validator.d.ts +3 -0
- package/dist/decorators/model/{validate.js → validator.js} +4 -4
- 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/meta/index.d.ts +1 -0
- package/dist/meta/index.js +1 -0
- package/dist/meta/model.metadata.d.ts +14 -12
- package/dist/meta/model.metadata.js +84 -33
- package/package.json +1 -1
- 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/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
|
@@ -2,14 +2,15 @@ import { ClassStatic } from "../types/index";
|
|
|
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
|
-
value:
|
|
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;
|
|
14
|
+
}
|
|
15
|
+
export interface EnumClass<T extends EnumBase = EnumBase> extends Class<T>, ClassStatic<typeof EnumBase> {
|
|
13
16
|
}
|
|
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
|
@@ -13,15 +13,36 @@ exports.EnumBase = void 0;
|
|
|
13
13
|
const _decorators_1 = require("../decorators/index");
|
|
14
14
|
const model_1 = require("./model");
|
|
15
15
|
class EnumBase extends model_1.ModelBase {
|
|
16
|
-
static values() {
|
|
17
|
-
|
|
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
|
+
}
|
|
18
34
|
constructor(value) {
|
|
19
35
|
super();
|
|
20
36
|
this.initializeProps({ value });
|
|
21
37
|
}
|
|
38
|
+
equals(other) {
|
|
39
|
+
const equalType = other instanceof this.constructor;
|
|
40
|
+
const equalValue = other.value === this.value;
|
|
41
|
+
return equalType && equalValue;
|
|
42
|
+
}
|
|
22
43
|
}
|
|
23
44
|
exports.EnumBase = EnumBase;
|
|
24
45
|
__decorate([
|
|
25
|
-
_decorators_1.Prop,
|
|
46
|
+
(0, _decorators_1.Prop)(),
|
|
26
47
|
__metadata("design:type", Object)
|
|
27
48
|
], EnumBase.prototype, "value", void 0);
|
|
@@ -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
|
}
|
|
@@ -9,14 +9,14 @@ class ModelWithId extends model_1.ModelBase {
|
|
|
9
9
|
super();
|
|
10
10
|
this._id = id;
|
|
11
11
|
}
|
|
12
|
-
static
|
|
12
|
+
static idGenerator() {
|
|
13
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,26 +1,35 @@
|
|
|
1
|
-
import { ModelMetadata } from "../../meta/index";
|
|
2
1
|
import { ClassStatic } from "../../types/index";
|
|
3
2
|
import { Class } from "type-fest";
|
|
4
|
-
export
|
|
5
|
-
|
|
3
|
+
export type PropKey = string | symbol;
|
|
4
|
+
export interface Props {
|
|
5
|
+
[key: PropKey]: any;
|
|
6
|
+
}
|
|
7
|
+
export declare class ModelBase<P extends Props> {
|
|
6
8
|
static readonly EMPTY_PROPS: {};
|
|
7
|
-
|
|
8
|
-
private _propKeys;
|
|
9
|
-
protected _props: Props;
|
|
10
|
-
static isMutable(): boolean;
|
|
9
|
+
protected _props: P;
|
|
11
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>>;
|
|
12
17
|
constructor();
|
|
13
18
|
private redefineModel;
|
|
19
|
+
protected get _prototype(): object;
|
|
20
|
+
protected get _type(): ModelClass<this>;
|
|
14
21
|
isMutable(): boolean;
|
|
15
|
-
getPropKeys(): Set<string | symbol>;
|
|
16
|
-
validateProps(props: Props): void;
|
|
17
|
-
validate(): void;
|
|
18
|
-
getModelMetadata(): ModelMetadata<this>;
|
|
19
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;
|
|
20
28
|
props(): any;
|
|
21
|
-
protected initializeProps(props:
|
|
29
|
+
protected initializeProps(props: P): void;
|
|
22
30
|
}
|
|
23
31
|
export type AnyModel = ModelBase<object>;
|
|
24
|
-
export type PropsOf<T extends AnyModel> = T extends ModelBase<infer P extends
|
|
25
|
-
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
|
+
}
|
|
26
35
|
export type EmptyProps = {};
|
package/dist/core/model/model.js
CHANGED
|
@@ -7,57 +7,75 @@ exports.ModelBase = void 0;
|
|
|
7
7
|
const _meta_1 = require("../../meta/index");
|
|
8
8
|
const lodash_1 = __importDefault(require("lodash"));
|
|
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, _meta_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");
|
|
@@ -86,5 +105,4 @@ class ModelBase {
|
|
|
86
105
|
}
|
|
87
106
|
}
|
|
88
107
|
exports.ModelBase = ModelBase;
|
|
89
|
-
ModelBase.STATIC_VALUES_KEY = "__STATIC_VALUES__";
|
|
90
108
|
ModelBase.EMPTY_PROPS = {};
|
|
@@ -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
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { AnyCommand, CommandClass, CommandHandler } from "../../core/index";
|
|
2
|
-
export declare const Handle: <T extends AnyCommand>(commandClass: CommandClass<T>) => <U extends CommandHandler<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
2
|
+
export declare const Handle: <T extends AnyCommand>(commandClass: CommandClass<T, any[]>) => <U extends CommandHandler<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { AnyEvent, EventApplier, EventClass } from "../../core/index";
|
|
2
|
-
export declare const When: <T extends AnyEvent>(eventClass: EventClass<T>) => <U extends EventApplier<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
2
|
+
export declare const When: <T extends AnyEvent>(eventClass: EventClass<T, any[]>) => <U extends EventApplier<T>>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<U>) => void;
|
|
@@ -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;
|
|
@@ -15,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./prop"), exports);
|
|
18
|
-
__exportStar(require("./name"), exports);
|
|
19
|
-
__exportStar(require("./
|
|
20
|
-
__exportStar(require("./model"), exports);
|
|
18
|
+
__exportStar(require("./model-name"), exports);
|
|
19
|
+
__exportStar(require("./validator"), exports);
|
|
21
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;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { AnyModel, PropKey, PropsOf } from "../../core/index";
|
|
2
|
+
export declare const Prop: <T extends AnyModel>(propKey?: keyof PropsOf<T> | undefined) => (target: T, key: PropKey) => void;
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Prop = void 0;
|
|
4
4
|
const _meta_1 = require("../../meta/index");
|
|
5
|
-
const Prop = (
|
|
6
|
-
|
|
5
|
+
const Prop = (propKey) => {
|
|
6
|
+
return (target, key) => {
|
|
7
|
+
(0, _meta_1.setProp)(target, key, propKey !== null && propKey !== void 0 ? propKey : key);
|
|
8
|
+
};
|
|
7
9
|
};
|
|
8
10
|
exports.Prop = Prop;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ModelClass } from "../../core/index";
|
|
2
|
-
import { StaticValueBuilder } from "
|
|
3
|
-
export declare const Static: <T extends ModelClass
|
|
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;
|
|
@@ -1,37 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Static = void 0;
|
|
4
|
-
const
|
|
5
|
-
// Lazy builder for static value (value of static prop is an instance of that model)
|
|
4
|
+
const _meta_1 = require("../../meta/index");
|
|
6
5
|
const Static = (builder) => {
|
|
7
6
|
return (target, key) => {
|
|
8
|
-
|
|
9
|
-
const typedTarget = target;
|
|
10
|
-
let staticValues = typedTarget[STATIC_VALUES_KEY];
|
|
11
|
-
if (!staticValues || staticValues.target !== target) {
|
|
12
|
-
Object.defineProperty(target, STATIC_VALUES_KEY, {
|
|
13
|
-
configurable: false,
|
|
14
|
-
enumerable: false,
|
|
15
|
-
writable: false,
|
|
16
|
-
value: new static_values_1.StaticValues(target),
|
|
17
|
-
});
|
|
18
|
-
staticValues = typedTarget[STATIC_VALUES_KEY];
|
|
19
|
-
}
|
|
20
|
-
staticValues.setValue(key, builder);
|
|
7
|
+
(0, _meta_1.setStaticValue)(target, key, builder);
|
|
21
8
|
Object.defineProperty(target, key, {
|
|
22
9
|
configurable: false,
|
|
23
10
|
enumerable: true,
|
|
24
11
|
get() {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return undefined;
|
|
30
|
-
if (sValues.hasValueOf(key))
|
|
31
|
-
return sValues.getValue(key);
|
|
32
|
-
loopTarget = Reflect.getPrototypeOf(loopTarget);
|
|
33
|
-
} while (loopTarget !== null);
|
|
34
|
-
return undefined;
|
|
12
|
+
return (0, _meta_1.getStaticValue)(target, key);
|
|
13
|
+
},
|
|
14
|
+
set() {
|
|
15
|
+
throw new Error("Static value is readonly");
|
|
35
16
|
},
|
|
36
17
|
});
|
|
37
18
|
};
|
|
@@ -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;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Validator = void 0;
|
|
4
4
|
const _meta_1 = require("../../meta/index");
|
|
5
|
-
const
|
|
5
|
+
const Validator = (validator) => {
|
|
6
6
|
return (target) => {
|
|
7
7
|
if (validator)
|
|
8
|
-
(0, _meta_1.
|
|
8
|
+
(0, _meta_1.setPropsValidator)(target, validator);
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
|
-
exports.
|
|
11
|
+
exports.Validator = Validator;
|
|
@@ -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;
|
package/dist/meta/index.d.ts
CHANGED
package/dist/meta/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("./helpers"), exports);
|
|
17
18
|
__exportStar(require("./aggregate.metadata"), exports);
|
|
18
19
|
__exportStar(require("./id.metadata"), exports);
|
|
19
20
|
__exportStar(require("./model.metadata"), exports);
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { AnyModel, PropsOf } from "../core/index";
|
|
2
|
-
|
|
3
|
-
export declare
|
|
4
|
-
|
|
5
|
-
export declare const
|
|
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;
|
|
6
10
|
export type PropsValidator<T extends AnyModel = AnyModel> = (props: PropsOf<T>) => void;
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
export declare const getModelMetadata: <T extends AnyModel = AnyModel>(target: object) => ModelMetadata<T>;
|
|
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;
|
|
@@ -1,46 +1,97 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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);
|
|
10
23
|
};
|
|
11
|
-
exports.
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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();
|
|
16
48
|
};
|
|
17
|
-
exports.
|
|
49
|
+
exports.getPropsMap = getPropsMap;
|
|
18
50
|
//
|
|
19
51
|
const ModelNameMetaKey = Symbol.for("MODEL_NAME");
|
|
20
52
|
const setModelName = (target, name) => {
|
|
21
|
-
Reflect.defineMetadata(ModelNameMetaKey, name, target);
|
|
53
|
+
Reflect.defineMetadata(ModelNameMetaKey, name !== null && name !== void 0 ? name : target.name, target);
|
|
22
54
|
};
|
|
23
55
|
exports.setModelName = setModelName;
|
|
24
56
|
const getModelName = (target) => {
|
|
25
|
-
const modelName = Reflect.getMetadata(ModelNameMetaKey, target);
|
|
26
|
-
if (!modelName)
|
|
27
|
-
|
|
28
|
-
return modelName;
|
|
57
|
+
const modelName = () => Reflect.getMetadata(ModelNameMetaKey, target);
|
|
58
|
+
if (!modelName())
|
|
59
|
+
(0, exports.setModelName)(target);
|
|
60
|
+
return modelName();
|
|
29
61
|
};
|
|
30
62
|
exports.getModelName = getModelName;
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
Reflect.defineMetadata(
|
|
34
|
-
};
|
|
35
|
-
exports.
|
|
36
|
-
const
|
|
37
|
-
return Reflect.getMetadata(
|
|
38
|
-
};
|
|
39
|
-
exports.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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;
|
|
45
96
|
};
|
|
46
|
-
exports.
|
|
97
|
+
exports.getStaticValue = getStaticValue;
|
package/package.json
CHANGED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { AnyModel, ModelClass } from "../../../core/index";
|
|
2
|
-
import { StaticValueBuilder } from "../types/static-value-builder";
|
|
3
|
-
export declare class StaticValues<T extends AnyModel> {
|
|
4
|
-
private readonly _target;
|
|
5
|
-
private readonly _values;
|
|
6
|
-
constructor(target: ModelClass<T>);
|
|
7
|
-
get target(): ModelClass<T>;
|
|
8
|
-
get values(): Map<string | symbol, T | StaticValueBuilder<T>>;
|
|
9
|
-
getValue(key: string | symbol): T | StaticValueBuilder<T> | undefined;
|
|
10
|
-
setValue(key: string | symbol, value: T | StaticValueBuilder<T>): void;
|
|
11
|
-
hasValueOf(key: string | symbol): boolean;
|
|
12
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StaticValues = void 0;
|
|
4
|
-
class StaticValues {
|
|
5
|
-
constructor(target) {
|
|
6
|
-
this._target = target;
|
|
7
|
-
this._values = new Map();
|
|
8
|
-
}
|
|
9
|
-
get target() {
|
|
10
|
-
return this._target;
|
|
11
|
-
}
|
|
12
|
-
get values() {
|
|
13
|
-
return this._values;
|
|
14
|
-
}
|
|
15
|
-
getValue(key) {
|
|
16
|
-
const valueOrBulider = this._values.get(key);
|
|
17
|
-
if (typeof valueOrBulider === "function") {
|
|
18
|
-
const value = valueOrBulider();
|
|
19
|
-
this.setValue(key, value);
|
|
20
|
-
}
|
|
21
|
-
return this._values.get(key);
|
|
22
|
-
}
|
|
23
|
-
setValue(key, value) {
|
|
24
|
-
this._values.set(key, value);
|
|
25
|
-
}
|
|
26
|
-
hasValueOf(key) {
|
|
27
|
-
return this._values.has(key);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.StaticValues = StaticValues;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { AnyModel, ModelClass } from "../../core/index";
|
|
2
|
-
import { PropsValidator } from "../../meta/index";
|
|
3
|
-
import { TypedClassDecorator } from "../type";
|
|
4
|
-
export type ModelOptions<T extends AnyModel> = {
|
|
5
|
-
name?: string;
|
|
6
|
-
validator?: PropsValidator<T>;
|
|
7
|
-
};
|
|
8
|
-
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(options?: ModelOptions<I>): TypedClassDecorator<T>;
|
|
9
|
-
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(name?: string, validator?: PropsValidator<I>): TypedClassDecorator<T>;
|
|
10
|
-
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(name?: string, options?: Omit<ModelOptions<I>, "name">): TypedClassDecorator<T>;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Model = void 0;
|
|
4
|
-
const model_1 = require("../model");
|
|
5
|
-
function Model(p1, p2) {
|
|
6
|
-
return (target) => {
|
|
7
|
-
let options = {};
|
|
8
|
-
if (p1 && !p2) {
|
|
9
|
-
if (typeof p1 === "string")
|
|
10
|
-
options = { name: p1 };
|
|
11
|
-
else
|
|
12
|
-
options = p1;
|
|
13
|
-
}
|
|
14
|
-
else if (p1 && p2) {
|
|
15
|
-
p1 = p1;
|
|
16
|
-
if (typeof p2 === "function")
|
|
17
|
-
options = { name: p1, validator: p2 };
|
|
18
|
-
else
|
|
19
|
-
options = Object.assign({ name: p1 }, p2);
|
|
20
|
-
}
|
|
21
|
-
const { name, validator } = options;
|
|
22
|
-
(0, model_1.Name)(name)(target);
|
|
23
|
-
(0, model_1.Validate)(validator)(target);
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
exports.Model = Model;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Name = void 0;
|
|
4
|
-
const _meta_1 = require("../../meta/index");
|
|
5
|
-
const Name = (name) => {
|
|
6
|
-
return (target) => {
|
|
7
|
-
(0, _meta_1.setModelName)(target, name !== null && name !== void 0 ? name : target.name);
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
exports.Name = Name;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { ModelBase, ModelClass } from "../../../core/index";
|
|
2
|
-
import { StaticValues } from "../helpers/static-values";
|
|
3
|
-
export type StaticTypedTarget<T extends ModelClass = ModelClass, I extends InstanceType<T> = InstanceType<T>> = T & {
|
|
4
|
-
[ModelBase.STATIC_VALUES_KEY]?: StaticValues<I>;
|
|
5
|
-
};
|