@akanjs/constant 0.0.54 → 0.0.56
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/index.mjs +1 -0
- package/package.json +7 -1
- package/src/baseGql.mjs +154 -0
- package/src/classMeta.mjs +128 -0
- package/src/constantDecorator.mjs +21 -0
- package/src/fieldMeta.mjs +67 -0
- package/src/filterMeta.mjs +169 -0
- package/src/index.mjs +7 -0
- package/src/scalar.mjs +76 -0
- package/src/types.mjs +10 -0
package/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akanjs/constant",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.56",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,5 +16,11 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"reflect-metadata": "^0.2.2"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"require": "./index.js",
|
|
23
|
+
"import": "./index.mjs"
|
|
24
|
+
}
|
|
19
25
|
}
|
|
20
26
|
}
|
package/src/baseGql.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { ID } from "@akanjs/base";
|
|
3
|
+
import { applyMixins } from "@akanjs/common";
|
|
4
|
+
import {
|
|
5
|
+
getFilterArgMetas,
|
|
6
|
+
getFilterMeta,
|
|
7
|
+
getFilterQueryMap,
|
|
8
|
+
getFilterSortMap,
|
|
9
|
+
isFilterModel,
|
|
10
|
+
setFilterArgMetasOnPrototype,
|
|
11
|
+
setFilterKeyMetaMapOnPrototype,
|
|
12
|
+
setFilterMeta
|
|
13
|
+
} from "./filterMeta";
|
|
14
|
+
import { getFieldMetaMap, isConstantModel, setFieldMetaMap } from "./scalar";
|
|
15
|
+
const defaultFieldMeta = {
|
|
16
|
+
fieldType: "property",
|
|
17
|
+
immutable: false,
|
|
18
|
+
select: true,
|
|
19
|
+
isClass: false,
|
|
20
|
+
isScalar: true,
|
|
21
|
+
nullable: false,
|
|
22
|
+
isArray: false,
|
|
23
|
+
arrDepth: 0,
|
|
24
|
+
optArrDepth: 0,
|
|
25
|
+
default: null,
|
|
26
|
+
isMap: false
|
|
27
|
+
};
|
|
28
|
+
const idFieldMeta = { ...defaultFieldMeta, key: "id", name: "ID", modelRef: ID };
|
|
29
|
+
const createdAtFieldMeta = { ...defaultFieldMeta, key: "createdAt", name: "Date", modelRef: Date };
|
|
30
|
+
const updatedAtFieldMeta = { ...defaultFieldMeta, key: "updatedAt", name: "Date", modelRef: Date };
|
|
31
|
+
const removedAtFieldMeta = {
|
|
32
|
+
...defaultFieldMeta,
|
|
33
|
+
key: "removedAt",
|
|
34
|
+
name: "Date",
|
|
35
|
+
modelRef: Date,
|
|
36
|
+
nullable: true,
|
|
37
|
+
default: null
|
|
38
|
+
};
|
|
39
|
+
const extendModel = (modelRef) => {
|
|
40
|
+
class BaseModel {
|
|
41
|
+
}
|
|
42
|
+
const metadataMap = getFieldMetaMap(modelRef);
|
|
43
|
+
setFieldMetaMap(BaseModel, metadataMap);
|
|
44
|
+
return BaseModel;
|
|
45
|
+
};
|
|
46
|
+
const as = extendModel;
|
|
47
|
+
const baseModelOf = (modelRef) => {
|
|
48
|
+
class BaseModel {
|
|
49
|
+
__ModelType__ = "full";
|
|
50
|
+
}
|
|
51
|
+
const metadataMap = getFieldMetaMap(modelRef);
|
|
52
|
+
metadataMap.set("id", idFieldMeta);
|
|
53
|
+
metadataMap.set("createdAt", createdAtFieldMeta);
|
|
54
|
+
metadataMap.set("updatedAt", updatedAtFieldMeta);
|
|
55
|
+
metadataMap.set("removedAt", removedAtFieldMeta);
|
|
56
|
+
Reflect.defineMetadata("fields", metadataMap, BaseModel.prototype);
|
|
57
|
+
return BaseModel;
|
|
58
|
+
};
|
|
59
|
+
const lightModelOf = (objectRef, fields) => {
|
|
60
|
+
const map = /* @__PURE__ */ new Map();
|
|
61
|
+
const metadataMap = getFieldMetaMap(objectRef);
|
|
62
|
+
class BaseGql {
|
|
63
|
+
__ModelType__ = "light";
|
|
64
|
+
}
|
|
65
|
+
["id", ...fields, "createdAt", "updatedAt", "removedAt"].forEach((key) => map.set(key, metadataMap.get(key)));
|
|
66
|
+
Reflect.defineMetadata("fields", map, BaseGql.prototype);
|
|
67
|
+
return BaseGql;
|
|
68
|
+
};
|
|
69
|
+
const fullModelOf = (modelRef, lightRef, overwriteRef, overwriteLightRef) => {
|
|
70
|
+
const modelFieldMetaMap = getFieldMetaMap(modelRef);
|
|
71
|
+
const lightFieldMetaMap = getFieldMetaMap(lightRef);
|
|
72
|
+
applyMixins(modelRef, [lightRef]);
|
|
73
|
+
if (overwriteRef) {
|
|
74
|
+
applyMixins(overwriteRef, [modelRef]);
|
|
75
|
+
setFieldMetaMap(overwriteRef, modelFieldMetaMap);
|
|
76
|
+
}
|
|
77
|
+
if (overwriteLightRef) {
|
|
78
|
+
applyMixins(overwriteLightRef, [lightRef]);
|
|
79
|
+
setFieldMetaMap(overwriteLightRef, lightFieldMetaMap);
|
|
80
|
+
}
|
|
81
|
+
setFieldMetaMap(modelRef, new Map([...modelFieldMetaMap, ...lightFieldMetaMap]));
|
|
82
|
+
return modelRef;
|
|
83
|
+
};
|
|
84
|
+
const via = (modelRef, fieldsOrLightModelRef, overwriteRef, overwriteLightRef) => {
|
|
85
|
+
if (!fieldsOrLightModelRef)
|
|
86
|
+
return baseModelOf(modelRef);
|
|
87
|
+
else if (Array.isArray(fieldsOrLightModelRef))
|
|
88
|
+
return lightModelOf(modelRef, fieldsOrLightModelRef);
|
|
89
|
+
else
|
|
90
|
+
return fullModelOf(modelRef, fieldsOrLightModelRef, overwriteRef, overwriteLightRef);
|
|
91
|
+
};
|
|
92
|
+
const addModelOf = (modelRef, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => {
|
|
93
|
+
const modelMetadataMap = getFieldMetaMap(modelRef);
|
|
94
|
+
const metadataMap = new Map(
|
|
95
|
+
[t1, t2, t3, t4, t5, t6, t7, t8, t9, t10].filter((t) => !!t).reduce((acc, writeRef) => {
|
|
96
|
+
const writeMetadataMap = getFieldMetaMap(writeRef);
|
|
97
|
+
applyMixins(modelRef, [writeRef]);
|
|
98
|
+
return new Map([...acc, ...writeMetadataMap]);
|
|
99
|
+
}, modelMetadataMap)
|
|
100
|
+
);
|
|
101
|
+
setFieldMetaMap(modelRef, metadataMap);
|
|
102
|
+
return modelRef;
|
|
103
|
+
};
|
|
104
|
+
const addFilterOf = (filterRef, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => {
|
|
105
|
+
const filterMeta = getFilterMeta(filterRef);
|
|
106
|
+
const filterQueryMap = getFilterQueryMap(filterRef);
|
|
107
|
+
const metadataMap = new Map(
|
|
108
|
+
[t1, t2, t3, t4, t5, t6, t7, t8, t9, t10].filter((t) => !!t).reduce((acc, writeRef) => {
|
|
109
|
+
const writeMetadataMap = getFilterQueryMap(writeRef);
|
|
110
|
+
applyMixins(filterRef, [writeRef]);
|
|
111
|
+
writeMetadataMap.forEach((value, key) => {
|
|
112
|
+
const filterArgMetas = getFilterArgMetas(writeRef, key);
|
|
113
|
+
setFilterArgMetasOnPrototype(filterRef.prototype, key, filterArgMetas);
|
|
114
|
+
});
|
|
115
|
+
return new Map([...acc, ...writeMetadataMap]);
|
|
116
|
+
}, filterQueryMap)
|
|
117
|
+
);
|
|
118
|
+
const filterSort = [t1, t2, t3, t4, t5, t6, t7, t8, t9, t10].filter((t) => !!t).map((t) => getFilterSortMap(t)).reduce((acc, sort) => {
|
|
119
|
+
Object.assign(acc, sort);
|
|
120
|
+
return acc;
|
|
121
|
+
}, filterMeta.sort);
|
|
122
|
+
setFilterKeyMetaMapOnPrototype(filterRef.prototype, metadataMap);
|
|
123
|
+
setFilterMeta(filterRef, { ...filterMeta, sort: filterSort });
|
|
124
|
+
return filterRef;
|
|
125
|
+
};
|
|
126
|
+
const from = (modelRef, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => {
|
|
127
|
+
if (isConstantModel(modelRef))
|
|
128
|
+
return addModelOf(modelRef, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
|
|
129
|
+
else if (isFilterModel(modelRef))
|
|
130
|
+
return addFilterOf(modelRef, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
|
|
131
|
+
else
|
|
132
|
+
throw new Error("Invalid modelRef");
|
|
133
|
+
};
|
|
134
|
+
const mixModelOf = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20) => {
|
|
135
|
+
class Mix {
|
|
136
|
+
}
|
|
137
|
+
const metadataMap = new Map(
|
|
138
|
+
[t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20].filter((t) => !!t).reduce((acc, modelRef) => {
|
|
139
|
+
const modelMetadataMap = getFieldMetaMap(modelRef);
|
|
140
|
+
applyMixins(Mix, [modelRef]);
|
|
141
|
+
return [...acc, ...modelMetadataMap];
|
|
142
|
+
}, [])
|
|
143
|
+
);
|
|
144
|
+
setFieldMetaMap(Mix, metadataMap);
|
|
145
|
+
return Mix;
|
|
146
|
+
};
|
|
147
|
+
const over = mixModelOf;
|
|
148
|
+
export {
|
|
149
|
+
as,
|
|
150
|
+
from,
|
|
151
|
+
mixModelOf,
|
|
152
|
+
over,
|
|
153
|
+
via
|
|
154
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { capitalize } from "@akanjs/common";
|
|
3
|
+
import { setFilterMeta } from "./filterMeta";
|
|
4
|
+
import { getClassMeta, getFieldMetas } from "./scalar";
|
|
5
|
+
class InputModelStorage {
|
|
6
|
+
}
|
|
7
|
+
class LightModelStorage {
|
|
8
|
+
}
|
|
9
|
+
class FullModelStorage {
|
|
10
|
+
}
|
|
11
|
+
class ScalarModelStorage {
|
|
12
|
+
}
|
|
13
|
+
class FilterModelStorage {
|
|
14
|
+
}
|
|
15
|
+
const getFullModelRef = (refName) => {
|
|
16
|
+
const modelRef = Reflect.getMetadata(capitalize(refName), FullModelStorage.prototype);
|
|
17
|
+
if (!modelRef)
|
|
18
|
+
throw new Error(`FullModel not found - ${refName}`);
|
|
19
|
+
return modelRef;
|
|
20
|
+
};
|
|
21
|
+
const getInputModelRef = (refName) => {
|
|
22
|
+
const modelRef = Reflect.getMetadata(capitalize(refName), InputModelStorage.prototype);
|
|
23
|
+
if (!modelRef)
|
|
24
|
+
throw new Error(`InputModel not found - ${refName}`);
|
|
25
|
+
return modelRef;
|
|
26
|
+
};
|
|
27
|
+
const getScalarModelRef = (refName) => {
|
|
28
|
+
const modelRef = Reflect.getMetadata(capitalize(refName), ScalarModelStorage.prototype);
|
|
29
|
+
if (!modelRef)
|
|
30
|
+
throw new Error(`ScalarModel not found - ${refName}`);
|
|
31
|
+
return modelRef;
|
|
32
|
+
};
|
|
33
|
+
const getChildClassRefs = (target) => {
|
|
34
|
+
const metadatas = getFieldMetas(target);
|
|
35
|
+
const refMap = /* @__PURE__ */ new Map();
|
|
36
|
+
const childRefs = metadatas.filter((metadata) => metadata.isClass).reduce((acc, metadata) => {
|
|
37
|
+
return [...acc, metadata.modelRef, ...getChildClassRefs(metadata.modelRef)];
|
|
38
|
+
}, []);
|
|
39
|
+
childRefs.filter((modelRef, idx) => childRefs.findIndex((ref) => ref.prototype === modelRef.prototype) === idx).map((modelRef) => refMap.set(getClassMeta(modelRef).refName, modelRef));
|
|
40
|
+
return [...refMap.values()];
|
|
41
|
+
};
|
|
42
|
+
const getFieldEnumMetas = (modelRef) => {
|
|
43
|
+
const fieldMetas = getFieldMetas(modelRef);
|
|
44
|
+
return fieldMetas.filter((fieldMeta) => !!fieldMeta.enum).map((fieldMeta) => ({ key: fieldMeta.key, enum: fieldMeta.enum }));
|
|
45
|
+
};
|
|
46
|
+
const hasTextField = (modelRef) => {
|
|
47
|
+
const fieldMetas = getFieldMetas(modelRef);
|
|
48
|
+
return fieldMetas.some(
|
|
49
|
+
(fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
const applyClassMeta = (type, modelType, storage) => {
|
|
53
|
+
return function(refName) {
|
|
54
|
+
return function(target) {
|
|
55
|
+
const modelRef = target;
|
|
56
|
+
const classMeta = { refName, type, modelType, modelRef, hasTextField: hasTextField(modelRef) };
|
|
57
|
+
Reflect.defineMetadata("class", classMeta, modelRef.prototype);
|
|
58
|
+
Reflect.defineMetadata(refName, modelRef, storage.prototype);
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const applyFilterMeta = (storage) => {
|
|
63
|
+
return function(refName) {
|
|
64
|
+
return function(target) {
|
|
65
|
+
const modelRef = target;
|
|
66
|
+
setFilterMeta(modelRef, { refName, sort: {} });
|
|
67
|
+
Reflect.defineMetadata(refName, modelRef, storage.prototype);
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const Model = {
|
|
72
|
+
Light: applyClassMeta("light", "data", LightModelStorage),
|
|
73
|
+
Object: applyClassMeta("full", "ephemeral", FullModelStorage),
|
|
74
|
+
Full: applyClassMeta("full", "data", FullModelStorage),
|
|
75
|
+
Input: applyClassMeta("input", "data", InputModelStorage),
|
|
76
|
+
Scalar: applyClassMeta("scalar", "data", ScalarModelStorage),
|
|
77
|
+
Summary: applyClassMeta("scalar", "summary", ScalarModelStorage),
|
|
78
|
+
Insight: applyClassMeta("scalar", "insight", ScalarModelStorage),
|
|
79
|
+
Filter: applyFilterMeta(FilterModelStorage)
|
|
80
|
+
};
|
|
81
|
+
const getLightModelRef = (modelRef) => {
|
|
82
|
+
const classMeta = getClassMeta(modelRef);
|
|
83
|
+
if (classMeta.type !== "full")
|
|
84
|
+
return modelRef;
|
|
85
|
+
const lightModelRef = Reflect.getMetadata(`Light${classMeta.refName}`, LightModelStorage.prototype);
|
|
86
|
+
if (!lightModelRef)
|
|
87
|
+
throw new Error(`LightModel not found - ${classMeta.refName}`);
|
|
88
|
+
return lightModelRef;
|
|
89
|
+
};
|
|
90
|
+
const getAllFullModelRefs = () => {
|
|
91
|
+
const modelNames = Reflect.getMetadataKeys(FullModelStorage.prototype);
|
|
92
|
+
const modelRefs = modelNames.map(
|
|
93
|
+
(modelName) => Reflect.getMetadata(modelName, FullModelStorage.prototype)
|
|
94
|
+
);
|
|
95
|
+
return modelRefs;
|
|
96
|
+
};
|
|
97
|
+
const getAllScalarModelRefs = () => {
|
|
98
|
+
const modelNames = Reflect.getMetadataKeys(ScalarModelStorage.prototype);
|
|
99
|
+
const modelRefs = modelNames.map(
|
|
100
|
+
(modelName) => Reflect.getMetadata(modelName, ScalarModelStorage.prototype)
|
|
101
|
+
);
|
|
102
|
+
return modelRefs;
|
|
103
|
+
};
|
|
104
|
+
const getAllFilterModelRefs = () => {
|
|
105
|
+
const modelNames = Reflect.getMetadataKeys(FilterModelStorage.prototype);
|
|
106
|
+
const modelRefs = modelNames.map(
|
|
107
|
+
(modelName) => Reflect.getMetadata(modelName, FilterModelStorage.prototype)
|
|
108
|
+
);
|
|
109
|
+
return modelRefs;
|
|
110
|
+
};
|
|
111
|
+
export {
|
|
112
|
+
FilterModelStorage,
|
|
113
|
+
FullModelStorage,
|
|
114
|
+
InputModelStorage,
|
|
115
|
+
LightModelStorage,
|
|
116
|
+
Model,
|
|
117
|
+
ScalarModelStorage,
|
|
118
|
+
getAllFilterModelRefs,
|
|
119
|
+
getAllFullModelRefs,
|
|
120
|
+
getAllScalarModelRefs,
|
|
121
|
+
getChildClassRefs,
|
|
122
|
+
getFieldEnumMetas,
|
|
123
|
+
getFullModelRef,
|
|
124
|
+
getInputModelRef,
|
|
125
|
+
getLightModelRef,
|
|
126
|
+
getScalarModelRef,
|
|
127
|
+
hasTextField
|
|
128
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
class CnstStorage {
|
|
3
|
+
}
|
|
4
|
+
const setCnstMeta = (refName, cnst) => {
|
|
5
|
+
Reflect.defineMetadata(refName, cnst, CnstStorage.prototype);
|
|
6
|
+
};
|
|
7
|
+
const getCnstMeta = (refName) => {
|
|
8
|
+
const cnst = Reflect.getMetadata(refName, CnstStorage.prototype);
|
|
9
|
+
if (!cnst)
|
|
10
|
+
throw new Error(`No cnst meta for ${refName}`);
|
|
11
|
+
return cnst;
|
|
12
|
+
};
|
|
13
|
+
const cnstOf = (refName, Input, Full, Light, Insight, Filter, Summary) => {
|
|
14
|
+
const cnst = { refName, Input, Full, Light, Insight, Filter, Summary };
|
|
15
|
+
setCnstMeta(refName, cnst);
|
|
16
|
+
return cnst;
|
|
17
|
+
};
|
|
18
|
+
export {
|
|
19
|
+
cnstOf,
|
|
20
|
+
getCnstMeta
|
|
21
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { getNonArrayModel, isGqlMap, isGqlScalar, scalarNameMap } from "@akanjs/base";
|
|
2
|
+
import {
|
|
3
|
+
getClassMeta,
|
|
4
|
+
getFieldMetaMapOnPrototype,
|
|
5
|
+
setFieldMetaMapOnPrototype
|
|
6
|
+
} from "./scalar";
|
|
7
|
+
const applyFieldMeta = (modelRef, arrDepth, option, optionArrDepth) => {
|
|
8
|
+
const isArray = arrDepth > 0;
|
|
9
|
+
const isClass = !isGqlScalar(modelRef);
|
|
10
|
+
const isMap = isGqlMap(modelRef);
|
|
11
|
+
const { refName, type } = isClass ? getClassMeta(modelRef) : { refName: "", type: "scalar" };
|
|
12
|
+
const name = isClass ? refName : scalarNameMap.get(modelRef) ?? "Unknown";
|
|
13
|
+
if (isMap && !option.of)
|
|
14
|
+
throw new Error("Map type must have 'of' option");
|
|
15
|
+
return (prototype, key) => {
|
|
16
|
+
const metadata = {
|
|
17
|
+
nullable: option.nullable ?? false,
|
|
18
|
+
ref: option.ref,
|
|
19
|
+
refPath: option.refPath,
|
|
20
|
+
refType: option.refType,
|
|
21
|
+
default: option.default ?? (isArray ? [] : null),
|
|
22
|
+
type: option.type,
|
|
23
|
+
fieldType: option.fieldType ?? "property",
|
|
24
|
+
immutable: option.immutable ?? false,
|
|
25
|
+
min: option.min,
|
|
26
|
+
max: option.max,
|
|
27
|
+
enum: option.enum,
|
|
28
|
+
select: option.select ?? true,
|
|
29
|
+
minlength: option.minlength,
|
|
30
|
+
maxlength: option.maxlength,
|
|
31
|
+
query: option.query,
|
|
32
|
+
accumulate: option.accumulate,
|
|
33
|
+
example: option.example,
|
|
34
|
+
validate: option.validate,
|
|
35
|
+
key,
|
|
36
|
+
name,
|
|
37
|
+
isClass,
|
|
38
|
+
isScalar: type === "scalar",
|
|
39
|
+
modelRef,
|
|
40
|
+
arrDepth,
|
|
41
|
+
isArray,
|
|
42
|
+
optArrDepth: optionArrDepth,
|
|
43
|
+
isMap,
|
|
44
|
+
of: option.of,
|
|
45
|
+
text: option.text
|
|
46
|
+
};
|
|
47
|
+
const metadataMap = getFieldMetaMapOnPrototype(prototype);
|
|
48
|
+
metadataMap.set(key, metadata);
|
|
49
|
+
setFieldMetaMapOnPrototype(prototype, metadataMap);
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const makeField = (customOption) => (returns, fieldOption) => {
|
|
53
|
+
const [modelRef, arrDepth] = getNonArrayModel(returns());
|
|
54
|
+
if (!fieldOption)
|
|
55
|
+
return applyFieldMeta(modelRef, arrDepth, { ...customOption }, arrDepth);
|
|
56
|
+
const [opt, optArrDepth] = getNonArrayModel(fieldOption);
|
|
57
|
+
return applyFieldMeta(modelRef, arrDepth, { ...opt, ...customOption }, optArrDepth);
|
|
58
|
+
};
|
|
59
|
+
const Field = {
|
|
60
|
+
Prop: makeField({ fieldType: "property" }),
|
|
61
|
+
Hidden: makeField({ fieldType: "hidden", nullable: true }),
|
|
62
|
+
Secret: makeField({ fieldType: "hidden", select: false, nullable: true }),
|
|
63
|
+
Resolve: makeField({ fieldType: "resolve" })
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
Field
|
|
67
|
+
};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result)
|
|
9
|
+
__defProp(target, key, result);
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
13
|
+
import { getNonArrayModel } from "@akanjs/base";
|
|
14
|
+
import {
|
|
15
|
+
getFieldMetaMap
|
|
16
|
+
} from "./scalar";
|
|
17
|
+
const isFilterModel = (filterRef) => {
|
|
18
|
+
return Reflect.getMetadata("filter", filterRef.prototype) !== void 0;
|
|
19
|
+
};
|
|
20
|
+
const getFilterMeta = (filterRef) => {
|
|
21
|
+
const filterMeta = Reflect.getMetadata("filter", filterRef.prototype);
|
|
22
|
+
if (!filterMeta)
|
|
23
|
+
throw new Error("filterMeta is not defined");
|
|
24
|
+
return filterMeta;
|
|
25
|
+
};
|
|
26
|
+
const setFilterMeta = (filterRef, filterMeta) => {
|
|
27
|
+
const existingFilterMeta = Reflect.getMetadata("filter", filterRef.prototype);
|
|
28
|
+
if (existingFilterMeta)
|
|
29
|
+
Object.assign(filterMeta.sort, existingFilterMeta.sort);
|
|
30
|
+
Reflect.defineMetadata("filter", filterMeta, filterRef.prototype);
|
|
31
|
+
};
|
|
32
|
+
const getFilterKeyMetaMapOnPrototype = (prototype) => {
|
|
33
|
+
const metadataMap = Reflect.getMetadata("filterKey", prototype) ?? /* @__PURE__ */ new Map();
|
|
34
|
+
return new Map(metadataMap);
|
|
35
|
+
};
|
|
36
|
+
const setFilterKeyMetaMapOnPrototype = (prototype, metadataMap) => {
|
|
37
|
+
Reflect.defineMetadata("filterKey", new Map(metadataMap), prototype);
|
|
38
|
+
};
|
|
39
|
+
const applyFilterKeyMeta = (option) => {
|
|
40
|
+
return (prototype, key, descriptor) => {
|
|
41
|
+
const metadata = { key, ...option, descriptor };
|
|
42
|
+
const metadataMap = getFilterKeyMetaMapOnPrototype(prototype);
|
|
43
|
+
metadataMap.set(key, metadata);
|
|
44
|
+
setFilterKeyMetaMapOnPrototype(prototype, metadataMap);
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const makeFilter = (customOption) => (fieldOption) => {
|
|
48
|
+
return applyFilterKeyMeta({ ...customOption, ...fieldOption });
|
|
49
|
+
};
|
|
50
|
+
const getFilterArgMetasOnPrototype = (prototype, key) => {
|
|
51
|
+
const filterArgMetas = Reflect.getMetadata("filterArg", prototype, key) ?? [];
|
|
52
|
+
return filterArgMetas;
|
|
53
|
+
};
|
|
54
|
+
const setFilterArgMetasOnPrototype = (prototype, key, filterArgMetas) => {
|
|
55
|
+
Reflect.defineMetadata("filterArg", filterArgMetas, prototype, key);
|
|
56
|
+
};
|
|
57
|
+
const getFilterArgMetas = (filterRef, key) => {
|
|
58
|
+
const filterArgMetas = getFilterArgMetasOnPrototype(filterRef.prototype, key);
|
|
59
|
+
return filterArgMetas;
|
|
60
|
+
};
|
|
61
|
+
const applyFilterArgMeta = (name, returns, argOption) => {
|
|
62
|
+
return (prototype, key, idx) => {
|
|
63
|
+
const [modelRef, arrDepth] = getNonArrayModel(returns());
|
|
64
|
+
const [opt, optArrDepth] = getNonArrayModel(argOption ?? {});
|
|
65
|
+
const filterArgMeta = { name, ...opt, modelRef, arrDepth, isArray: arrDepth > 0, optArrDepth };
|
|
66
|
+
const filterArgMetas = getFilterArgMetasOnPrototype(prototype, key);
|
|
67
|
+
filterArgMetas[idx] = filterArgMeta;
|
|
68
|
+
setFilterArgMetasOnPrototype(prototype, key, filterArgMetas);
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const getFilterQuery = (filterRef, key) => {
|
|
72
|
+
const filterKeyMetaMap = getFilterKeyMetaMapOnPrototype(filterRef.prototype);
|
|
73
|
+
const filterKeyMeta = filterKeyMetaMap.get(key);
|
|
74
|
+
if (!filterKeyMeta?.descriptor.value)
|
|
75
|
+
throw new Error(`filterKeyMeta is not defined for key: ${key}`);
|
|
76
|
+
return filterKeyMeta.descriptor.value;
|
|
77
|
+
};
|
|
78
|
+
const getFilterQueryMap = (filterRef) => {
|
|
79
|
+
const filterKeyMetaMap = getFilterKeyMetaMapOnPrototype(filterRef.prototype);
|
|
80
|
+
return filterKeyMetaMap;
|
|
81
|
+
};
|
|
82
|
+
const getFilterSort = (filterRef, key) => {
|
|
83
|
+
const filterMeta = getFilterMeta(filterRef);
|
|
84
|
+
const sort = filterMeta.sort[key];
|
|
85
|
+
return sort;
|
|
86
|
+
};
|
|
87
|
+
const getFilterSortMap = (filterRef) => {
|
|
88
|
+
const filterMeta = getFilterMeta(filterRef);
|
|
89
|
+
return filterMeta.sort;
|
|
90
|
+
};
|
|
91
|
+
const Filter = {
|
|
92
|
+
Mongo: makeFilter({ type: "mongo" }),
|
|
93
|
+
// Meili: makeFilter({ fieldType: "hidden", nullable: true }),
|
|
94
|
+
Arg: applyFilterArgMeta
|
|
95
|
+
};
|
|
96
|
+
const sortOf = (modelRef, sort) => {
|
|
97
|
+
const fieldMetaMap = getFieldMetaMap(modelRef);
|
|
98
|
+
const statusFieldMeta = fieldMetaMap.get("status");
|
|
99
|
+
if (!statusFieldMeta)
|
|
100
|
+
throw new Error(`No status field meta fount in ${modelRef.name}`);
|
|
101
|
+
class BaseFilter2 {
|
|
102
|
+
latest = { createdAt: -1 };
|
|
103
|
+
oldest = { createdAt: 1 };
|
|
104
|
+
any() {
|
|
105
|
+
return { removedAt: { $exists: false } };
|
|
106
|
+
}
|
|
107
|
+
byStatuses(statuses) {
|
|
108
|
+
return statuses?.length ? { status: { $in: statuses } } : {};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
__decorateClass([
|
|
112
|
+
Filter.Mongo()
|
|
113
|
+
], BaseFilter2.prototype, "any", 1);
|
|
114
|
+
__decorateClass([
|
|
115
|
+
Filter.Mongo(),
|
|
116
|
+
__decorateParam(0, Filter.Arg("statuses", () => [String], { nullable: true, enum: statusFieldMeta?.enum }))
|
|
117
|
+
], BaseFilter2.prototype, "byStatuses", 1);
|
|
118
|
+
Object.assign(BaseFilter2.prototype, sort);
|
|
119
|
+
setFilterMeta(BaseFilter2, {
|
|
120
|
+
refName: "BaseFilter",
|
|
121
|
+
sort: Object.assign({ latest: { createdAt: -1 }, oldest: { createdAt: 1 } }, sort)
|
|
122
|
+
});
|
|
123
|
+
return BaseFilter2;
|
|
124
|
+
};
|
|
125
|
+
function BaseFilter(modelRef, sort) {
|
|
126
|
+
const fieldMetaMap = getFieldMetaMap(modelRef);
|
|
127
|
+
const statusFieldMeta = fieldMetaMap.get("status");
|
|
128
|
+
if (!statusFieldMeta)
|
|
129
|
+
throw new Error(`No status field meta fount in ${modelRef.name}`);
|
|
130
|
+
class BaseFilter2 {
|
|
131
|
+
latest = { createdAt: -1 };
|
|
132
|
+
oldest = { createdAt: 1 };
|
|
133
|
+
any() {
|
|
134
|
+
return { removedAt: { $exists: false } };
|
|
135
|
+
}
|
|
136
|
+
byStatuses(statuses) {
|
|
137
|
+
return statuses?.length ? { status: { $in: statuses } } : {};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
__decorateClass([
|
|
141
|
+
Filter.Mongo()
|
|
142
|
+
], BaseFilter2.prototype, "any", 1);
|
|
143
|
+
__decorateClass([
|
|
144
|
+
Filter.Mongo(),
|
|
145
|
+
__decorateParam(0, Filter.Arg("statuses", () => [String], { nullable: true, enum: statusFieldMeta?.enum }))
|
|
146
|
+
], BaseFilter2.prototype, "byStatuses", 1);
|
|
147
|
+
Object.assign(BaseFilter2.prototype, sort);
|
|
148
|
+
setFilterMeta(BaseFilter2, {
|
|
149
|
+
refName: "BaseFilter",
|
|
150
|
+
sort: Object.assign({ latest: { createdAt: -1 }, oldest: { createdAt: 1 } }, sort)
|
|
151
|
+
});
|
|
152
|
+
return BaseFilter2;
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
BaseFilter,
|
|
156
|
+
Filter,
|
|
157
|
+
getFilterArgMetas,
|
|
158
|
+
getFilterKeyMetaMapOnPrototype,
|
|
159
|
+
getFilterMeta,
|
|
160
|
+
getFilterQuery,
|
|
161
|
+
getFilterQueryMap,
|
|
162
|
+
getFilterSort,
|
|
163
|
+
getFilterSortMap,
|
|
164
|
+
isFilterModel,
|
|
165
|
+
setFilterArgMetasOnPrototype,
|
|
166
|
+
setFilterKeyMetaMapOnPrototype,
|
|
167
|
+
setFilterMeta,
|
|
168
|
+
sortOf
|
|
169
|
+
};
|
package/src/index.mjs
ADDED
package/src/scalar.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {
|
|
3
|
+
Float,
|
|
4
|
+
getNonArrayModel,
|
|
5
|
+
ID,
|
|
6
|
+
Int,
|
|
7
|
+
JSON,
|
|
8
|
+
scalarNameMap,
|
|
9
|
+
Upload
|
|
10
|
+
} from "@akanjs/base";
|
|
11
|
+
const scalarExampleMap = /* @__PURE__ */ new Map([
|
|
12
|
+
[ID, "1234567890abcdef12345678"],
|
|
13
|
+
[Int, 0],
|
|
14
|
+
[Float, 0],
|
|
15
|
+
[String, "String"],
|
|
16
|
+
[Boolean, true],
|
|
17
|
+
[Date, (/* @__PURE__ */ new Date()).toISOString()],
|
|
18
|
+
[Upload, "FileUpload"],
|
|
19
|
+
[JSON, {}],
|
|
20
|
+
[Map, {}]
|
|
21
|
+
]);
|
|
22
|
+
const getScalarExample = (ref) => scalarExampleMap.get(ref) ?? null;
|
|
23
|
+
const getGqlTypeStr = (ref) => scalarNameMap.get(ref) ?? getClassMeta(ref).refName;
|
|
24
|
+
const fieldTypes = ["email", "password", "url"];
|
|
25
|
+
const getClassMeta = (modelRef) => {
|
|
26
|
+
const [target] = getNonArrayModel(modelRef);
|
|
27
|
+
const classMeta = Reflect.getMetadata("class", target.prototype);
|
|
28
|
+
if (!classMeta)
|
|
29
|
+
throw new Error(`No ClassMeta for this target ${target.name}`);
|
|
30
|
+
return classMeta;
|
|
31
|
+
};
|
|
32
|
+
const getFieldMetas = (modelRef) => {
|
|
33
|
+
const [target] = getNonArrayModel(modelRef);
|
|
34
|
+
const metadataMap = Reflect.getMetadata("fields", target.prototype) ?? /* @__PURE__ */ new Map();
|
|
35
|
+
const keySortMap = { id: -1, createdAt: 1, updatedAt: 2, removedAt: 3 };
|
|
36
|
+
return [...metadataMap.values()].sort((a, b) => (keySortMap[a.key] ?? 0) - (keySortMap[b.key] ?? 0));
|
|
37
|
+
};
|
|
38
|
+
const isConstantModel = (modelRef) => {
|
|
39
|
+
return Reflect.getMetadata("class", modelRef.prototype) !== void 0;
|
|
40
|
+
};
|
|
41
|
+
const getFieldMetaMap = (modelRef) => {
|
|
42
|
+
const [target] = getNonArrayModel(modelRef);
|
|
43
|
+
const metadataMap = Reflect.getMetadata("fields", target.prototype) ?? /* @__PURE__ */ new Map();
|
|
44
|
+
return new Map(metadataMap);
|
|
45
|
+
};
|
|
46
|
+
const setFieldMetaMap = (modelRef, metadataMap) => {
|
|
47
|
+
const [target] = getNonArrayModel(modelRef);
|
|
48
|
+
Reflect.defineMetadata("fields", new Map(metadataMap), target.prototype);
|
|
49
|
+
};
|
|
50
|
+
const getFieldMetaMapOnPrototype = (prototype) => {
|
|
51
|
+
const metadataMap = Reflect.getMetadata("fields", prototype) ?? /* @__PURE__ */ new Map();
|
|
52
|
+
return new Map(metadataMap);
|
|
53
|
+
};
|
|
54
|
+
const setFieldMetaMapOnPrototype = (prototype, metadataMap) => {
|
|
55
|
+
Reflect.defineMetadata("fields", new Map(metadataMap), prototype);
|
|
56
|
+
};
|
|
57
|
+
const getQueryMap = (modelRef) => {
|
|
58
|
+
const fieldMetas = getFieldMetas(modelRef);
|
|
59
|
+
return Object.fromEntries(
|
|
60
|
+
fieldMetas.filter((fieldMeta) => !!fieldMeta.query).map((fieldMeta) => [fieldMeta.key, fieldMeta.query])
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
fieldTypes,
|
|
65
|
+
getClassMeta,
|
|
66
|
+
getFieldMetaMap,
|
|
67
|
+
getFieldMetaMapOnPrototype,
|
|
68
|
+
getFieldMetas,
|
|
69
|
+
getGqlTypeStr,
|
|
70
|
+
getQueryMap,
|
|
71
|
+
getScalarExample,
|
|
72
|
+
isConstantModel,
|
|
73
|
+
scalarExampleMap,
|
|
74
|
+
setFieldMetaMap,
|
|
75
|
+
setFieldMetaMapOnPrototype
|
|
76
|
+
};
|