@akanjs/constant 1.0.5 → 1.0.6
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/cjs/src/baseGql.js +89 -98
- package/cjs/src/classMeta.js +6 -21
- package/cjs/src/constantInfo.js +1 -12
- package/cjs/src/crystalize.js +18 -35
- package/cjs/src/default.js +15 -36
- package/cjs/src/fieldInfo.js +117 -16
- package/cjs/src/immerify.js +3 -5
- package/cjs/src/purify.js +31 -44
- package/cjs/src/scalar.js +1 -30
- package/cjs/src/serialize.js +6 -6
- package/esm/src/baseGql.js +90 -98
- package/esm/src/classMeta.js +6 -21
- package/esm/src/constantInfo.js +1 -12
- package/esm/src/crystalize.js +19 -36
- package/esm/src/default.js +15 -36
- package/esm/src/fieldInfo.js +119 -19
- package/esm/src/immerify.js +3 -5
- package/esm/src/purify.js +32 -45
- package/esm/src/scalar.js +1 -31
- package/esm/src/serialize.js +7 -7
- package/package.json +1 -1
- package/src/baseGql.d.ts +29 -8
- package/src/classMeta.d.ts +5 -8
- package/src/constantInfo.d.ts +18 -23
- package/src/crystalize.d.ts +5 -2
- package/src/default.d.ts +2 -5
- package/src/fieldInfo.d.ts +93 -13
- package/src/immerify.d.ts +4 -1
- package/src/purify.d.ts +4 -4
- package/src/scalar.d.ts +0 -5
- package/src/serialize.d.ts +3 -3
- package/src/types.d.ts +4 -4
package/cjs/src/baseGql.js
CHANGED
|
@@ -21,92 +21,87 @@ __export(baseGql_exports, {
|
|
|
21
21
|
});
|
|
22
22
|
module.exports = __toCommonJS(baseGql_exports);
|
|
23
23
|
var import_reflect_metadata = require("reflect-metadata");
|
|
24
|
-
var import_base = require("@akanjs/base");
|
|
25
24
|
var import_common = require("@akanjs/common");
|
|
26
|
-
var
|
|
25
|
+
var import_immer = require("immer");
|
|
26
|
+
var import__ = require(".");
|
|
27
27
|
var import_constantInfo = require("./constantInfo");
|
|
28
28
|
var import_fieldInfo = require("./fieldInfo");
|
|
29
|
-
var
|
|
30
|
-
const defaultFieldMeta = {
|
|
31
|
-
fieldType: "property",
|
|
32
|
-
immutable: false,
|
|
33
|
-
select: true,
|
|
34
|
-
isClass: false,
|
|
35
|
-
isScalar: true,
|
|
36
|
-
nullable: false,
|
|
37
|
-
isArray: false,
|
|
38
|
-
arrDepth: 0,
|
|
39
|
-
optArrDepth: 0,
|
|
40
|
-
default: null,
|
|
41
|
-
isMap: false,
|
|
42
|
-
meta: {}
|
|
43
|
-
};
|
|
44
|
-
const baseFieldMetaMap = /* @__PURE__ */ new Map([
|
|
45
|
-
["id", { ...defaultFieldMeta, key: "id", modelRef: import_base.ID }],
|
|
46
|
-
["createdAt", { ...defaultFieldMeta, key: "createdAt", modelRef: Date }],
|
|
47
|
-
["updatedAt", { ...defaultFieldMeta, key: "updatedAt", modelRef: Date }],
|
|
48
|
-
["removedAt", { ...defaultFieldMeta, key: "removedAt", modelRef: Date, nullable: true, default: null }]
|
|
49
|
-
]);
|
|
50
|
-
const baseInsightFieldMetaMap = /* @__PURE__ */ new Map([
|
|
51
|
-
["count", { ...defaultFieldMeta, key: "count", modelRef: import_base.Int, default: 0, accumulate: { $sum: 1 } }]
|
|
52
|
-
]);
|
|
29
|
+
var import_purify = require("./purify");
|
|
53
30
|
const objectModelOf = (inputRef, fieldMap) => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return ObjectModel;
|
|
31
|
+
const field2 = Object.assign(
|
|
32
|
+
import_fieldInfo.ConstantField.getBaseModelField(),
|
|
33
|
+
inputRef.field,
|
|
34
|
+
Object.fromEntries(Object.entries(fieldMap).map(([key, fieldInfo]) => [key, fieldInfo.toField()]))
|
|
35
|
+
);
|
|
36
|
+
const baseObjectModelRef = getBaseConstantClass(field2);
|
|
37
|
+
applyConstantStatics(baseObjectModelRef);
|
|
38
|
+
import_constantInfo.constantInfo.setModelType(baseObjectModelRef, "object");
|
|
39
|
+
return baseObjectModelRef;
|
|
64
40
|
};
|
|
65
41
|
const lightModelOf = (objectRef, fields, fieldMap, ...libLightModelRefs) => {
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
(0, import_common.applyMixins)(BaseLightModel, libLightModelRefs);
|
|
78
|
-
(0, import_scalar.setFieldMetaMap)(BaseLightModel, fieldMetaMap);
|
|
79
|
-
(0, import_classMeta.setExtendRef)(BaseLightModel, objectRef);
|
|
80
|
-
import_constantInfo.constantInfo.setModelType(BaseLightModel, "light");
|
|
81
|
-
Object.entries(fieldMap).forEach(([key, fieldInfo]) => {
|
|
82
|
-
fieldInfo.applyFieldMeta(BaseLightModel, key);
|
|
83
|
-
});
|
|
84
|
-
return BaseLightModel;
|
|
42
|
+
const libLightModelRef = libLightModelRefs.at(0);
|
|
43
|
+
const field2 = Object.assign(
|
|
44
|
+
libLightModelRef?.field ?? import_fieldInfo.ConstantField.getBaseModelField(),
|
|
45
|
+
Object.fromEntries(Object.entries(fieldMap).map(([key, field3]) => [key, field3.toField()])),
|
|
46
|
+
Object.fromEntries(fields.map((field3) => [field3, objectRef.field[field3]]))
|
|
47
|
+
);
|
|
48
|
+
const baseLightModelRef = getBaseConstantClass(field2);
|
|
49
|
+
applyConstantStatics(baseLightModelRef);
|
|
50
|
+
(0, import_common.applyMixins)(baseLightModelRef, libLightModelRefs);
|
|
51
|
+
import_constantInfo.constantInfo.setModelType(baseLightModelRef, "light");
|
|
52
|
+
return baseLightModelRef;
|
|
85
53
|
};
|
|
86
54
|
const fullModelOf = (objectRef, lightRef, fieldMap, ...libFullModelRefs) => {
|
|
87
|
-
const fullRef = libFullModelRefs.at(0) ??
|
|
88
|
-
|
|
89
|
-
|
|
55
|
+
const fullRef = libFullModelRefs.at(0) ?? getBaseConstantClass(import_fieldInfo.ConstantField.getBaseModelField());
|
|
56
|
+
Object.assign(
|
|
57
|
+
fullRef.field,
|
|
58
|
+
objectRef.field,
|
|
59
|
+
lightRef.field,
|
|
60
|
+
Object.fromEntries(Object.entries(fieldMap).map(([key, field2]) => [key, field2.toField()]))
|
|
61
|
+
);
|
|
90
62
|
(0, import_common.applyMixins)(fullRef, [objectRef, lightRef, ...libFullModelRefs]);
|
|
91
63
|
libFullModelRefs.forEach((libFullModelRef) => {
|
|
92
64
|
(0, import_common.applyMixins)(libFullModelRef, [objectRef, lightRef]);
|
|
93
65
|
});
|
|
94
|
-
(
|
|
95
|
-
(0, import_classMeta.setExtendRef)(fullRef, objectRef);
|
|
66
|
+
applyConstantStatics(fullRef);
|
|
96
67
|
import_constantInfo.constantInfo.setModelType(fullRef, "full");
|
|
97
|
-
Object.entries(fieldMap).forEach(([key, fieldInfo]) => {
|
|
98
|
-
fieldInfo.applyFieldMeta(fullRef, key);
|
|
99
|
-
});
|
|
100
68
|
return fullRef;
|
|
101
69
|
};
|
|
102
|
-
const
|
|
103
|
-
class
|
|
70
|
+
const getBaseConstantClass = (field2) => {
|
|
71
|
+
class BaseConstant {
|
|
72
|
+
static field = field2;
|
|
73
|
+
[import_immer.immerable] = true;
|
|
74
|
+
constructor(obj) {
|
|
75
|
+
Object.assign(this, this.constructor.default);
|
|
76
|
+
if (obj)
|
|
77
|
+
this.set(obj);
|
|
78
|
+
}
|
|
79
|
+
set(obj) {
|
|
80
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
81
|
+
if (!this.constructor.field[key])
|
|
82
|
+
return;
|
|
83
|
+
const fieldProp = this.constructor.field[key].getProps();
|
|
84
|
+
this[key] = (0, import__.crystalize)(fieldProp, value);
|
|
85
|
+
});
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
104
88
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
89
|
+
return BaseConstant;
|
|
90
|
+
};
|
|
91
|
+
const makeBaseScalar = (fieldMap) => {
|
|
92
|
+
const fieldObject = Object.fromEntries(Object.entries(fieldMap).map(([key, field2]) => [key, field2.toField()]));
|
|
93
|
+
const baseScalarRef = getBaseConstantClass(fieldObject);
|
|
94
|
+
applyConstantStatics(baseScalarRef);
|
|
95
|
+
import_constantInfo.constantInfo.setModelType(baseScalarRef, "scalar");
|
|
96
|
+
return baseScalarRef;
|
|
97
|
+
};
|
|
98
|
+
const applyConstantStatics = (model) => {
|
|
99
|
+
Object.assign(model, {
|
|
100
|
+
purify: (0, import_purify.makePurify)(model),
|
|
101
|
+
default: (0, import__.makeDefault)(model.field)
|
|
102
|
+
// getDefault: () => immerify(model, Object.assign(new model() as object, defaultFn)),
|
|
108
103
|
});
|
|
109
|
-
return
|
|
104
|
+
return model;
|
|
110
105
|
};
|
|
111
106
|
function via(firstRefOrBuildField, secondRefOrFieldsOrBuildField, thirdRefOrResolveField, ...extendRefs) {
|
|
112
107
|
if (!firstRefOrBuildField.prototype || !import_constantInfo.constantInfo.getModelType(firstRefOrBuildField, { allowEmpty: true })) {
|
|
@@ -166,39 +161,35 @@ function via(firstRefOrBuildField, secondRefOrFieldsOrBuildField, thirdRefOrReso
|
|
|
166
161
|
}
|
|
167
162
|
const extendModelInputs = (fieldMap, ...libInputModelRefs) => {
|
|
168
163
|
const baseInputModelRef = libInputModelRefs.at(0);
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return BaseInput;
|
|
164
|
+
const fieldObject = Object.assign(
|
|
165
|
+
baseInputModelRef?.field ?? {},
|
|
166
|
+
Object.fromEntries(Object.entries(fieldMap).map(([key, field2]) => [key, field2.toField()]))
|
|
167
|
+
);
|
|
168
|
+
const baseInputRef = getBaseConstantClass(fieldObject);
|
|
169
|
+
applyConstantStatics(baseInputRef);
|
|
170
|
+
import_constantInfo.constantInfo.setModelType(baseInputRef, "scalar");
|
|
171
|
+
return baseInputRef;
|
|
178
172
|
};
|
|
179
173
|
const extendModelObjects = (inputRef, fieldMap, ...libObjectModelRefs) => {
|
|
180
174
|
const baseObjectModelRef = libObjectModelRefs.at(0);
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
});
|
|
191
|
-
return BaseInput;
|
|
175
|
+
const field2 = Object.assign(
|
|
176
|
+
baseObjectModelRef?.field ?? {},
|
|
177
|
+
inputRef.field,
|
|
178
|
+
Object.fromEntries(Object.entries(fieldMap).map(([key, fieldInfo]) => [key, fieldInfo.toField()]))
|
|
179
|
+
);
|
|
180
|
+
const baseInputRef = getBaseConstantClass(field2);
|
|
181
|
+
applyConstantStatics(baseInputRef);
|
|
182
|
+
import_constantInfo.constantInfo.setModelType(baseInputRef, "object");
|
|
183
|
+
return baseInputRef;
|
|
192
184
|
};
|
|
193
185
|
const extendModelInsights = (fieldMap, ...insightModelRefs) => {
|
|
194
186
|
const baseInsightModelRef = insightModelRefs.at(0);
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return BaseInsight2;
|
|
187
|
+
const field2 = Object.assign(
|
|
188
|
+
baseInsightModelRef?.field ?? import_fieldInfo.ConstantField.getBaseInsightField(),
|
|
189
|
+
Object.fromEntries(Object.entries(fieldMap).map(([key, fieldInfo]) => [key, fieldInfo.toField()]))
|
|
190
|
+
);
|
|
191
|
+
const baseInsightRef = getBaseConstantClass(field2);
|
|
192
|
+
applyConstantStatics(baseInsightRef);
|
|
193
|
+
import_constantInfo.constantInfo.setModelType(baseInsightRef, "insight");
|
|
194
|
+
return baseInsightRef;
|
|
204
195
|
};
|
package/cjs/src/classMeta.js
CHANGED
|
@@ -18,40 +18,25 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
18
18
|
var classMeta_exports = {};
|
|
19
19
|
__export(classMeta_exports, {
|
|
20
20
|
getChildClassRefs: () => getChildClassRefs,
|
|
21
|
-
getExtendRef: () => getExtendRef,
|
|
22
21
|
getFieldEnumMetas: () => getFieldEnumMetas,
|
|
23
|
-
hasTextField: () => hasTextField
|
|
24
|
-
setExtendRef: () => setExtendRef
|
|
22
|
+
hasTextField: () => hasTextField
|
|
25
23
|
});
|
|
26
24
|
module.exports = __toCommonJS(classMeta_exports);
|
|
27
25
|
var import_reflect_metadata = require("reflect-metadata");
|
|
28
26
|
var import_constantInfo = require("./constantInfo");
|
|
29
|
-
var import_scalar = require("./scalar");
|
|
30
|
-
const setExtendRef = (modelRef, extendRef) => {
|
|
31
|
-
Reflect.defineMetadata("akan:extend", extendRef, modelRef.prototype);
|
|
32
|
-
};
|
|
33
|
-
const getExtendRef = (modelRef, { allowEmpty } = {}) => {
|
|
34
|
-
const extendRef = Reflect.getMetadata("akan:extend", modelRef.prototype);
|
|
35
|
-
if (!extendRef && !allowEmpty)
|
|
36
|
-
throw new Error(`ExtendRef not found - ${modelRef.name}`);
|
|
37
|
-
return extendRef;
|
|
38
|
-
};
|
|
39
27
|
const getChildClassRefs = (target) => {
|
|
40
|
-
const metadatas = (0, import_scalar.getFieldMetas)(target);
|
|
41
28
|
const refMap = /* @__PURE__ */ new Map();
|
|
42
|
-
const childRefs =
|
|
43
|
-
return [...acc,
|
|
29
|
+
const childRefs = Object.entries(target.field).filter(([_, field]) => field.isClass).reduce((acc, [key, field]) => {
|
|
30
|
+
return [...acc, field.modelRef, ...getChildClassRefs(field.modelRef)];
|
|
44
31
|
}, []);
|
|
45
32
|
childRefs.filter((modelRef, idx) => childRefs.findIndex((ref) => ref.prototype === modelRef.prototype) === idx).map((modelRef) => refMap.set(import_constantInfo.constantInfo.getRefName(modelRef), modelRef));
|
|
46
33
|
return [...refMap.values()];
|
|
47
34
|
};
|
|
48
35
|
const getFieldEnumMetas = (modelRef) => {
|
|
49
|
-
|
|
50
|
-
return fieldMetas.filter((fieldMeta) => !!fieldMeta.enum).map((fieldMeta) => ({ key: fieldMeta.key, enum: fieldMeta.enum }));
|
|
36
|
+
return Object.entries(modelRef.field).filter(([_, field]) => !!field.enum).map(([key, field]) => ({ key, enum: field.enum }));
|
|
51
37
|
};
|
|
52
38
|
const hasTextField = (modelRef) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
(fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
39
|
+
return Object.entries(modelRef.field).some(
|
|
40
|
+
([_, field]) => !!field.text || field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)
|
|
56
41
|
);
|
|
57
42
|
};
|
package/cjs/src/constantInfo.js
CHANGED
|
@@ -24,8 +24,6 @@ __export(constantInfo_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(constantInfo_exports);
|
|
25
25
|
var import_reflect_metadata = require("reflect-metadata");
|
|
26
26
|
var import_base = require("@akanjs/base");
|
|
27
|
-
var import__ = require(".");
|
|
28
|
-
var import_immerify = require("./immerify");
|
|
29
27
|
const constantInfo = {
|
|
30
28
|
database: /* @__PURE__ */ new Map(),
|
|
31
29
|
scalar: /* @__PURE__ */ new Map(),
|
|
@@ -93,7 +91,7 @@ const constantInfo = {
|
|
|
93
91
|
return constantInfo.getDatabase(refName)[modelType];
|
|
94
92
|
}
|
|
95
93
|
};
|
|
96
|
-
const cnstOf = (refName, inputRef, objectRef, fullRef, lightRef, insightRef
|
|
94
|
+
const cnstOf = (refName, inputRef, objectRef, fullRef, lightRef, insightRef) => {
|
|
97
95
|
[inputRef, objectRef, fullRef, lightRef, insightRef].forEach((modelRef) => {
|
|
98
96
|
constantInfo.modelRefNameMap.set(modelRef, refName);
|
|
99
97
|
});
|
|
@@ -109,12 +107,6 @@ const cnstOf = (refName, inputRef, objectRef, fullRef, lightRef, insightRef, opt
|
|
|
109
107
|
full: fullRef,
|
|
110
108
|
light: lightRef,
|
|
111
109
|
insight: insightRef,
|
|
112
|
-
crystalize: (0, import__.makeCrystalize)(fullRef, option),
|
|
113
|
-
lightCrystalize: (0, import__.makeCrystalize)(lightRef, option),
|
|
114
|
-
crystalizeInsight: (0, import__.makeCrystalize)(insightRef, option),
|
|
115
|
-
purify: (0, import__.makePurify)(inputRef, option),
|
|
116
|
-
getDefault: () => (0, import_immerify.immerify)(fullRef, Object.assign(new fullRef(), (0, import__.makeDefault)(fullRef, option))),
|
|
117
|
-
getDefaultInsight: () => (0, import_immerify.immerify)(insightRef, Object.assign(new insightRef(), (0, import__.makeDefault)(insightRef, option))),
|
|
118
110
|
_CapitalizedT: null,
|
|
119
111
|
_Default: null,
|
|
120
112
|
_DefaultInput: null,
|
|
@@ -135,9 +127,6 @@ const scalarCnstOf = (refName, Model) => {
|
|
|
135
127
|
const cnst = {
|
|
136
128
|
refName,
|
|
137
129
|
model: Model,
|
|
138
|
-
crystalize: (0, import__.makeCrystalize)(Model),
|
|
139
|
-
purify: (0, import__.makePurify)(Model),
|
|
140
|
-
getDefault: () => (0, import_immerify.immerify)(Model, Object.assign(new Model(), (0, import__.makeDefault)(Model))),
|
|
141
130
|
_Default: null,
|
|
142
131
|
_Doc: null,
|
|
143
132
|
_PurifiedInput: null
|
package/cjs/src/crystalize.js
CHANGED
|
@@ -17,14 +17,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
var crystalize_exports = {};
|
|
19
19
|
__export(crystalize_exports, {
|
|
20
|
+
crystalize: () => crystalize,
|
|
20
21
|
makeCrystalize: () => makeCrystalize
|
|
21
22
|
});
|
|
22
23
|
module.exports = __toCommonJS(crystalize_exports);
|
|
23
24
|
var import_base = require("@akanjs/base");
|
|
24
|
-
var import_common = require("@akanjs/common");
|
|
25
25
|
var import__ = require(".");
|
|
26
|
-
class CrystalizeStorage {
|
|
27
|
-
}
|
|
28
26
|
const scalarCrystalizeMap = /* @__PURE__ */ new Map([
|
|
29
27
|
[Date, (value) => (0, import_base.dayjs)(value)],
|
|
30
28
|
[String, (value) => value],
|
|
@@ -34,13 +32,13 @@ const scalarCrystalizeMap = /* @__PURE__ */ new Map([
|
|
|
34
32
|
[import_base.Float, (value) => value],
|
|
35
33
|
[import_base.JSON, (value) => value]
|
|
36
34
|
]);
|
|
37
|
-
const crystalize = (
|
|
35
|
+
const crystalize = (field, value) => {
|
|
38
36
|
if (value === void 0 || value === null)
|
|
39
37
|
return value;
|
|
40
|
-
if (
|
|
41
|
-
return value.map((v) => crystalize({ ...
|
|
42
|
-
if (
|
|
43
|
-
const [valueRef] = (0, import_base.getNonArrayModel)(
|
|
38
|
+
if (field.isArray && Array.isArray(value))
|
|
39
|
+
return value.map((v) => crystalize({ ...field, isArray: false }, v));
|
|
40
|
+
if (field.isMap) {
|
|
41
|
+
const [valueRef] = (0, import_base.getNonArrayModel)(field.of);
|
|
44
42
|
const crystalizeValue = scalarCrystalizeMap.get(valueRef) ?? ((value2) => value2);
|
|
45
43
|
return new Map(
|
|
46
44
|
Object.entries(value).map(([key, val]) => [
|
|
@@ -49,36 +47,23 @@ const crystalize = (metadata, value) => {
|
|
|
49
47
|
])
|
|
50
48
|
);
|
|
51
49
|
}
|
|
52
|
-
if (
|
|
53
|
-
return
|
|
54
|
-
if (
|
|
50
|
+
if (field.isClass)
|
|
51
|
+
return new field.modelRef().set(value);
|
|
52
|
+
if (field.modelRef === Date)
|
|
55
53
|
return (0, import_base.dayjs)(value);
|
|
56
|
-
return (scalarCrystalizeMap.get(
|
|
57
|
-
};
|
|
58
|
-
const getPredefinedCrystalizeFn = (refName) => {
|
|
59
|
-
const crystalize2 = Reflect.getMetadata(refName, CrystalizeStorage.prototype);
|
|
60
|
-
return crystalize2;
|
|
61
|
-
};
|
|
62
|
-
const setPredefinedCrystalizeFn = (refName, crystalize2) => {
|
|
63
|
-
Reflect.defineMetadata(refName, crystalize2, CrystalizeStorage.prototype);
|
|
54
|
+
return (scalarCrystalizeMap.get(field.modelRef) ?? ((value2) => value2))(value);
|
|
64
55
|
};
|
|
65
56
|
const makeCrystalize = (modelRef, option = {}) => {
|
|
66
|
-
const
|
|
67
|
-
const crystalName = `${import__.constantInfo.isLight(modelRef) ? "Light" : ""}${(0, import_common.capitalize)(refName)}${import__.constantInfo.isInsight(modelRef) ? "Insight" : ""}`;
|
|
68
|
-
const crystalizeFn = getPredefinedCrystalizeFn(crystalName);
|
|
69
|
-
if (crystalizeFn && !option.overwrite && !option.partial?.length)
|
|
70
|
-
return crystalizeFn;
|
|
71
|
-
const fieldMetaMap = (0, import__.getFieldMetaMap)(modelRef);
|
|
72
|
-
const fieldKeys = option.partial?.length ? import__.constantInfo.isScalar(modelRef) ? option.partial : ["id", ...option.partial, "updatedAt"] : [...fieldMetaMap.keys()];
|
|
73
|
-
const metadatas = fieldKeys.map((key) => fieldMetaMap.get(key));
|
|
57
|
+
const fieldKeys = option.partial?.length ? import__.constantInfo.isScalar(modelRef) ? option.partial : ["id", ...option.partial, "updatedAt"] : Object.keys(modelRef.field);
|
|
74
58
|
const fn = (self, isChild) => {
|
|
75
59
|
try {
|
|
76
|
-
const result =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
60
|
+
const result = new modelRef().set(self);
|
|
61
|
+
fieldKeys.forEach((key) => {
|
|
62
|
+
const field = modelRef.field[key];
|
|
63
|
+
if (field.fieldType === "hidden")
|
|
64
|
+
return;
|
|
65
|
+
result[key] = crystalize(field.getProps(), self[key]);
|
|
66
|
+
});
|
|
82
67
|
return result;
|
|
83
68
|
} catch (err) {
|
|
84
69
|
if (isChild)
|
|
@@ -86,7 +71,5 @@ const makeCrystalize = (modelRef, option = {}) => {
|
|
|
86
71
|
return null;
|
|
87
72
|
}
|
|
88
73
|
};
|
|
89
|
-
if (!option.partial?.length)
|
|
90
|
-
setPredefinedCrystalizeFn(crystalName, fn);
|
|
91
74
|
return fn;
|
|
92
75
|
};
|
package/cjs/src/default.js
CHANGED
|
@@ -21,45 +21,24 @@ __export(default_exports, {
|
|
|
21
21
|
});
|
|
22
22
|
module.exports = __toCommonJS(default_exports);
|
|
23
23
|
var import_base = require("@akanjs/base");
|
|
24
|
-
|
|
25
|
-
var import_constantInfo = require("./constantInfo");
|
|
26
|
-
var import_scalar = require("./scalar");
|
|
27
|
-
class DefaultStorage {
|
|
28
|
-
}
|
|
29
|
-
const getPredefinedDefault = (refName) => {
|
|
30
|
-
const defaultData = Reflect.getMetadata(refName, DefaultStorage.prototype);
|
|
31
|
-
return defaultData;
|
|
32
|
-
};
|
|
33
|
-
const setPredefinedDefault = (refName, defaultData) => {
|
|
34
|
-
Reflect.defineMetadata(refName, defaultData, DefaultStorage.prototype);
|
|
35
|
-
};
|
|
36
|
-
const makeDefault = (modelRef, option = {}) => {
|
|
37
|
-
const refName = import_constantInfo.constantInfo.getRefName(modelRef);
|
|
38
|
-
const defaultName = `${(0, import_common.capitalize)(refName)}${import_constantInfo.constantInfo.isInsight(modelRef) ? "Insight" : ""}`;
|
|
39
|
-
const predefinedDefault = getPredefinedDefault(defaultName);
|
|
40
|
-
if (predefinedDefault && !option.overwrite)
|
|
41
|
-
return predefinedDefault;
|
|
42
|
-
if (option.isChild && import_constantInfo.constantInfo.isScalar(modelRef))
|
|
43
|
-
return null;
|
|
44
|
-
const metadatas = (0, import_scalar.getFieldMetas)(modelRef);
|
|
24
|
+
const makeDefault = (fieldObj) => {
|
|
45
25
|
const result = {};
|
|
46
|
-
for (const
|
|
47
|
-
if (
|
|
48
|
-
result[
|
|
49
|
-
else if (
|
|
50
|
-
if (typeof
|
|
51
|
-
result[
|
|
26
|
+
for (const [key, field] of Object.entries(fieldObj)) {
|
|
27
|
+
if (field.fieldType === "hidden")
|
|
28
|
+
result[key] = null;
|
|
29
|
+
else if (field.default) {
|
|
30
|
+
if (typeof field.default === "function")
|
|
31
|
+
result[key] = field.default();
|
|
52
32
|
else
|
|
53
|
-
result[
|
|
54
|
-
} else if (
|
|
55
|
-
result[
|
|
56
|
-
else if (
|
|
57
|
-
result[
|
|
58
|
-
else if (
|
|
59
|
-
result[
|
|
33
|
+
result[key] = field.default;
|
|
34
|
+
} else if (field.isArray)
|
|
35
|
+
result[key] = [];
|
|
36
|
+
else if (field.nullable)
|
|
37
|
+
result[key] = null;
|
|
38
|
+
else if (field.isClass)
|
|
39
|
+
result[key] = field.isScalar ? makeDefault(field.modelRef.field) : null;
|
|
60
40
|
else
|
|
61
|
-
result[
|
|
41
|
+
result[key] = import_base.scalarDefaultMap.get(field.modelRef);
|
|
62
42
|
}
|
|
63
|
-
setPredefinedDefault(defaultName, result);
|
|
64
43
|
return result;
|
|
65
44
|
};
|
package/cjs/src/fieldInfo.js
CHANGED
|
@@ -17,13 +17,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
var fieldInfo_exports = {};
|
|
19
19
|
__export(fieldInfo_exports, {
|
|
20
|
+
ConstantField: () => ConstantField,
|
|
20
21
|
field: () => field,
|
|
21
22
|
resolve: () => resolve
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(fieldInfo_exports);
|
|
24
25
|
var import_base = require("@akanjs/base");
|
|
25
26
|
var import_constantInfo = require("./constantInfo");
|
|
26
|
-
var import_scalar = require("./scalar");
|
|
27
27
|
class FieldInfo {
|
|
28
28
|
value;
|
|
29
29
|
type;
|
|
@@ -43,15 +43,81 @@ class FieldInfo {
|
|
|
43
43
|
this.option.meta = meta;
|
|
44
44
|
return this;
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
toField() {
|
|
47
|
+
return ConstantField.fromFieldInfo(this);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class ConstantField {
|
|
51
|
+
static getBaseModelField() {
|
|
52
|
+
return {
|
|
53
|
+
id: field(import_base.ID).toField(),
|
|
54
|
+
createdAt: field(Date).toField(),
|
|
55
|
+
updatedAt: field(Date).toField(),
|
|
56
|
+
removedAt: field(Date).optional().toField()
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
static getBaseInsightField() {
|
|
60
|
+
return {
|
|
61
|
+
count: field(import_base.Int, { default: 0, accumulate: { $sum: 1 } }).toField()
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
nullable;
|
|
65
|
+
ref;
|
|
66
|
+
refPath;
|
|
67
|
+
refType;
|
|
68
|
+
default;
|
|
69
|
+
type;
|
|
70
|
+
fieldType;
|
|
71
|
+
immutable;
|
|
72
|
+
min;
|
|
73
|
+
max;
|
|
74
|
+
enum;
|
|
75
|
+
select;
|
|
76
|
+
minlength;
|
|
77
|
+
maxlength;
|
|
78
|
+
accumulate;
|
|
79
|
+
example;
|
|
80
|
+
of;
|
|
81
|
+
// for Map type fields
|
|
82
|
+
validate;
|
|
83
|
+
text;
|
|
84
|
+
modelRef;
|
|
85
|
+
arrDepth;
|
|
86
|
+
optArrDepth;
|
|
87
|
+
meta;
|
|
88
|
+
constructor(props) {
|
|
89
|
+
this.nullable = props.nullable;
|
|
90
|
+
this.ref = props.ref;
|
|
91
|
+
this.refPath = props.refPath;
|
|
92
|
+
this.refType = props.refType;
|
|
93
|
+
this.default = props.default;
|
|
94
|
+
this.type = props.type;
|
|
95
|
+
this.fieldType = props.fieldType;
|
|
96
|
+
this.immutable = props.immutable;
|
|
97
|
+
this.min = props.min;
|
|
98
|
+
this.max = props.max;
|
|
99
|
+
this.enum = props.enum;
|
|
100
|
+
this.select = props.select;
|
|
101
|
+
this.minlength = props.minlength;
|
|
102
|
+
this.maxlength = props.maxlength;
|
|
103
|
+
this.accumulate = props.accumulate;
|
|
104
|
+
this.example = props.example;
|
|
105
|
+
this.of = props.of;
|
|
106
|
+
this.validate = props.validate;
|
|
107
|
+
this.text = props.text;
|
|
108
|
+
this.modelRef = props.modelRef;
|
|
109
|
+
this.arrDepth = props.arrDepth;
|
|
110
|
+
this.optArrDepth = props.optArrDepth;
|
|
111
|
+
this.meta = props.meta;
|
|
112
|
+
}
|
|
113
|
+
static fromFieldInfo(fieldInfo) {
|
|
114
|
+
const [modelRef, arrDepth] = (0, import_base.getNonArrayModel)(fieldInfo.type);
|
|
115
|
+
const [option, optArrDepth] = (0, import_base.getNonArrayModel)(fieldInfo.option);
|
|
49
116
|
const isArray = arrDepth > 0;
|
|
50
|
-
const isClass = !(0, import_base.isGqlScalar)(modelRef);
|
|
51
117
|
const isMap = (0, import_base.isGqlMap)(modelRef);
|
|
52
118
|
if (isMap && !option.of)
|
|
53
119
|
throw new Error("Map type must have 'of' option");
|
|
54
|
-
|
|
120
|
+
return new ConstantField({
|
|
55
121
|
nullable: option.nullable ?? (option.default === "" ? true : false),
|
|
56
122
|
ref: option.ref,
|
|
57
123
|
refPath: option.refPath,
|
|
@@ -68,22 +134,57 @@ class FieldInfo {
|
|
|
68
134
|
maxlength: option.maxlength,
|
|
69
135
|
accumulate: option.accumulate,
|
|
70
136
|
example: option.example,
|
|
137
|
+
of: option.of,
|
|
71
138
|
validate: option.validate,
|
|
72
|
-
|
|
73
|
-
isClass,
|
|
74
|
-
isScalar: import_constantInfo.constantInfo.isScalar(modelRef),
|
|
139
|
+
text: option.text,
|
|
75
140
|
modelRef,
|
|
76
141
|
arrDepth,
|
|
77
|
-
isArray,
|
|
78
142
|
optArrDepth,
|
|
79
|
-
isMap,
|
|
80
|
-
of: option.of,
|
|
81
|
-
text: option.text,
|
|
82
143
|
meta: option.meta ?? {}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
get isClass() {
|
|
147
|
+
return !(0, import_base.isGqlScalar)(this.modelRef);
|
|
148
|
+
}
|
|
149
|
+
get isScalar() {
|
|
150
|
+
return import_constantInfo.constantInfo.isScalar(this.modelRef) || (0, import_base.isGqlScalar)(this.modelRef);
|
|
151
|
+
}
|
|
152
|
+
get isArray() {
|
|
153
|
+
return this.arrDepth > 0;
|
|
154
|
+
}
|
|
155
|
+
get isMap() {
|
|
156
|
+
return (0, import_base.isGqlMap)(this.modelRef);
|
|
157
|
+
}
|
|
158
|
+
getProps() {
|
|
159
|
+
return {
|
|
160
|
+
nullable: this.nullable,
|
|
161
|
+
ref: this.ref,
|
|
162
|
+
refPath: this.refPath,
|
|
163
|
+
refType: this.refType,
|
|
164
|
+
default: this.default,
|
|
165
|
+
type: this.type,
|
|
166
|
+
fieldType: this.fieldType,
|
|
167
|
+
immutable: this.immutable,
|
|
168
|
+
min: this.min,
|
|
169
|
+
max: this.max,
|
|
170
|
+
enum: this.enum,
|
|
171
|
+
select: this.select,
|
|
172
|
+
minlength: this.minlength,
|
|
173
|
+
maxlength: this.maxlength,
|
|
174
|
+
accumulate: this.accumulate,
|
|
175
|
+
example: this.example,
|
|
176
|
+
of: this.of,
|
|
177
|
+
validate: this.validate,
|
|
178
|
+
text: this.text,
|
|
179
|
+
modelRef: this.modelRef,
|
|
180
|
+
arrDepth: this.arrDepth,
|
|
181
|
+
optArrDepth: this.optArrDepth,
|
|
182
|
+
meta: this.meta,
|
|
183
|
+
isClass: this.isClass,
|
|
184
|
+
isScalar: this.isScalar,
|
|
185
|
+
isArray: this.isArray,
|
|
186
|
+
isMap: this.isMap
|
|
83
187
|
};
|
|
84
|
-
const metadataMap = (0, import_scalar.getFieldMetaMapOnPrototype)(target.prototype);
|
|
85
|
-
metadataMap.set(key, metadata);
|
|
86
|
-
(0, import_scalar.setFieldMetaMapOnPrototype)(target.prototype, metadataMap);
|
|
87
188
|
}
|
|
88
189
|
}
|
|
89
190
|
const field = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "property" });
|
package/cjs/src/immerify.js
CHANGED
|
@@ -21,16 +21,14 @@ __export(immerify_exports, {
|
|
|
21
21
|
});
|
|
22
22
|
module.exports = __toCommonJS(immerify_exports);
|
|
23
23
|
var import_immer = require("immer");
|
|
24
|
-
var import__ = require(".");
|
|
25
24
|
const immerify = (modelRef, objOrArr) => {
|
|
26
25
|
if (Array.isArray(objOrArr))
|
|
27
26
|
return objOrArr.map((val) => immerify(modelRef, val));
|
|
28
|
-
const fieldMetas = (0, import__.getFieldMetas)(modelRef);
|
|
29
27
|
const immeredObj = Object.assign({}, objOrArr, { [import_immer.immerable]: true });
|
|
30
28
|
const objRecord = objOrArr;
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
immeredObj[
|
|
29
|
+
Object.entries(modelRef.field).forEach(([key, field]) => {
|
|
30
|
+
if (field.isScalar && field.isClass && !!objRecord[key])
|
|
31
|
+
immeredObj[key] = immerify(field.modelRef, objRecord[key]);
|
|
34
32
|
});
|
|
35
33
|
return immeredObj;
|
|
36
34
|
};
|