ddd-node 12.1.0 → 12.2.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.
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Class } from "type-fest";
|
|
2
|
+
import { ValueObjectBase } from "./value-object";
|
|
3
|
+
import { ClassStatic } from "../types/index";
|
|
4
|
+
export type EnumValue = string | number;
|
|
5
|
+
export interface EnumProps<V extends EnumValue> {
|
|
6
|
+
value: V;
|
|
7
|
+
}
|
|
8
|
+
export declare class EnumBase<V extends EnumValue = EnumValue> extends ValueObjectBase<EnumProps<V>> {
|
|
9
|
+
constructor(value: V);
|
|
10
|
+
value: V;
|
|
11
|
+
}
|
|
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>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EnumBase = void 0;
|
|
13
|
+
const _decorators_1 = require("../decorators/index");
|
|
14
|
+
const value_object_1 = require("./value-object");
|
|
15
|
+
class EnumBase extends value_object_1.ValueObjectBase {
|
|
16
|
+
constructor(value) {
|
|
17
|
+
super({ value });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.EnumBase = EnumBase;
|
|
21
|
+
__decorate([
|
|
22
|
+
_decorators_1.Prop,
|
|
23
|
+
__metadata("design:type", Object)
|
|
24
|
+
], EnumBase.prototype, "value", void 0);
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./aggregate"), exports);
|
|
18
18
|
__exportStar(require("./entity"), exports);
|
|
19
|
+
__exportStar(require("./enum"), exports);
|
|
19
20
|
__exportStar(require("./error"), exports);
|
|
20
21
|
__exportStar(require("./id"), exports);
|
|
21
22
|
__exportStar(require("./message"), exports);
|
|
@@ -2,15 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getModelMetadata = exports.getValidateProps = exports.setValidateProps = exports.getModelName = exports.setModelName = exports.setPropKey = exports.getPropKeySet = void 0;
|
|
4
4
|
const PropMetaKey = Symbol.for("PROP");
|
|
5
|
-
// export const getPropKeys = (
|
|
6
|
-
// target: object | null
|
|
7
|
-
// ): Map<string | symbol, boolean> => {
|
|
8
|
-
// if (!target) return new Map();
|
|
9
|
-
// return (
|
|
10
|
-
// Reflect.getMetadata(PropMetaKey, target) ||
|
|
11
|
-
// new Map<string | symbol, boolean>()
|
|
12
|
-
// );
|
|
13
|
-
// };
|
|
14
5
|
const getPropKeySet = (target) => {
|
|
15
6
|
var _a;
|
|
16
7
|
if (!target)
|
|
@@ -10,3 +10,4 @@ export type ModelOptions<T extends AnyModel> = {
|
|
|
10
10
|
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(options?: ModelOptions<I>): TypedClassDecorator<T>;
|
|
11
11
|
export declare function Model<T extends ModelClass, I extends InstanceType<T> = InstanceType<T>>(name?: string, validator?: PropsValidator<I>): TypedClassDecorator<T>;
|
|
12
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: Extract<keyof T, string>) => void;
|
package/dist/decorators/model.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Model = exports.Validate = exports.Name = exports.Prop = void 0;
|
|
3
|
+
exports.Static = exports.Model = exports.Validate = exports.Name = exports.Prop = void 0;
|
|
4
4
|
const _core_1 = require("../core/index");
|
|
5
5
|
const Prop = (target, propKey) => {
|
|
6
6
|
(0, _core_1.setPropKey)(target, propKey);
|
|
@@ -41,3 +41,33 @@ function Model(p1, p2) {
|
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
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 = "_staticValues";
|
|
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;
|