@akanjs/server 1.0.5-canary.4 → 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/compressor.js +31 -35
- package/cjs/src/gql.js +17 -19
- package/cjs/src/resolver.js +8 -9
- package/cjs/src/schema.js +70 -72
- package/cjs/src/searchDaemon.js +17 -29
- package/esm/src/compressor.js +32 -38
- package/esm/src/gql.js +18 -20
- package/esm/src/resolver.js +9 -10
- package/esm/src/schema.js +71 -73
- package/esm/src/searchDaemon.js +18 -30
- package/package.json +1 -1
- package/src/compressor.d.ts +3 -3
- package/src/gql.d.ts +4 -4
- package/src/module.d.ts +2 -2
- package/src/searchDaemon.d.ts +2 -3
package/cjs/src/compressor.js
CHANGED
|
@@ -61,15 +61,14 @@ const getProtoModel = (modelRef) => {
|
|
|
61
61
|
const allModelRefs = [modelRef, ...childModelRefs];
|
|
62
62
|
const modelDatas = allModelRefs.map((modelRef2) => {
|
|
63
63
|
const refName2 = import_constant.constantInfo.getRefName(modelRef2);
|
|
64
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef2);
|
|
65
64
|
return [
|
|
66
65
|
refName2,
|
|
67
66
|
{
|
|
68
67
|
fields: Object.fromEntries(
|
|
69
|
-
|
|
70
|
-
const rule =
|
|
71
|
-
const type =
|
|
72
|
-
return [
|
|
68
|
+
Object.entries(modelRef2.field).map(([key, field], id) => {
|
|
69
|
+
const rule = field.isArray ? "repeated" : field.nullable ? "optional" : "required";
|
|
70
|
+
const type = field.isClass ? import_constant.constantInfo.getRefName(field.modelRef) : field.enum ? `${refName2}${(0, import_common.capitalize)(key)}Enum` : protobufTypeMap.get(field.modelRef) ?? "string";
|
|
71
|
+
return [key, { type, id, rule }];
|
|
73
72
|
})
|
|
74
73
|
)
|
|
75
74
|
}
|
|
@@ -119,23 +118,23 @@ const getProtoEncodeFn = (modelRef) => {
|
|
|
119
118
|
const [valueRef] = (0, import_base.getNonArrayModel)(modelRef);
|
|
120
119
|
return scalarProtoEncodeMap.get(valueRef) ?? ((value) => value);
|
|
121
120
|
};
|
|
122
|
-
const protoEncode = (
|
|
123
|
-
if (
|
|
121
|
+
const protoEncode = (field, value) => {
|
|
122
|
+
if (field.nullable && (value === null || value === void 0))
|
|
124
123
|
return null;
|
|
125
|
-
if (
|
|
126
|
-
return value.map((v) => protoEncode(
|
|
124
|
+
if (field.isArray && Array.isArray(value)) {
|
|
125
|
+
return value.map((v) => protoEncode(field, v));
|
|
127
126
|
}
|
|
128
|
-
if (
|
|
129
|
-
const protoEncodeFn = getProtoEncodeFn(
|
|
127
|
+
if (field.isMap && field.of) {
|
|
128
|
+
const protoEncodeFn = getProtoEncodeFn(field.of);
|
|
130
129
|
return Object.fromEntries(
|
|
131
130
|
[...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, protoEncodeFn)])
|
|
132
131
|
);
|
|
133
132
|
}
|
|
134
|
-
if (
|
|
135
|
-
return makeProtoEncode(
|
|
136
|
-
if (
|
|
137
|
-
return
|
|
138
|
-
return getProtoEncodeFn(
|
|
133
|
+
if (field.isClass)
|
|
134
|
+
return makeProtoEncode(field.modelRef)(value);
|
|
135
|
+
if (field.enum)
|
|
136
|
+
return field.enum.indexOf(value);
|
|
137
|
+
return getProtoEncodeFn(field.modelRef)(value);
|
|
139
138
|
};
|
|
140
139
|
const getPredefinedProtoEncodeFn = (refName) => {
|
|
141
140
|
const protoEncode2 = Reflect.getMetadata(refName, ProtoEncodeStorage.prototype);
|
|
@@ -149,11 +148,10 @@ const makeProtoEncode = (modelRef) => {
|
|
|
149
148
|
const predefinedProtoEncode = getPredefinedProtoEncodeFn(refName);
|
|
150
149
|
if (predefinedProtoEncode)
|
|
151
150
|
return predefinedProtoEncode;
|
|
152
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
153
151
|
const protoEncodeFn = (value) => {
|
|
154
152
|
const result = {};
|
|
155
|
-
|
|
156
|
-
result[
|
|
153
|
+
Object.entries(modelRef.field).forEach(([key, field], id) => {
|
|
154
|
+
result[key] = protoEncode(field.getProps(), value[key]);
|
|
157
155
|
});
|
|
158
156
|
return result;
|
|
159
157
|
};
|
|
@@ -193,17 +191,17 @@ const getProtoDecodeFn = (modelRef) => {
|
|
|
193
191
|
const [valueRef] = (0, import_base.getNonArrayModel)(modelRef);
|
|
194
192
|
return scalarProtoDecodeMap.get(valueRef) ?? ((value) => value);
|
|
195
193
|
};
|
|
196
|
-
const protoDecode = (
|
|
197
|
-
if (
|
|
194
|
+
const protoDecode = (field, value) => {
|
|
195
|
+
if (field.nullable && (value === null || value === void 0))
|
|
198
196
|
return null;
|
|
199
|
-
if (
|
|
197
|
+
if (field.isArray) {
|
|
200
198
|
if (value === void 0)
|
|
201
199
|
return [];
|
|
202
200
|
if (Array.isArray(value))
|
|
203
|
-
return value.map((v) => protoDecode(
|
|
201
|
+
return value.map((v) => protoDecode(field, v));
|
|
204
202
|
}
|
|
205
|
-
if (
|
|
206
|
-
const protoDecodeFn = getProtoDecodeFn(
|
|
203
|
+
if (field.isMap && field.of) {
|
|
204
|
+
const protoDecodeFn = getProtoDecodeFn(field.of);
|
|
207
205
|
return new Map(
|
|
208
206
|
Object.entries(value).map(([key, val]) => [
|
|
209
207
|
key,
|
|
@@ -211,11 +209,11 @@ const protoDecode = (metadata, value) => {
|
|
|
211
209
|
])
|
|
212
210
|
);
|
|
213
211
|
}
|
|
214
|
-
if (
|
|
215
|
-
return makeProtoDecode(
|
|
216
|
-
if (
|
|
217
|
-
return
|
|
218
|
-
return getProtoDecodeFn(
|
|
212
|
+
if (field.isClass)
|
|
213
|
+
return makeProtoDecode(field.modelRef)(value);
|
|
214
|
+
if (field.enum)
|
|
215
|
+
return field.enum.values.at(value);
|
|
216
|
+
return getProtoDecodeFn(field.modelRef)(value);
|
|
219
217
|
};
|
|
220
218
|
const getPredefinedProtoDecodeFn = (refName) => {
|
|
221
219
|
const protoDecode2 = Reflect.getMetadata(refName, ProtoDecodeStorage.prototype);
|
|
@@ -229,11 +227,10 @@ const makeProtoDecode = (modelRef) => {
|
|
|
229
227
|
const predefinedProtoDecode = getPredefinedProtoDecodeFn(refName);
|
|
230
228
|
if (predefinedProtoDecode)
|
|
231
229
|
return predefinedProtoDecode;
|
|
232
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
233
230
|
const protoDecodeFn = (value) => {
|
|
234
231
|
const result = {};
|
|
235
|
-
|
|
236
|
-
result[
|
|
232
|
+
Object.entries(modelRef.field).forEach(([key, field], id) => {
|
|
233
|
+
result[key] = protoDecode(field.getProps(), value[key]);
|
|
237
234
|
});
|
|
238
235
|
return result;
|
|
239
236
|
};
|
|
@@ -245,8 +242,7 @@ const decode = (modelRef, buffer) => {
|
|
|
245
242
|
const message = protoModel.decode(Buffer.from(buffer));
|
|
246
243
|
const data = protoModel.toObject(message);
|
|
247
244
|
const protoDecode2 = makeProtoDecode(modelRef);
|
|
248
|
-
const
|
|
249
|
-
const result = crystalize(protoDecode2(data));
|
|
245
|
+
const result = new modelRef().set(protoDecode2(data));
|
|
250
246
|
return result;
|
|
251
247
|
};
|
|
252
248
|
// Annotate the CommonJS export names for ESM import in node:
|
package/cjs/src/gql.js
CHANGED
|
@@ -98,14 +98,16 @@ const gqlNestFieldMap = /* @__PURE__ */ new Map([
|
|
|
98
98
|
[import_base.JSON, import_graphql_type_json.default],
|
|
99
99
|
[Map, import_graphql_type_json.default]
|
|
100
100
|
]);
|
|
101
|
-
const applyNestField = (model,
|
|
102
|
-
if (
|
|
101
|
+
const applyNestField = (model, field, key, type = "object") => {
|
|
102
|
+
if (field.fieldType === "hidden" && type === "object")
|
|
103
103
|
return;
|
|
104
|
-
const modelRef =
|
|
105
|
-
(0, import_graphql.Field)(() => (0, import_base.arraiedModel)(modelRef,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
const modelRef = field.isClass ? type === "object" ? generateGql(field.modelRef) : field.isScalar ? generateGqlInput(field.modelRef) : Nest.ID : gqlNestFieldMap.get(field.modelRef) ?? field.modelRef;
|
|
105
|
+
(0, import_graphql.Field)(() => (0, import_base.arraiedModel)(modelRef, field.arrDepth), { nullable: field.nullable })(model.prototype, key);
|
|
106
|
+
};
|
|
107
|
+
const gqlClassMap = /* @__PURE__ */ new Map();
|
|
108
|
+
const getGqlClass = (modelRef) => {
|
|
109
|
+
return gqlClassMap.get(modelRef) ?? class GqlClass {
|
|
110
|
+
};
|
|
109
111
|
};
|
|
110
112
|
const generateGqlInput = (inputRef) => {
|
|
111
113
|
const refName = import_constant.constantInfo.getRefName(inputRef);
|
|
@@ -114,12 +116,9 @@ const generateGqlInput = (inputRef) => {
|
|
|
114
116
|
const predefinedInputGql = getPredefinedInqutGql(gqlName);
|
|
115
117
|
if (predefinedInputGql)
|
|
116
118
|
return predefinedInputGql;
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const inputGql = import_constant.constantInfo.isScalar(inputRef) ? InputGql : import_constant.constantInfo.getDatabase(refName).input;
|
|
121
|
-
fieldMetas.forEach((fieldMeta) => {
|
|
122
|
-
applyNestField(inputGql, fieldMeta, "input");
|
|
119
|
+
const inputGql = getGqlClass(import_constant.constantInfo.isScalar(inputRef) ? inputRef : import_constant.constantInfo.getDatabase(refName).input);
|
|
120
|
+
Object.entries(inputRef.field).forEach(([key, field], id) => {
|
|
121
|
+
applyNestField(inputGql, field.getProps(), key, "input");
|
|
123
122
|
});
|
|
124
123
|
(0, import_graphql.InputType)(gqlName)(inputGql);
|
|
125
124
|
setPredefinedInqutGql(gqlName, inputGql);
|
|
@@ -136,12 +135,11 @@ const generateGql = (objectRef) => {
|
|
|
136
135
|
const predefinedObjectGql = getPredefinedObjectGql(gqlName);
|
|
137
136
|
if (predefinedObjectGql)
|
|
138
137
|
return predefinedObjectGql;
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
applyNestField(objectGql, fieldMeta);
|
|
138
|
+
const objectGql = getGqlClass(
|
|
139
|
+
import_constant.constantInfo.isScalar(objectRef) || import_constant.constantInfo.isInsight(objectRef) ? objectRef : import_constant.constantInfo.getDatabase(refName).full
|
|
140
|
+
);
|
|
141
|
+
Object.entries(objectRef.field).forEach(([key, field], id) => {
|
|
142
|
+
applyNestField(objectGql, field.getProps(), key);
|
|
145
143
|
});
|
|
146
144
|
(0, import_graphql.ObjectType)(gqlName)(objectGql);
|
|
147
145
|
setPredefinedObjectGql(gqlName, objectGql);
|
package/cjs/src/resolver.js
CHANGED
|
@@ -93,20 +93,19 @@ const resolverOf = (sigRef, allSrvs) => {
|
|
|
93
93
|
const resolveFieldKeys = resolveFieldMetas.map((resolveFieldMeta) => resolveFieldMeta.key);
|
|
94
94
|
if (sigMeta.returns) {
|
|
95
95
|
const modelRef = sigMeta.returns();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const refName = import_constant.constantInfo.getRefName(fieldMeta.modelRef);
|
|
96
|
+
Object.entries(modelRef.field).filter(([key, field]) => field.isClass && !field.isScalar && !resolveFieldKeys.includes(key)).forEach(([key, field]) => {
|
|
97
|
+
const refName = import_constant.constantInfo.getRefName(field.modelRef);
|
|
99
98
|
const className = (0, import_common.capitalize)(refName);
|
|
100
99
|
const serviceName = `${refName}Service`;
|
|
101
|
-
Rsv.prototype[
|
|
100
|
+
Rsv.prototype[key] = async function(parent) {
|
|
102
101
|
const service = this[serviceName];
|
|
103
|
-
return
|
|
102
|
+
return field.arrDepth ? await service[`load${className}Many`](parent[key]) : await service[`load${className}`](parent[key]);
|
|
104
103
|
};
|
|
105
|
-
Nest.Parent()(Rsv.prototype,
|
|
106
|
-
Nest.ResolveField(getNestReturn(() => (0, import_base.arraiedModel)(
|
|
104
|
+
Nest.Parent()(Rsv.prototype, key, 0);
|
|
105
|
+
Nest.ResolveField(getNestReturn(() => (0, import_base.arraiedModel)(field.modelRef, field.arrDepth)))(
|
|
107
106
|
Rsv.prototype,
|
|
108
|
-
|
|
109
|
-
Object.getOwnPropertyDescriptor(Rsv.prototype,
|
|
107
|
+
key,
|
|
108
|
+
Object.getOwnPropertyDescriptor(Rsv.prototype, key) ?? {}
|
|
110
109
|
);
|
|
111
110
|
});
|
|
112
111
|
}
|
package/cjs/src/schema.js
CHANGED
|
@@ -60,87 +60,87 @@ const scalarMongoTypeMap = /* @__PURE__ */ new Map([
|
|
|
60
60
|
[Boolean, Boolean],
|
|
61
61
|
[Date, Date]
|
|
62
62
|
]);
|
|
63
|
-
const applyMongoProp = (schemaProps,
|
|
64
|
-
if (["id", "createdAt", "updatedAt"].includes(
|
|
63
|
+
const applyMongoProp = (schemaProps, field, key) => {
|
|
64
|
+
if (["id", "createdAt", "updatedAt"].includes(key) || field.fieldType === "resolve")
|
|
65
65
|
return;
|
|
66
|
-
const type =
|
|
66
|
+
const type = field.isClass ? field.isScalar ? createSchema(field.modelRef) : import_document.ObjectId : scalarMongoTypeMap.get(field.modelRef) ?? field.modelRef;
|
|
67
67
|
let prop = {};
|
|
68
|
-
if (
|
|
68
|
+
if (field.optArrDepth) {
|
|
69
69
|
prop.type = type;
|
|
70
70
|
prop.required = true;
|
|
71
|
-
if (
|
|
72
|
-
prop.ref = import_constant.constantInfo.getRefName(
|
|
73
|
-
if (
|
|
74
|
-
prop.refPath =
|
|
75
|
-
if (typeof
|
|
76
|
-
prop.min =
|
|
77
|
-
if (typeof
|
|
78
|
-
prop.max =
|
|
79
|
-
if (
|
|
80
|
-
prop.enum = [...
|
|
81
|
-
if (typeof
|
|
82
|
-
prop.minlength =
|
|
83
|
-
if (typeof
|
|
84
|
-
prop.maxlength =
|
|
85
|
-
if (
|
|
71
|
+
if (field.isClass && !field.refPath)
|
|
72
|
+
prop.ref = import_constant.constantInfo.getRefName(field.modelRef);
|
|
73
|
+
if (field.refPath)
|
|
74
|
+
prop.refPath = field.refPath;
|
|
75
|
+
if (typeof field.min === "number")
|
|
76
|
+
prop.min = field.min;
|
|
77
|
+
if (typeof field.max === "number")
|
|
78
|
+
prop.max = field.max;
|
|
79
|
+
if (field.enum)
|
|
80
|
+
prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
|
|
81
|
+
if (typeof field.minlength === "number")
|
|
82
|
+
prop.minlength = field.minlength;
|
|
83
|
+
if (typeof field.maxlength === "number")
|
|
84
|
+
prop.maxlength = field.maxlength;
|
|
85
|
+
if (field.validate) {
|
|
86
86
|
prop.validate = function(value) {
|
|
87
|
-
return
|
|
87
|
+
return field.validate?.(field.modelRef === Date && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
|
-
prop = { type: (0, import_base.arraiedModel)(prop,
|
|
91
|
-
if (
|
|
90
|
+
prop = { type: (0, import_base.arraiedModel)(prop, field.optArrDepth), default: [], required: true };
|
|
91
|
+
if (field.modelRef.prototype === Date.prototype) {
|
|
92
92
|
prop.get = (dates) => (0, import_base.applyFnToArrayObjects)(dates, (date) => (0, import_base.dayjs)(date));
|
|
93
93
|
prop.set = (days) => (0, import_base.applyFnToArrayObjects)(days, (day) => day.toDate());
|
|
94
94
|
}
|
|
95
|
-
if (
|
|
95
|
+
if (field.isClass && !field.isScalar || field.modelRef.prototype === import_base.ID.prototype) {
|
|
96
96
|
prop.get = (ids) => (0, import_base.applyFnToArrayObjects)(ids, (id) => id.toString());
|
|
97
97
|
prop.set = (ids) => (0, import_base.applyFnToArrayObjects)(ids, (id) => new import_mongoose.Types.ObjectId(id));
|
|
98
98
|
}
|
|
99
99
|
} else {
|
|
100
|
-
prop.type = (0, import_base.arraiedModel)(type,
|
|
101
|
-
prop.required = !
|
|
102
|
-
if (
|
|
103
|
-
prop.of = scalarMongoTypeMap.get(
|
|
104
|
-
if (!
|
|
100
|
+
prop.type = (0, import_base.arraiedModel)(type, field.arrDepth);
|
|
101
|
+
prop.required = !field.nullable;
|
|
102
|
+
if (field.isMap) {
|
|
103
|
+
prop.of = scalarMongoTypeMap.get(field.of) ?? createSchema(field.of);
|
|
104
|
+
if (!field.default)
|
|
105
105
|
prop.default = /* @__PURE__ */ new Map();
|
|
106
106
|
}
|
|
107
|
-
if (
|
|
108
|
-
if (typeof
|
|
107
|
+
if (field.default !== null) {
|
|
108
|
+
if (typeof field.default === "function")
|
|
109
109
|
prop.default = function() {
|
|
110
|
-
const def =
|
|
110
|
+
const def = field.default(this);
|
|
111
111
|
return (0, import_common.isDayjs)(def) ? def.toDate() : def;
|
|
112
112
|
};
|
|
113
113
|
else
|
|
114
|
-
prop.default = (0, import_common.isDayjs)(
|
|
114
|
+
prop.default = (0, import_common.isDayjs)(field.default) ? field.default.toDate() : field.default;
|
|
115
115
|
}
|
|
116
|
-
if (typeof
|
|
117
|
-
prop.immutable =
|
|
118
|
-
if (
|
|
119
|
-
prop.ref = import_constant.constantInfo.getRefName(
|
|
120
|
-
if (
|
|
121
|
-
prop.refPath =
|
|
122
|
-
if (typeof
|
|
123
|
-
prop.min =
|
|
124
|
-
if (typeof
|
|
125
|
-
prop.max =
|
|
126
|
-
if (
|
|
127
|
-
prop.enum = [...
|
|
128
|
-
if (typeof
|
|
129
|
-
prop.select =
|
|
130
|
-
if (typeof
|
|
131
|
-
prop.minlength =
|
|
132
|
-
if (typeof
|
|
133
|
-
prop.maxlength =
|
|
134
|
-
if (
|
|
116
|
+
if (typeof field.immutable !== "undefined")
|
|
117
|
+
prop.immutable = field.immutable;
|
|
118
|
+
if (field.isClass && !field.refPath)
|
|
119
|
+
prop.ref = import_constant.constantInfo.getRefName(field.modelRef);
|
|
120
|
+
if (field.refPath)
|
|
121
|
+
prop.refPath = field.refPath;
|
|
122
|
+
if (typeof field.min === "number")
|
|
123
|
+
prop.min = field.min;
|
|
124
|
+
if (typeof field.max === "number")
|
|
125
|
+
prop.max = field.max;
|
|
126
|
+
if (field.enum)
|
|
127
|
+
prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
|
|
128
|
+
if (typeof field.select === "boolean")
|
|
129
|
+
prop.select = field.select;
|
|
130
|
+
if (typeof field.minlength === "number")
|
|
131
|
+
prop.minlength = field.minlength;
|
|
132
|
+
if (typeof field.maxlength === "number")
|
|
133
|
+
prop.maxlength = field.maxlength;
|
|
134
|
+
if (field.nullable) {
|
|
135
135
|
prop.get = (v) => v === void 0 ? void 0 : v;
|
|
136
136
|
prop.set = (v) => v === null ? void 0 : v;
|
|
137
137
|
}
|
|
138
|
-
if (
|
|
138
|
+
if (field.modelRef.prototype === Date.prototype) {
|
|
139
139
|
prop.get = (date) => (0, import_base.applyFnToArrayObjects)(date, (date2) => date2 ? (0, import_base.dayjs)(date2) : void 0);
|
|
140
140
|
prop.set = (day) => (0, import_base.applyFnToArrayObjects)(day, (day2) => day2 ? (0, import_base.dayjs)(day2).toDate() : void 0);
|
|
141
141
|
}
|
|
142
|
-
if (
|
|
143
|
-
if (
|
|
142
|
+
if (field.isClass && !field.isScalar || field.modelRef.prototype === import_base.ID.prototype) {
|
|
143
|
+
if (field.arrDepth === 0) {
|
|
144
144
|
prop.get = (id) => id ? id.toString() : void 0;
|
|
145
145
|
prop.set = (id) => id ? new import_mongoose.Types.ObjectId(id) : void 0;
|
|
146
146
|
} else {
|
|
@@ -148,26 +148,25 @@ const applyMongoProp = (schemaProps, fieldMeta) => {
|
|
|
148
148
|
prop.set = (val) => (0, import_base.applyFnToArrayObjects)(val, (id) => id ? new import_mongoose.Types.ObjectId(id) : void 0);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
-
if (
|
|
152
|
-
prop.default =
|
|
151
|
+
if (field.isClass && field.isScalar && field.default === null && !field.nullable) {
|
|
152
|
+
prop.default = field.modelRef.default;
|
|
153
153
|
}
|
|
154
|
-
if (
|
|
154
|
+
if (field.validate) {
|
|
155
155
|
prop.validate = function(value) {
|
|
156
|
-
return
|
|
156
|
+
return field.validate?.(field.modelRef === Date && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
|
|
157
157
|
};
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
-
schemaProps[
|
|
160
|
+
schemaProps[key] = prop;
|
|
161
161
|
};
|
|
162
162
|
const createSchema = (modelRef) => {
|
|
163
163
|
const refName = import_constant.constantInfo.getRefName(modelRef);
|
|
164
164
|
const schemaMeta = getScalarSchemaMetaByName(refName);
|
|
165
165
|
if (schemaMeta)
|
|
166
166
|
return schemaMeta;
|
|
167
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
168
167
|
const schemaProps = {};
|
|
169
|
-
|
|
170
|
-
applyMongoProp(schemaProps,
|
|
168
|
+
Object.entries(modelRef.field).forEach(([key, field]) => {
|
|
169
|
+
applyMongoProp(schemaProps, field.getProps(), key);
|
|
171
170
|
});
|
|
172
171
|
const schema = new import_mongoose.Schema(schemaProps);
|
|
173
172
|
setScalarSchemaMetaByName(refName, schema);
|
|
@@ -178,7 +177,6 @@ const schemaOf = (modelRef, docRef, middleware) => {
|
|
|
178
177
|
const schemaMeta = getSchemaMetaByName(refName);
|
|
179
178
|
if (schemaMeta)
|
|
180
179
|
return schemaMeta;
|
|
181
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(docRef);
|
|
182
180
|
const schemaProps = {
|
|
183
181
|
createdAt: {
|
|
184
182
|
type: Date,
|
|
@@ -191,8 +189,9 @@ const schemaOf = (modelRef, docRef, middleware) => {
|
|
|
191
189
|
set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
|
|
192
190
|
}
|
|
193
191
|
};
|
|
194
|
-
|
|
195
|
-
|
|
192
|
+
const cnst = import_constant.constantInfo.getDatabase(refName);
|
|
193
|
+
Object.entries(cnst.full.field).forEach(([key, field]) => {
|
|
194
|
+
applyMongoProp(schemaProps, field.getProps(), key);
|
|
196
195
|
});
|
|
197
196
|
const schema = new import_mongoose.Schema(schemaProps, (0, import_document.getDefaultSchemaOptions)());
|
|
198
197
|
schema.methods.refresh = async function() {
|
|
@@ -255,7 +254,6 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
|
|
|
255
254
|
const modelSchema = Reflect.getMetadata(refName, SchemaStorage.prototype);
|
|
256
255
|
if (!modelSchema)
|
|
257
256
|
throw new Error(`Schema of ${refName} not found`);
|
|
258
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(docRef);
|
|
259
257
|
const schemaProps = {
|
|
260
258
|
createdAt: {
|
|
261
259
|
type: Date,
|
|
@@ -268,13 +266,13 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
|
|
|
268
266
|
set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
|
|
269
267
|
}
|
|
270
268
|
};
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
(
|
|
269
|
+
const cnst = import_constant.constantInfo.getDatabase(refName);
|
|
270
|
+
Object.entries(cnst.full.field).forEach(([key, field]) => {
|
|
271
|
+
applyMongoProp(schemaProps, field.getProps(), key);
|
|
272
|
+
(0, import__.applyNestField)(originDoc, field.getProps(), key);
|
|
274
273
|
});
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
(0, import__.applyNestField)(originInput, fieldMeta, "input");
|
|
274
|
+
Object.entries(cnst.input.field).forEach(([key, field]) => {
|
|
275
|
+
(0, import__.applyNestField)(originInput, field.getProps(), key, "input");
|
|
278
276
|
});
|
|
279
277
|
const schema = new import_mongoose.Schema(schemaProps, (0, import_document.getDefaultSchemaOptions)());
|
|
280
278
|
modelSchema.add(schema);
|
package/cjs/src/searchDaemon.js
CHANGED
|
@@ -38,38 +38,29 @@ var import_document = require("@akanjs/document");
|
|
|
38
38
|
var import_common2 = require("@nestjs/common");
|
|
39
39
|
var import_mongoose = require("@nestjs/mongoose");
|
|
40
40
|
const hasTextField = (modelRef) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
(fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
41
|
+
return Object.entries(modelRef.field).some(
|
|
42
|
+
([_, field]) => !!field.text || field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)
|
|
44
43
|
);
|
|
45
44
|
};
|
|
46
45
|
const getTextFieldKeys = (modelRef) => {
|
|
47
46
|
const allSearchFields = [];
|
|
48
47
|
const allFilterFields = [];
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
allSearchFields.push(fieldMeta.key);
|
|
56
|
-
return fieldMeta.key;
|
|
48
|
+
const stringTextFields = Object.entries(modelRef.field).filter(([_, field]) => !!field.text).map(([key, field]) => {
|
|
49
|
+
if (field.text === "filter")
|
|
50
|
+
allFilterFields.push(key);
|
|
51
|
+
else if (field.text === "search")
|
|
52
|
+
allSearchFields.push(key);
|
|
53
|
+
return key;
|
|
57
54
|
});
|
|
58
|
-
const scalarTextFields =
|
|
59
|
-
(fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
60
|
-
).map((fieldMeta) => fieldMeta.key);
|
|
55
|
+
const scalarTextFields = Object.entries(modelRef.field).filter(([_, field]) => field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)).map(([key, field]) => key);
|
|
61
56
|
const deepFields = scalarTextFields.map((key) => {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
fieldMeta.modelRef
|
|
67
|
-
);
|
|
68
|
-
allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
|
|
69
|
-
allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
|
|
57
|
+
const field = modelRef.field[key];
|
|
58
|
+
const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(field.modelRef);
|
|
59
|
+
allFilterFields2.push(...allSearchFields2.map((field2) => `${key}.${field2}`));
|
|
60
|
+
allSearchFields2.push(...stringTextFields2.map((field2) => `${key}.${field2}`));
|
|
70
61
|
return [
|
|
71
|
-
...stringTextFields2.map((
|
|
72
|
-
...allTextFields.map((
|
|
62
|
+
...stringTextFields2.map((field2) => `${key}.${field2}`),
|
|
63
|
+
...allTextFields.map((field2) => `${key}.${field2}`)
|
|
73
64
|
];
|
|
74
65
|
}).flat();
|
|
75
66
|
return {
|
|
@@ -81,7 +72,6 @@ const getTextFieldKeys = (modelRef) => {
|
|
|
81
72
|
};
|
|
82
73
|
};
|
|
83
74
|
const makeTextFilter = (modelRef) => {
|
|
84
|
-
const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
|
|
85
75
|
const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
|
|
86
76
|
const filterData = (data, assignObj = {}) => {
|
|
87
77
|
if (Array.isArray(data))
|
|
@@ -90,10 +80,8 @@ const makeTextFilter = (modelRef) => {
|
|
|
90
80
|
Object.fromEntries([
|
|
91
81
|
...stringTextFields.map((key) => [key, data[key]]),
|
|
92
82
|
...scalarTextFields.map((key) => {
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
throw new Error(`No fieldMeta for ${key}`);
|
|
96
|
-
const filterFunc = makeTextFilter(fieldMeta.modelRef);
|
|
83
|
+
const field = modelRef.field[key];
|
|
84
|
+
const filterFunc = makeTextFilter(field.modelRef);
|
|
97
85
|
return [key, filterFunc(data[key])];
|
|
98
86
|
})
|
|
99
87
|
]),
|
package/esm/src/compressor.js
CHANGED
|
@@ -11,9 +11,7 @@ import { capitalize, Logger } from "@akanjs/common";
|
|
|
11
11
|
import {
|
|
12
12
|
constantInfo,
|
|
13
13
|
getChildClassRefs,
|
|
14
|
-
getFieldEnumMetas
|
|
15
|
-
getFieldMetas,
|
|
16
|
-
makeCrystalize
|
|
14
|
+
getFieldEnumMetas
|
|
17
15
|
} from "@akanjs/constant";
|
|
18
16
|
import * as proto from "protobufjs";
|
|
19
17
|
class ProtoModelStorage {
|
|
@@ -42,15 +40,14 @@ const getProtoModel = (modelRef) => {
|
|
|
42
40
|
const allModelRefs = [modelRef, ...childModelRefs];
|
|
43
41
|
const modelDatas = allModelRefs.map((modelRef2) => {
|
|
44
42
|
const refName2 = constantInfo.getRefName(modelRef2);
|
|
45
|
-
const fieldMetas = getFieldMetas(modelRef2);
|
|
46
43
|
return [
|
|
47
44
|
refName2,
|
|
48
45
|
{
|
|
49
46
|
fields: Object.fromEntries(
|
|
50
|
-
|
|
51
|
-
const rule =
|
|
52
|
-
const type =
|
|
53
|
-
return [
|
|
47
|
+
Object.entries(modelRef2.field).map(([key, field], id) => {
|
|
48
|
+
const rule = field.isArray ? "repeated" : field.nullable ? "optional" : "required";
|
|
49
|
+
const type = field.isClass ? constantInfo.getRefName(field.modelRef) : field.enum ? `${refName2}${capitalize(key)}Enum` : protobufTypeMap.get(field.modelRef) ?? "string";
|
|
50
|
+
return [key, { type, id, rule }];
|
|
54
51
|
})
|
|
55
52
|
)
|
|
56
53
|
}
|
|
@@ -100,23 +97,23 @@ const getProtoEncodeFn = (modelRef) => {
|
|
|
100
97
|
const [valueRef] = getNonArrayModel(modelRef);
|
|
101
98
|
return scalarProtoEncodeMap.get(valueRef) ?? ((value) => value);
|
|
102
99
|
};
|
|
103
|
-
const protoEncode = (
|
|
104
|
-
if (
|
|
100
|
+
const protoEncode = (field, value) => {
|
|
101
|
+
if (field.nullable && (value === null || value === void 0))
|
|
105
102
|
return null;
|
|
106
|
-
if (
|
|
107
|
-
return value.map((v) => protoEncode(
|
|
103
|
+
if (field.isArray && Array.isArray(value)) {
|
|
104
|
+
return value.map((v) => protoEncode(field, v));
|
|
108
105
|
}
|
|
109
|
-
if (
|
|
110
|
-
const protoEncodeFn = getProtoEncodeFn(
|
|
106
|
+
if (field.isMap && field.of) {
|
|
107
|
+
const protoEncodeFn = getProtoEncodeFn(field.of);
|
|
111
108
|
return Object.fromEntries(
|
|
112
109
|
[...value.entries()].map(([key, val]) => [key, applyFnToArrayObjects(val, protoEncodeFn)])
|
|
113
110
|
);
|
|
114
111
|
}
|
|
115
|
-
if (
|
|
116
|
-
return makeProtoEncode(
|
|
117
|
-
if (
|
|
118
|
-
return
|
|
119
|
-
return getProtoEncodeFn(
|
|
112
|
+
if (field.isClass)
|
|
113
|
+
return makeProtoEncode(field.modelRef)(value);
|
|
114
|
+
if (field.enum)
|
|
115
|
+
return field.enum.indexOf(value);
|
|
116
|
+
return getProtoEncodeFn(field.modelRef)(value);
|
|
120
117
|
};
|
|
121
118
|
const getPredefinedProtoEncodeFn = (refName) => {
|
|
122
119
|
const protoEncode2 = Reflect.getMetadata(refName, ProtoEncodeStorage.prototype);
|
|
@@ -130,11 +127,10 @@ const makeProtoEncode = (modelRef) => {
|
|
|
130
127
|
const predefinedProtoEncode = getPredefinedProtoEncodeFn(refName);
|
|
131
128
|
if (predefinedProtoEncode)
|
|
132
129
|
return predefinedProtoEncode;
|
|
133
|
-
const fieldMetas = getFieldMetas(modelRef);
|
|
134
130
|
const protoEncodeFn = (value) => {
|
|
135
131
|
const result = {};
|
|
136
|
-
|
|
137
|
-
result[
|
|
132
|
+
Object.entries(modelRef.field).forEach(([key, field], id) => {
|
|
133
|
+
result[key] = protoEncode(field.getProps(), value[key]);
|
|
138
134
|
});
|
|
139
135
|
return result;
|
|
140
136
|
};
|
|
@@ -174,17 +170,17 @@ const getProtoDecodeFn = (modelRef) => {
|
|
|
174
170
|
const [valueRef] = getNonArrayModel(modelRef);
|
|
175
171
|
return scalarProtoDecodeMap.get(valueRef) ?? ((value) => value);
|
|
176
172
|
};
|
|
177
|
-
const protoDecode = (
|
|
178
|
-
if (
|
|
173
|
+
const protoDecode = (field, value) => {
|
|
174
|
+
if (field.nullable && (value === null || value === void 0))
|
|
179
175
|
return null;
|
|
180
|
-
if (
|
|
176
|
+
if (field.isArray) {
|
|
181
177
|
if (value === void 0)
|
|
182
178
|
return [];
|
|
183
179
|
if (Array.isArray(value))
|
|
184
|
-
return value.map((v) => protoDecode(
|
|
180
|
+
return value.map((v) => protoDecode(field, v));
|
|
185
181
|
}
|
|
186
|
-
if (
|
|
187
|
-
const protoDecodeFn = getProtoDecodeFn(
|
|
182
|
+
if (field.isMap && field.of) {
|
|
183
|
+
const protoDecodeFn = getProtoDecodeFn(field.of);
|
|
188
184
|
return new Map(
|
|
189
185
|
Object.entries(value).map(([key, val]) => [
|
|
190
186
|
key,
|
|
@@ -192,11 +188,11 @@ const protoDecode = (metadata, value) => {
|
|
|
192
188
|
])
|
|
193
189
|
);
|
|
194
190
|
}
|
|
195
|
-
if (
|
|
196
|
-
return makeProtoDecode(
|
|
197
|
-
if (
|
|
198
|
-
return
|
|
199
|
-
return getProtoDecodeFn(
|
|
191
|
+
if (field.isClass)
|
|
192
|
+
return makeProtoDecode(field.modelRef)(value);
|
|
193
|
+
if (field.enum)
|
|
194
|
+
return field.enum.values.at(value);
|
|
195
|
+
return getProtoDecodeFn(field.modelRef)(value);
|
|
200
196
|
};
|
|
201
197
|
const getPredefinedProtoDecodeFn = (refName) => {
|
|
202
198
|
const protoDecode2 = Reflect.getMetadata(refName, ProtoDecodeStorage.prototype);
|
|
@@ -210,11 +206,10 @@ const makeProtoDecode = (modelRef) => {
|
|
|
210
206
|
const predefinedProtoDecode = getPredefinedProtoDecodeFn(refName);
|
|
211
207
|
if (predefinedProtoDecode)
|
|
212
208
|
return predefinedProtoDecode;
|
|
213
|
-
const fieldMetas = getFieldMetas(modelRef);
|
|
214
209
|
const protoDecodeFn = (value) => {
|
|
215
210
|
const result = {};
|
|
216
|
-
|
|
217
|
-
result[
|
|
211
|
+
Object.entries(modelRef.field).forEach(([key, field], id) => {
|
|
212
|
+
result[key] = protoDecode(field.getProps(), value[key]);
|
|
218
213
|
});
|
|
219
214
|
return result;
|
|
220
215
|
};
|
|
@@ -226,8 +221,7 @@ const decode = (modelRef, buffer) => {
|
|
|
226
221
|
const message = protoModel.decode(Buffer.from(buffer));
|
|
227
222
|
const data = protoModel.toObject(message);
|
|
228
223
|
const protoDecode2 = makeProtoDecode(modelRef);
|
|
229
|
-
const
|
|
230
|
-
const result = crystalize(protoDecode2(data));
|
|
224
|
+
const result = new modelRef().set(protoDecode2(data));
|
|
231
225
|
return result;
|
|
232
226
|
};
|
|
233
227
|
export {
|
package/esm/src/gql.js
CHANGED
|
@@ -11,7 +11,7 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
11
11
|
};
|
|
12
12
|
import { arraiedModel, dayjs, Float, ID, Int, JSON } from "@akanjs/base";
|
|
13
13
|
import { capitalize } from "@akanjs/common";
|
|
14
|
-
import { constantInfo
|
|
14
|
+
import { constantInfo } from "@akanjs/constant";
|
|
15
15
|
import * as Nest from "@nestjs/graphql";
|
|
16
16
|
import { Field, InputType, ObjectType } from "@nestjs/graphql";
|
|
17
17
|
import { isDayjs } from "dayjs";
|
|
@@ -65,14 +65,16 @@ const gqlNestFieldMap = /* @__PURE__ */ new Map([
|
|
|
65
65
|
[JSON, GraphQLJSON],
|
|
66
66
|
[Map, GraphQLJSON]
|
|
67
67
|
]);
|
|
68
|
-
const applyNestField = (model,
|
|
69
|
-
if (
|
|
68
|
+
const applyNestField = (model, field, key, type = "object") => {
|
|
69
|
+
if (field.fieldType === "hidden" && type === "object")
|
|
70
70
|
return;
|
|
71
|
-
const modelRef =
|
|
72
|
-
Field(() => arraiedModel(modelRef,
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
const modelRef = field.isClass ? type === "object" ? generateGql(field.modelRef) : field.isScalar ? generateGqlInput(field.modelRef) : Nest.ID : gqlNestFieldMap.get(field.modelRef) ?? field.modelRef;
|
|
72
|
+
Field(() => arraiedModel(modelRef, field.arrDepth), { nullable: field.nullable })(model.prototype, key);
|
|
73
|
+
};
|
|
74
|
+
const gqlClassMap = /* @__PURE__ */ new Map();
|
|
75
|
+
const getGqlClass = (modelRef) => {
|
|
76
|
+
return gqlClassMap.get(modelRef) ?? class GqlClass {
|
|
77
|
+
};
|
|
76
78
|
};
|
|
77
79
|
const generateGqlInput = (inputRef) => {
|
|
78
80
|
const refName = constantInfo.getRefName(inputRef);
|
|
@@ -81,12 +83,9 @@ const generateGqlInput = (inputRef) => {
|
|
|
81
83
|
const predefinedInputGql = getPredefinedInqutGql(gqlName);
|
|
82
84
|
if (predefinedInputGql)
|
|
83
85
|
return predefinedInputGql;
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const inputGql = constantInfo.isScalar(inputRef) ? InputGql : constantInfo.getDatabase(refName).input;
|
|
88
|
-
fieldMetas.forEach((fieldMeta) => {
|
|
89
|
-
applyNestField(inputGql, fieldMeta, "input");
|
|
86
|
+
const inputGql = getGqlClass(constantInfo.isScalar(inputRef) ? inputRef : constantInfo.getDatabase(refName).input);
|
|
87
|
+
Object.entries(inputRef.field).forEach(([key, field], id) => {
|
|
88
|
+
applyNestField(inputGql, field.getProps(), key, "input");
|
|
90
89
|
});
|
|
91
90
|
InputType(gqlName)(inputGql);
|
|
92
91
|
setPredefinedInqutGql(gqlName, inputGql);
|
|
@@ -103,12 +102,11 @@ const generateGql = (objectRef) => {
|
|
|
103
102
|
const predefinedObjectGql = getPredefinedObjectGql(gqlName);
|
|
104
103
|
if (predefinedObjectGql)
|
|
105
104
|
return predefinedObjectGql;
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
applyNestField(objectGql, fieldMeta);
|
|
105
|
+
const objectGql = getGqlClass(
|
|
106
|
+
constantInfo.isScalar(objectRef) || constantInfo.isInsight(objectRef) ? objectRef : constantInfo.getDatabase(refName).full
|
|
107
|
+
);
|
|
108
|
+
Object.entries(objectRef.field).forEach(([key, field], id) => {
|
|
109
|
+
applyNestField(objectGql, field.getProps(), key);
|
|
112
110
|
});
|
|
113
111
|
ObjectType(gqlName)(objectGql);
|
|
114
112
|
setPredefinedObjectGql(gqlName, objectGql);
|
package/esm/src/resolver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { arraiedModel, Float, getNonArrayModel, ID, Int, JSON, Upload } from "@akanjs/base";
|
|
2
2
|
import { capitalize, lowerlize } from "@akanjs/common";
|
|
3
|
-
import { constantInfo
|
|
3
|
+
import { constantInfo } from "@akanjs/constant";
|
|
4
4
|
import { getNestParamDecorator, None } from "@akanjs/nest";
|
|
5
5
|
import { getServiceRefs, isServiceEnabled } from "@akanjs/service";
|
|
6
6
|
import {
|
|
@@ -67,20 +67,19 @@ const resolverOf = (sigRef, allSrvs) => {
|
|
|
67
67
|
const resolveFieldKeys = resolveFieldMetas.map((resolveFieldMeta) => resolveFieldMeta.key);
|
|
68
68
|
if (sigMeta.returns) {
|
|
69
69
|
const modelRef = sigMeta.returns();
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const refName = constantInfo.getRefName(fieldMeta.modelRef);
|
|
70
|
+
Object.entries(modelRef.field).filter(([key, field]) => field.isClass && !field.isScalar && !resolveFieldKeys.includes(key)).forEach(([key, field]) => {
|
|
71
|
+
const refName = constantInfo.getRefName(field.modelRef);
|
|
73
72
|
const className = capitalize(refName);
|
|
74
73
|
const serviceName = `${refName}Service`;
|
|
75
|
-
Rsv.prototype[
|
|
74
|
+
Rsv.prototype[key] = async function(parent) {
|
|
76
75
|
const service = this[serviceName];
|
|
77
|
-
return
|
|
76
|
+
return field.arrDepth ? await service[`load${className}Many`](parent[key]) : await service[`load${className}`](parent[key]);
|
|
78
77
|
};
|
|
79
|
-
Nest.Parent()(Rsv.prototype,
|
|
80
|
-
Nest.ResolveField(getNestReturn(() => arraiedModel(
|
|
78
|
+
Nest.Parent()(Rsv.prototype, key, 0);
|
|
79
|
+
Nest.ResolveField(getNestReturn(() => arraiedModel(field.modelRef, field.arrDepth)))(
|
|
81
80
|
Rsv.prototype,
|
|
82
|
-
|
|
83
|
-
Object.getOwnPropertyDescriptor(Rsv.prototype,
|
|
81
|
+
key,
|
|
82
|
+
Object.getOwnPropertyDescriptor(Rsv.prototype, key) ?? {}
|
|
84
83
|
);
|
|
85
84
|
});
|
|
86
85
|
}
|
package/esm/src/schema.js
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
JSON
|
|
9
9
|
} from "@akanjs/base";
|
|
10
10
|
import { isDayjs, Logger } from "@akanjs/common";
|
|
11
|
-
import { constantInfo
|
|
11
|
+
import { constantInfo } from "@akanjs/constant";
|
|
12
12
|
import { documentInfo, getDefaultSchemaOptions, ObjectId } from "@akanjs/document";
|
|
13
13
|
import { Schema, Types } from "mongoose";
|
|
14
14
|
import { applyNestField } from ".";
|
|
@@ -44,87 +44,87 @@ const scalarMongoTypeMap = /* @__PURE__ */ new Map([
|
|
|
44
44
|
[Boolean, Boolean],
|
|
45
45
|
[Date, Date]
|
|
46
46
|
]);
|
|
47
|
-
const applyMongoProp = (schemaProps,
|
|
48
|
-
if (["id", "createdAt", "updatedAt"].includes(
|
|
47
|
+
const applyMongoProp = (schemaProps, field, key) => {
|
|
48
|
+
if (["id", "createdAt", "updatedAt"].includes(key) || field.fieldType === "resolve")
|
|
49
49
|
return;
|
|
50
|
-
const type =
|
|
50
|
+
const type = field.isClass ? field.isScalar ? createSchema(field.modelRef) : ObjectId : scalarMongoTypeMap.get(field.modelRef) ?? field.modelRef;
|
|
51
51
|
let prop = {};
|
|
52
|
-
if (
|
|
52
|
+
if (field.optArrDepth) {
|
|
53
53
|
prop.type = type;
|
|
54
54
|
prop.required = true;
|
|
55
|
-
if (
|
|
56
|
-
prop.ref = constantInfo.getRefName(
|
|
57
|
-
if (
|
|
58
|
-
prop.refPath =
|
|
59
|
-
if (typeof
|
|
60
|
-
prop.min =
|
|
61
|
-
if (typeof
|
|
62
|
-
prop.max =
|
|
63
|
-
if (
|
|
64
|
-
prop.enum = [...
|
|
65
|
-
if (typeof
|
|
66
|
-
prop.minlength =
|
|
67
|
-
if (typeof
|
|
68
|
-
prop.maxlength =
|
|
69
|
-
if (
|
|
55
|
+
if (field.isClass && !field.refPath)
|
|
56
|
+
prop.ref = constantInfo.getRefName(field.modelRef);
|
|
57
|
+
if (field.refPath)
|
|
58
|
+
prop.refPath = field.refPath;
|
|
59
|
+
if (typeof field.min === "number")
|
|
60
|
+
prop.min = field.min;
|
|
61
|
+
if (typeof field.max === "number")
|
|
62
|
+
prop.max = field.max;
|
|
63
|
+
if (field.enum)
|
|
64
|
+
prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
|
|
65
|
+
if (typeof field.minlength === "number")
|
|
66
|
+
prop.minlength = field.minlength;
|
|
67
|
+
if (typeof field.maxlength === "number")
|
|
68
|
+
prop.maxlength = field.maxlength;
|
|
69
|
+
if (field.validate) {
|
|
70
70
|
prop.validate = function(value) {
|
|
71
|
-
return
|
|
71
|
+
return field.validate?.(field.modelRef === Date && !!value ? dayjs() : value, this) ?? true;
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
|
-
prop = { type: arraiedModel(prop,
|
|
75
|
-
if (
|
|
74
|
+
prop = { type: arraiedModel(prop, field.optArrDepth), default: [], required: true };
|
|
75
|
+
if (field.modelRef.prototype === Date.prototype) {
|
|
76
76
|
prop.get = (dates) => applyFnToArrayObjects(dates, (date) => dayjs(date));
|
|
77
77
|
prop.set = (days) => applyFnToArrayObjects(days, (day) => day.toDate());
|
|
78
78
|
}
|
|
79
|
-
if (
|
|
79
|
+
if (field.isClass && !field.isScalar || field.modelRef.prototype === ID.prototype) {
|
|
80
80
|
prop.get = (ids) => applyFnToArrayObjects(ids, (id) => id.toString());
|
|
81
81
|
prop.set = (ids) => applyFnToArrayObjects(ids, (id) => new Types.ObjectId(id));
|
|
82
82
|
}
|
|
83
83
|
} else {
|
|
84
|
-
prop.type = arraiedModel(type,
|
|
85
|
-
prop.required = !
|
|
86
|
-
if (
|
|
87
|
-
prop.of = scalarMongoTypeMap.get(
|
|
88
|
-
if (!
|
|
84
|
+
prop.type = arraiedModel(type, field.arrDepth);
|
|
85
|
+
prop.required = !field.nullable;
|
|
86
|
+
if (field.isMap) {
|
|
87
|
+
prop.of = scalarMongoTypeMap.get(field.of) ?? createSchema(field.of);
|
|
88
|
+
if (!field.default)
|
|
89
89
|
prop.default = /* @__PURE__ */ new Map();
|
|
90
90
|
}
|
|
91
|
-
if (
|
|
92
|
-
if (typeof
|
|
91
|
+
if (field.default !== null) {
|
|
92
|
+
if (typeof field.default === "function")
|
|
93
93
|
prop.default = function() {
|
|
94
|
-
const def =
|
|
94
|
+
const def = field.default(this);
|
|
95
95
|
return isDayjs(def) ? def.toDate() : def;
|
|
96
96
|
};
|
|
97
97
|
else
|
|
98
|
-
prop.default = isDayjs(
|
|
98
|
+
prop.default = isDayjs(field.default) ? field.default.toDate() : field.default;
|
|
99
99
|
}
|
|
100
|
-
if (typeof
|
|
101
|
-
prop.immutable =
|
|
102
|
-
if (
|
|
103
|
-
prop.ref = constantInfo.getRefName(
|
|
104
|
-
if (
|
|
105
|
-
prop.refPath =
|
|
106
|
-
if (typeof
|
|
107
|
-
prop.min =
|
|
108
|
-
if (typeof
|
|
109
|
-
prop.max =
|
|
110
|
-
if (
|
|
111
|
-
prop.enum = [...
|
|
112
|
-
if (typeof
|
|
113
|
-
prop.select =
|
|
114
|
-
if (typeof
|
|
115
|
-
prop.minlength =
|
|
116
|
-
if (typeof
|
|
117
|
-
prop.maxlength =
|
|
118
|
-
if (
|
|
100
|
+
if (typeof field.immutable !== "undefined")
|
|
101
|
+
prop.immutable = field.immutable;
|
|
102
|
+
if (field.isClass && !field.refPath)
|
|
103
|
+
prop.ref = constantInfo.getRefName(field.modelRef);
|
|
104
|
+
if (field.refPath)
|
|
105
|
+
prop.refPath = field.refPath;
|
|
106
|
+
if (typeof field.min === "number")
|
|
107
|
+
prop.min = field.min;
|
|
108
|
+
if (typeof field.max === "number")
|
|
109
|
+
prop.max = field.max;
|
|
110
|
+
if (field.enum)
|
|
111
|
+
prop.enum = [...field.enum.values, ...field.nullable ? [null] : []];
|
|
112
|
+
if (typeof field.select === "boolean")
|
|
113
|
+
prop.select = field.select;
|
|
114
|
+
if (typeof field.minlength === "number")
|
|
115
|
+
prop.minlength = field.minlength;
|
|
116
|
+
if (typeof field.maxlength === "number")
|
|
117
|
+
prop.maxlength = field.maxlength;
|
|
118
|
+
if (field.nullable) {
|
|
119
119
|
prop.get = (v) => v === void 0 ? void 0 : v;
|
|
120
120
|
prop.set = (v) => v === null ? void 0 : v;
|
|
121
121
|
}
|
|
122
|
-
if (
|
|
122
|
+
if (field.modelRef.prototype === Date.prototype) {
|
|
123
123
|
prop.get = (date) => applyFnToArrayObjects(date, (date2) => date2 ? dayjs(date2) : void 0);
|
|
124
124
|
prop.set = (day) => applyFnToArrayObjects(day, (day2) => day2 ? dayjs(day2).toDate() : void 0);
|
|
125
125
|
}
|
|
126
|
-
if (
|
|
127
|
-
if (
|
|
126
|
+
if (field.isClass && !field.isScalar || field.modelRef.prototype === ID.prototype) {
|
|
127
|
+
if (field.arrDepth === 0) {
|
|
128
128
|
prop.get = (id) => id ? id.toString() : void 0;
|
|
129
129
|
prop.set = (id) => id ? new Types.ObjectId(id) : void 0;
|
|
130
130
|
} else {
|
|
@@ -132,26 +132,25 @@ const applyMongoProp = (schemaProps, fieldMeta) => {
|
|
|
132
132
|
prop.set = (val) => applyFnToArrayObjects(val, (id) => id ? new Types.ObjectId(id) : void 0);
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
|
-
if (
|
|
136
|
-
prop.default =
|
|
135
|
+
if (field.isClass && field.isScalar && field.default === null && !field.nullable) {
|
|
136
|
+
prop.default = field.modelRef.default;
|
|
137
137
|
}
|
|
138
|
-
if (
|
|
138
|
+
if (field.validate) {
|
|
139
139
|
prop.validate = function(value) {
|
|
140
|
-
return
|
|
140
|
+
return field.validate?.(field.modelRef === Date && !!value ? dayjs() : value, this) ?? true;
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
|
-
schemaProps[
|
|
144
|
+
schemaProps[key] = prop;
|
|
145
145
|
};
|
|
146
146
|
const createSchema = (modelRef) => {
|
|
147
147
|
const refName = constantInfo.getRefName(modelRef);
|
|
148
148
|
const schemaMeta = getScalarSchemaMetaByName(refName);
|
|
149
149
|
if (schemaMeta)
|
|
150
150
|
return schemaMeta;
|
|
151
|
-
const fieldMetas = getFieldMetas(modelRef);
|
|
152
151
|
const schemaProps = {};
|
|
153
|
-
|
|
154
|
-
applyMongoProp(schemaProps,
|
|
152
|
+
Object.entries(modelRef.field).forEach(([key, field]) => {
|
|
153
|
+
applyMongoProp(schemaProps, field.getProps(), key);
|
|
155
154
|
});
|
|
156
155
|
const schema = new Schema(schemaProps);
|
|
157
156
|
setScalarSchemaMetaByName(refName, schema);
|
|
@@ -162,7 +161,6 @@ const schemaOf = (modelRef, docRef, middleware) => {
|
|
|
162
161
|
const schemaMeta = getSchemaMetaByName(refName);
|
|
163
162
|
if (schemaMeta)
|
|
164
163
|
return schemaMeta;
|
|
165
|
-
const fieldMetas = getFieldMetas(docRef);
|
|
166
164
|
const schemaProps = {
|
|
167
165
|
createdAt: {
|
|
168
166
|
type: Date,
|
|
@@ -175,8 +173,9 @@ const schemaOf = (modelRef, docRef, middleware) => {
|
|
|
175
173
|
set: (day) => day ? dayjs(day).toDate() : day
|
|
176
174
|
}
|
|
177
175
|
};
|
|
178
|
-
|
|
179
|
-
|
|
176
|
+
const cnst = constantInfo.getDatabase(refName);
|
|
177
|
+
Object.entries(cnst.full.field).forEach(([key, field]) => {
|
|
178
|
+
applyMongoProp(schemaProps, field.getProps(), key);
|
|
180
179
|
});
|
|
181
180
|
const schema = new Schema(schemaProps, getDefaultSchemaOptions());
|
|
182
181
|
schema.methods.refresh = async function() {
|
|
@@ -239,7 +238,6 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
|
|
|
239
238
|
const modelSchema = Reflect.getMetadata(refName, SchemaStorage.prototype);
|
|
240
239
|
if (!modelSchema)
|
|
241
240
|
throw new Error(`Schema of ${refName} not found`);
|
|
242
|
-
const fieldMetas = getFieldMetas(docRef);
|
|
243
241
|
const schemaProps = {
|
|
244
242
|
createdAt: {
|
|
245
243
|
type: Date,
|
|
@@ -252,13 +250,13 @@ const addSchema = (modelRef, docRef, inputRef, middleware) => {
|
|
|
252
250
|
set: (day) => day ? dayjs(day).toDate() : day
|
|
253
251
|
}
|
|
254
252
|
};
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
253
|
+
const cnst = constantInfo.getDatabase(refName);
|
|
254
|
+
Object.entries(cnst.full.field).forEach(([key, field]) => {
|
|
255
|
+
applyMongoProp(schemaProps, field.getProps(), key);
|
|
256
|
+
applyNestField(originDoc, field.getProps(), key);
|
|
258
257
|
});
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
applyNestField(originInput, fieldMeta, "input");
|
|
258
|
+
Object.entries(cnst.input.field).forEach(([key, field]) => {
|
|
259
|
+
applyNestField(originInput, field.getProps(), key, "input");
|
|
262
260
|
});
|
|
263
261
|
const schema = new Schema(schemaProps, getDefaultSchemaOptions());
|
|
264
262
|
modelSchema.add(schema);
|
package/esm/src/searchDaemon.js
CHANGED
|
@@ -12,43 +12,34 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
12
12
|
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
13
13
|
import { baseEnv } from "@akanjs/base";
|
|
14
14
|
import { capitalize, Logger } from "@akanjs/common";
|
|
15
|
-
import { constantInfo
|
|
15
|
+
import { constantInfo } from "@akanjs/constant";
|
|
16
16
|
import { documentInfo, getFilterSortMap } from "@akanjs/document";
|
|
17
17
|
import { Global, Inject, Injectable, Module } from "@nestjs/common";
|
|
18
18
|
import { InjectConnection } from "@nestjs/mongoose";
|
|
19
19
|
const hasTextField = (modelRef) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
(fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
20
|
+
return Object.entries(modelRef.field).some(
|
|
21
|
+
([_, field]) => !!field.text || field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)
|
|
23
22
|
);
|
|
24
23
|
};
|
|
25
24
|
const getTextFieldKeys = (modelRef) => {
|
|
26
25
|
const allSearchFields = [];
|
|
27
26
|
const allFilterFields = [];
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
allSearchFields.push(fieldMeta.key);
|
|
35
|
-
return fieldMeta.key;
|
|
27
|
+
const stringTextFields = Object.entries(modelRef.field).filter(([_, field]) => !!field.text).map(([key, field]) => {
|
|
28
|
+
if (field.text === "filter")
|
|
29
|
+
allFilterFields.push(key);
|
|
30
|
+
else if (field.text === "search")
|
|
31
|
+
allSearchFields.push(key);
|
|
32
|
+
return key;
|
|
36
33
|
});
|
|
37
|
-
const scalarTextFields =
|
|
38
|
-
(fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
39
|
-
).map((fieldMeta) => fieldMeta.key);
|
|
34
|
+
const scalarTextFields = Object.entries(modelRef.field).filter(([_, field]) => field.isScalar && field.isClass && field.select && hasTextField(field.modelRef)).map(([key, field]) => key);
|
|
40
35
|
const deepFields = scalarTextFields.map((key) => {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
fieldMeta.modelRef
|
|
46
|
-
);
|
|
47
|
-
allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
|
|
48
|
-
allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
|
|
36
|
+
const field = modelRef.field[key];
|
|
37
|
+
const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(field.modelRef);
|
|
38
|
+
allFilterFields2.push(...allSearchFields2.map((field2) => `${key}.${field2}`));
|
|
39
|
+
allSearchFields2.push(...stringTextFields2.map((field2) => `${key}.${field2}`));
|
|
49
40
|
return [
|
|
50
|
-
...stringTextFields2.map((
|
|
51
|
-
...allTextFields.map((
|
|
41
|
+
...stringTextFields2.map((field2) => `${key}.${field2}`),
|
|
42
|
+
...allTextFields.map((field2) => `${key}.${field2}`)
|
|
52
43
|
];
|
|
53
44
|
}).flat();
|
|
54
45
|
return {
|
|
@@ -60,7 +51,6 @@ const getTextFieldKeys = (modelRef) => {
|
|
|
60
51
|
};
|
|
61
52
|
};
|
|
62
53
|
const makeTextFilter = (modelRef) => {
|
|
63
|
-
const fieldMetaMap = getFieldMetaMap(modelRef);
|
|
64
54
|
const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
|
|
65
55
|
const filterData = (data, assignObj = {}) => {
|
|
66
56
|
if (Array.isArray(data))
|
|
@@ -69,10 +59,8 @@ const makeTextFilter = (modelRef) => {
|
|
|
69
59
|
Object.fromEntries([
|
|
70
60
|
...stringTextFields.map((key) => [key, data[key]]),
|
|
71
61
|
...scalarTextFields.map((key) => {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
throw new Error(`No fieldMeta for ${key}`);
|
|
75
|
-
const filterFunc = makeTextFilter(fieldMeta.modelRef);
|
|
62
|
+
const field = modelRef.field[key];
|
|
63
|
+
const filterFunc = makeTextFilter(field.modelRef);
|
|
76
64
|
return [key, filterFunc(data[key])];
|
|
77
65
|
})
|
|
78
66
|
]),
|
package/package.json
CHANGED
package/src/compressor.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const encode: <T>(modelRef:
|
|
3
|
-
export declare const decode: <T>(modelRef:
|
|
1
|
+
import { Cnst, ConstantMethods } from "@akanjs/constant";
|
|
2
|
+
export declare const encode: <T>(modelRef: Cnst<T>, value: object) => Buffer<ArrayBufferLike> | null;
|
|
3
|
+
export declare const decode: <T>(modelRef: Cnst<T & ConstantMethods<T>>, buffer: Buffer) => import("@akanjs/constant").Crystal<T & ConstantMethods<T>>;
|
package/src/gql.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseObject, Dayjs, Type } from "@akanjs/base";
|
|
2
|
-
import {
|
|
2
|
+
import { Cnst, DocumentModel, FieldProps } from "@akanjs/constant";
|
|
3
3
|
import type { Doc } from "@akanjs/document";
|
|
4
4
|
import * as Nest from "@nestjs/graphql";
|
|
5
5
|
import { ValueNode } from "graphql";
|
|
@@ -9,6 +9,6 @@ export declare class DateScalar implements Nest.CustomScalar<Date, Dayjs> {
|
|
|
9
9
|
serialize(value: Dayjs): Date;
|
|
10
10
|
parseLiteral(ast: ValueNode): Dayjs;
|
|
11
11
|
}
|
|
12
|
-
export declare const applyNestField: (model: Type,
|
|
13
|
-
export declare const generateGqlInput: <InputModel>(inputRef:
|
|
14
|
-
export declare const generateGql: <ObjectModel>(objectRef:
|
|
12
|
+
export declare const applyNestField: (model: Type, field: FieldProps, key: string, type?: "object" | "input") => void;
|
|
13
|
+
export declare const generateGqlInput: <InputModel>(inputRef: Cnst<InputModel>) => Type<DocumentModel<InputModel>>;
|
|
14
|
+
export declare const generateGql: <ObjectModel>(objectRef: Cnst<ObjectModel>) => Type<ObjectModel extends BaseObject ? Doc<ObjectModel> : DocumentModel<ObjectModel>>;
|
package/src/module.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Type } from "@akanjs/base";
|
|
2
|
-
import { type ConstantModel } from "@akanjs/constant";
|
|
2
|
+
import { Cnst, type ConstantModel } from "@akanjs/constant";
|
|
3
3
|
import { Database } from "@akanjs/document";
|
|
4
4
|
import { DynamicModule } from "@nestjs/common";
|
|
5
5
|
import type { NextFunction, Request, Response } from "express";
|
|
@@ -20,7 +20,7 @@ export declare const serviceModuleOf: ({ signal, service }: ServiceModuleCreateO
|
|
|
20
20
|
[key: string]: Type | undefined;
|
|
21
21
|
}) => DynamicModule | null;
|
|
22
22
|
interface ScalarModulesCreateOptions {
|
|
23
|
-
constants:
|
|
23
|
+
constants: Cnst[];
|
|
24
24
|
}
|
|
25
25
|
export declare const scalarModulesOf: ({ constants }: ScalarModulesCreateOptions, allSrvs: {
|
|
26
26
|
[key: string]: Type | undefined;
|
package/src/searchDaemon.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type TextDoc } from "@akanjs/constant";
|
|
1
|
+
import { Cnst, type TextDoc } from "@akanjs/constant";
|
|
3
2
|
import type { Types } from "mongoose";
|
|
4
3
|
export interface ChangedData {
|
|
5
4
|
_id: {
|
|
@@ -25,7 +24,7 @@ export interface ChangedData {
|
|
|
25
24
|
};
|
|
26
25
|
fullDocument?: Record<string, any>;
|
|
27
26
|
}
|
|
28
|
-
export declare const makeTextFilter: (modelRef:
|
|
27
|
+
export declare const makeTextFilter: (modelRef: Cnst) => (data: Record<string, any>, assignObj?: {
|
|
29
28
|
[key: string]: string;
|
|
30
29
|
}) => TextDoc;
|
|
31
30
|
export declare class SearchDaemonModule {
|