@akanjs/server 0.0.45 → 0.0.47
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.js +2 -3785
- package/package.json +2 -23
- package/src/boot.js +197 -0
- package/src/controller.js +98 -0
- package/src/gql.js +145 -0
- package/src/index.js +26 -0
- package/src/module.js +246 -0
- package/src/processor.js +87 -0
- package/src/resolver.js +141 -0
- package/src/schema.js +238 -0
- package/src/searchDaemon.js +223 -0
- package/src/types.js +15 -0
- package/src/websocket.js +141 -0
package/src/schema.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var schema_exports = {};
|
|
19
|
+
__export(schema_exports, {
|
|
20
|
+
addSchema: () => addSchema,
|
|
21
|
+
schemaOf: () => schemaOf
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(schema_exports);
|
|
24
|
+
var import_base = require("@akanjs/base");
|
|
25
|
+
var import_common = require("@akanjs/common");
|
|
26
|
+
var import_constant = require("@akanjs/constant");
|
|
27
|
+
var import_document = require("@akanjs/document");
|
|
28
|
+
var import_signal = require("@akanjs/signal");
|
|
29
|
+
var import_mongoose = require("mongoose");
|
|
30
|
+
var import__ = require(".");
|
|
31
|
+
class ScalarSchemaStorage {
|
|
32
|
+
}
|
|
33
|
+
class SchemaStorage {
|
|
34
|
+
}
|
|
35
|
+
const scalarMongoTypeMap = /* @__PURE__ */ new Map([
|
|
36
|
+
[import_base.ID, import_document.ObjectId],
|
|
37
|
+
[import_base.Int, Number],
|
|
38
|
+
[import_base.Float, Number],
|
|
39
|
+
[import_base.JSON, import_mongoose.Schema.Types.Mixed],
|
|
40
|
+
[Map, Map],
|
|
41
|
+
[String, String],
|
|
42
|
+
[Boolean, Boolean],
|
|
43
|
+
[Date, Date]
|
|
44
|
+
]);
|
|
45
|
+
const applyMongoProp = (schemaProps, fieldMeta) => {
|
|
46
|
+
if (["id", "createdAt", "updatedAt"].includes(fieldMeta.key) || fieldMeta.fieldType === "resolve")
|
|
47
|
+
return;
|
|
48
|
+
const type = fieldMeta.isClass ? fieldMeta.isScalar ? createSchema(fieldMeta.modelRef) : import_document.ObjectId : scalarMongoTypeMap.get(fieldMeta.modelRef) ?? fieldMeta.modelRef;
|
|
49
|
+
let prop = {};
|
|
50
|
+
if (fieldMeta.optArrDepth) {
|
|
51
|
+
prop.type = type;
|
|
52
|
+
prop.required = true;
|
|
53
|
+
if (fieldMeta.isClass && !fieldMeta.refPath)
|
|
54
|
+
prop.ref = (0, import_constant.getClassMeta)(fieldMeta.modelRef).refName;
|
|
55
|
+
if (fieldMeta.refPath)
|
|
56
|
+
prop.refPath = fieldMeta.refPath;
|
|
57
|
+
if (typeof fieldMeta.min === "number")
|
|
58
|
+
prop.min = fieldMeta.min;
|
|
59
|
+
if (typeof fieldMeta.max === "number")
|
|
60
|
+
prop.max = fieldMeta.max;
|
|
61
|
+
if (fieldMeta.enum)
|
|
62
|
+
prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
|
|
63
|
+
if (typeof fieldMeta.minlength === "number")
|
|
64
|
+
prop.minlength = fieldMeta.minlength;
|
|
65
|
+
if (typeof fieldMeta.maxlength === "number")
|
|
66
|
+
prop.maxlength = fieldMeta.maxlength;
|
|
67
|
+
if (fieldMeta.validate) {
|
|
68
|
+
prop.validate = function(value) {
|
|
69
|
+
return fieldMeta.validate?.(fieldMeta.name === "Date" && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
prop = { type: (0, import_base.arraiedModel)(prop, fieldMeta.optArrDepth), default: [], required: true };
|
|
73
|
+
if (fieldMeta.modelRef.prototype === Date.prototype) {
|
|
74
|
+
prop.get = (dates) => dates.map((date) => (0, import_base.dayjs)(date));
|
|
75
|
+
prop.set = (days) => days.map((day) => day.toDate());
|
|
76
|
+
}
|
|
77
|
+
if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === import_base.ID.prototype) {
|
|
78
|
+
prop.get = (ids) => ids.map((id) => id.toString());
|
|
79
|
+
prop.set = (ids) => ids.map((id) => new import_mongoose.Types.ObjectId(id));
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
prop.type = (0, import_base.arraiedModel)(type, fieldMeta.arrDepth);
|
|
83
|
+
prop.required = !fieldMeta.nullable;
|
|
84
|
+
if (fieldMeta.isMap) {
|
|
85
|
+
prop.of = scalarMongoTypeMap.get(fieldMeta.of) ?? createSchema(fieldMeta.of);
|
|
86
|
+
if (!fieldMeta.default)
|
|
87
|
+
prop.default = /* @__PURE__ */ new Map();
|
|
88
|
+
}
|
|
89
|
+
if (fieldMeta.default !== null) {
|
|
90
|
+
if (typeof fieldMeta.default === "function")
|
|
91
|
+
prop.default = function() {
|
|
92
|
+
const def = fieldMeta.default(this);
|
|
93
|
+
return (0, import_common.isDayjs)(def) ? def.toDate() : def;
|
|
94
|
+
};
|
|
95
|
+
else
|
|
96
|
+
prop.default = (0, import_common.isDayjs)(fieldMeta.default) ? fieldMeta.default.toDate() : fieldMeta.default instanceof import_base.Enum ? [...fieldMeta.default.values] : fieldMeta.default;
|
|
97
|
+
}
|
|
98
|
+
if (typeof fieldMeta.immutable !== "undefined")
|
|
99
|
+
prop.immutable = fieldMeta.immutable;
|
|
100
|
+
if (fieldMeta.isClass && !fieldMeta.refPath)
|
|
101
|
+
prop.ref = (0, import_constant.getClassMeta)(fieldMeta.modelRef).refName;
|
|
102
|
+
if (fieldMeta.refPath)
|
|
103
|
+
prop.refPath = fieldMeta.refPath;
|
|
104
|
+
if (typeof fieldMeta.min === "number")
|
|
105
|
+
prop.min = fieldMeta.min;
|
|
106
|
+
if (typeof fieldMeta.max === "number")
|
|
107
|
+
prop.max = fieldMeta.max;
|
|
108
|
+
if (fieldMeta.enum)
|
|
109
|
+
prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
|
|
110
|
+
if (typeof fieldMeta.select === "boolean")
|
|
111
|
+
prop.select = fieldMeta.select;
|
|
112
|
+
if (typeof fieldMeta.minlength === "number")
|
|
113
|
+
prop.minlength = fieldMeta.minlength;
|
|
114
|
+
if (typeof fieldMeta.maxlength === "number")
|
|
115
|
+
prop.maxlength = fieldMeta.maxlength;
|
|
116
|
+
if (fieldMeta.nullable) {
|
|
117
|
+
prop.get = (v) => v === void 0 ? void 0 : v;
|
|
118
|
+
prop.set = (v) => v === null ? void 0 : v;
|
|
119
|
+
}
|
|
120
|
+
if (fieldMeta.modelRef.prototype === Date.prototype) {
|
|
121
|
+
prop.get = (date) => date ? (0, import_base.dayjs)(date) : void 0;
|
|
122
|
+
prop.set = (day) => day ? (0, import_base.dayjs)(day).toDate() : void 0;
|
|
123
|
+
}
|
|
124
|
+
if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === import_base.ID.prototype) {
|
|
125
|
+
prop.get = (id) => id ? id.toString() : void 0;
|
|
126
|
+
prop.set = (id) => id ? new import_mongoose.Types.ObjectId(id) : void 0;
|
|
127
|
+
}
|
|
128
|
+
if (fieldMeta.isClass && fieldMeta.isScalar && fieldMeta.default === null && !fieldMeta.nullable) {
|
|
129
|
+
prop.default = (0, import_signal.makeDefault)(fieldMeta.modelRef);
|
|
130
|
+
}
|
|
131
|
+
if (fieldMeta.validate) {
|
|
132
|
+
prop.validate = function(value) {
|
|
133
|
+
return fieldMeta.validate?.(fieldMeta.name === "Date" && !!value ? (0, import_base.dayjs)() : value, this) ?? true;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
schemaProps[fieldMeta.key] = prop;
|
|
138
|
+
};
|
|
139
|
+
const createSchema = (modelRef) => {
|
|
140
|
+
const classMeta = (0, import_constant.getClassMeta)(modelRef);
|
|
141
|
+
const schemaMeta = Reflect.getMetadata(classMeta.refName, ScalarSchemaStorage.prototype);
|
|
142
|
+
if (schemaMeta)
|
|
143
|
+
return schemaMeta;
|
|
144
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
145
|
+
const schemaProps = {};
|
|
146
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
147
|
+
applyMongoProp(schemaProps, fieldMeta);
|
|
148
|
+
});
|
|
149
|
+
const schema = new import_mongoose.Schema(schemaProps);
|
|
150
|
+
Reflect.defineMetadata(classMeta.refName, schema, ScalarSchemaStorage.prototype);
|
|
151
|
+
return schema;
|
|
152
|
+
};
|
|
153
|
+
const schemaOf = (modelRef, docRef, middleware) => {
|
|
154
|
+
const classMeta = (0, import_constant.getClassMeta)(docRef);
|
|
155
|
+
const schemaMeta = Reflect.getMetadata(classMeta.refName, SchemaStorage.prototype);
|
|
156
|
+
if (schemaMeta)
|
|
157
|
+
return schemaMeta;
|
|
158
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(docRef);
|
|
159
|
+
const schemaProps = {
|
|
160
|
+
createdAt: {
|
|
161
|
+
type: Date,
|
|
162
|
+
get: (date) => date ? (0, import_base.dayjs)(date) : date,
|
|
163
|
+
set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
|
|
164
|
+
},
|
|
165
|
+
updatedAt: {
|
|
166
|
+
type: Date,
|
|
167
|
+
get: (date) => date ? (0, import_base.dayjs)(date) : date,
|
|
168
|
+
set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
172
|
+
applyMongoProp(schemaProps, fieldMeta);
|
|
173
|
+
});
|
|
174
|
+
const schema = new import_mongoose.Schema(schemaProps, (0, import_document.getDefaultSchemaOptions)());
|
|
175
|
+
schema.methods.refresh = async function() {
|
|
176
|
+
Object.assign(this, await this.constructor.findById(this._id));
|
|
177
|
+
return this;
|
|
178
|
+
};
|
|
179
|
+
Object.getOwnPropertyNames(docRef.prototype).forEach((name) => {
|
|
180
|
+
if (name === "constructor")
|
|
181
|
+
return;
|
|
182
|
+
schema.methods[name] = Object.getOwnPropertyDescriptor(docRef.prototype, name)?.value;
|
|
183
|
+
});
|
|
184
|
+
schema.pre("save", async function(next) {
|
|
185
|
+
const model = this.constructor;
|
|
186
|
+
if (this.isNew)
|
|
187
|
+
model.addSummary(["total", this.status]);
|
|
188
|
+
else if (!!this.removedAt && this.isModified("removedAt"))
|
|
189
|
+
model.subSummary(["total", this.status]);
|
|
190
|
+
next();
|
|
191
|
+
});
|
|
192
|
+
const onSchema = Object.getOwnPropertyDescriptor(middleware.prototype, "onSchema")?.value;
|
|
193
|
+
onSchema?.(schema);
|
|
194
|
+
schema.index({ removedAt: -1 });
|
|
195
|
+
Reflect.defineMetadata(classMeta.refName, schema, SchemaStorage.prototype);
|
|
196
|
+
return schema;
|
|
197
|
+
};
|
|
198
|
+
const addSchema = (modelRef, docRef, inputRef, middleware) => {
|
|
199
|
+
const originDocClassMeta = (0, import_constant.getClassMeta)(docRef);
|
|
200
|
+
const originInputClassMeta = (0, import_constant.getClassMeta)(inputRef);
|
|
201
|
+
const originDoc = (0, import_constant.getFullModelRef)(originDocClassMeta.refName);
|
|
202
|
+
const originInput = (0, import_constant.getInputModelRef)(originInputClassMeta.refName);
|
|
203
|
+
const classMeta = (0, import_constant.getClassMeta)(docRef);
|
|
204
|
+
const modelSchema = Reflect.getMetadata(classMeta.refName, SchemaStorage.prototype);
|
|
205
|
+
if (!modelSchema)
|
|
206
|
+
throw new Error(`Schema of ${classMeta.refName} not found`);
|
|
207
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(docRef);
|
|
208
|
+
const schemaProps = {
|
|
209
|
+
createdAt: {
|
|
210
|
+
type: Date,
|
|
211
|
+
get: (date) => date ? (0, import_base.dayjs)(date) : date,
|
|
212
|
+
set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
|
|
213
|
+
},
|
|
214
|
+
updatedAt: {
|
|
215
|
+
type: Date,
|
|
216
|
+
get: (date) => date ? (0, import_base.dayjs)(date) : date,
|
|
217
|
+
set: (day) => day ? (0, import_base.dayjs)(day).toDate() : day
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
221
|
+
applyMongoProp(schemaProps, fieldMeta);
|
|
222
|
+
(0, import__.applyNestField)(originDoc, fieldMeta);
|
|
223
|
+
});
|
|
224
|
+
const inputFieldMetas = (0, import_constant.getFieldMetas)(inputRef);
|
|
225
|
+
inputFieldMetas.forEach((fieldMeta) => {
|
|
226
|
+
(0, import__.applyNestField)(originInput, fieldMeta, "input");
|
|
227
|
+
});
|
|
228
|
+
const schema = new import_mongoose.Schema(schemaProps, (0, import_document.getDefaultSchemaOptions)());
|
|
229
|
+
modelSchema.add(schema);
|
|
230
|
+
Object.getOwnPropertyNames(docRef.prototype).forEach((name) => {
|
|
231
|
+
if (name === "constructor")
|
|
232
|
+
return;
|
|
233
|
+
modelSchema.methods[name] = Object.getOwnPropertyDescriptor(docRef.prototype, name)?.value;
|
|
234
|
+
});
|
|
235
|
+
const onSchema = Object.getOwnPropertyDescriptor(middleware.prototype, "onSchema")?.value;
|
|
236
|
+
onSchema?.(modelSchema);
|
|
237
|
+
return modelSchema;
|
|
238
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
19
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
20
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
21
|
+
if (decorator = decorators[i])
|
|
22
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
23
|
+
if (kind && result)
|
|
24
|
+
__defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
28
|
+
var searchDaemon_exports = {};
|
|
29
|
+
__export(searchDaemon_exports, {
|
|
30
|
+
SearchDaemonModule: () => SearchDaemonModule,
|
|
31
|
+
makeTextFilter: () => makeTextFilter
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(searchDaemon_exports);
|
|
34
|
+
var import_common = require("@akanjs/common");
|
|
35
|
+
var import_constant = require("@akanjs/constant");
|
|
36
|
+
var import_document = require("@akanjs/document");
|
|
37
|
+
var import_common2 = require("@nestjs/common");
|
|
38
|
+
var import_mongoose = require("@nestjs/mongoose");
|
|
39
|
+
const hasTextField = (modelRef) => {
|
|
40
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
41
|
+
return fieldMetas.some(
|
|
42
|
+
(fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
const getTextFieldKeys = (modelRef) => {
|
|
46
|
+
const allSearchFields = [];
|
|
47
|
+
const allFilterFields = [];
|
|
48
|
+
const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
|
|
49
|
+
const fieldMetas = [...fieldMetaMap.values()];
|
|
50
|
+
const stringTextFields = fieldMetas.filter((fieldMeta) => !!fieldMeta.text).map((fieldMeta) => {
|
|
51
|
+
if (fieldMeta.text === "filter")
|
|
52
|
+
allFilterFields.push(fieldMeta.key);
|
|
53
|
+
else if (fieldMeta.text === "search")
|
|
54
|
+
allSearchFields.push(fieldMeta.key);
|
|
55
|
+
return fieldMeta.key;
|
|
56
|
+
});
|
|
57
|
+
const scalarTextFields = fieldMetas.filter(
|
|
58
|
+
(fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
59
|
+
).map((fieldMeta) => fieldMeta.key);
|
|
60
|
+
const deepFields = scalarTextFields.map((key) => {
|
|
61
|
+
const fieldMeta = fieldMetaMap.get(key);
|
|
62
|
+
if (!fieldMeta)
|
|
63
|
+
throw new Error(`No fieldMeta for ${key}`);
|
|
64
|
+
const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(
|
|
65
|
+
fieldMeta.modelRef
|
|
66
|
+
);
|
|
67
|
+
allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
|
|
68
|
+
allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
|
|
69
|
+
return [
|
|
70
|
+
...stringTextFields2.map((field) => `${key}.${field}`),
|
|
71
|
+
...allTextFields.map((field) => `${key}.${field}`)
|
|
72
|
+
];
|
|
73
|
+
}).flat();
|
|
74
|
+
return {
|
|
75
|
+
stringTextFields,
|
|
76
|
+
scalarTextFields,
|
|
77
|
+
allTextFields: [...stringTextFields, ...deepFields],
|
|
78
|
+
allSearchFields,
|
|
79
|
+
allFilterFields
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const makeTextFilter = (modelRef) => {
|
|
83
|
+
const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
|
|
84
|
+
const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
|
|
85
|
+
const filterData = (data, assignObj = {}) => {
|
|
86
|
+
if (Array.isArray(data))
|
|
87
|
+
return data.map((d) => filterData(d));
|
|
88
|
+
return Object.assign(
|
|
89
|
+
Object.fromEntries([
|
|
90
|
+
...stringTextFields.map((key) => [key, data[key]]),
|
|
91
|
+
...scalarTextFields.map((key) => {
|
|
92
|
+
const fieldMeta = fieldMetaMap.get(key);
|
|
93
|
+
if (!fieldMeta)
|
|
94
|
+
throw new Error(`No fieldMeta for ${key}`);
|
|
95
|
+
const filterFunc = makeTextFilter(fieldMeta.modelRef);
|
|
96
|
+
return [key, filterFunc(data[key])];
|
|
97
|
+
})
|
|
98
|
+
]),
|
|
99
|
+
assignObj
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
return filterData;
|
|
103
|
+
};
|
|
104
|
+
const getSortableAttributes = (refName) => {
|
|
105
|
+
const cnst = (0, import_constant.getCnstMeta)(refName);
|
|
106
|
+
const sortMap = (0, import_constant.getFilterSortMap)(cnst.Filter);
|
|
107
|
+
const sortFields = Object.values(sortMap).filter((val) => typeof val === "object").map((sort) => Object.keys(sort)).flat();
|
|
108
|
+
return [...new Set(sortFields)];
|
|
109
|
+
};
|
|
110
|
+
let SearchDaemon = class {
|
|
111
|
+
constructor(connection, meili) {
|
|
112
|
+
this.connection = connection;
|
|
113
|
+
this.meili = meili;
|
|
114
|
+
}
|
|
115
|
+
logger = new import_common.Logger("SearchDaemon");
|
|
116
|
+
async onModuleInit() {
|
|
117
|
+
const databaseModelNames = (0, import_document.getAllDatabaseModelNames)();
|
|
118
|
+
const indexes = (await this.meili.getIndexes({ limit: 1e3 })).results;
|
|
119
|
+
const indexMap = new Map(indexes.map((index) => [index.uid, index]));
|
|
120
|
+
const indexCreationNames = [];
|
|
121
|
+
const indexUpdateNames = [];
|
|
122
|
+
for (const modelName of databaseModelNames) {
|
|
123
|
+
const indexName = (0, import_common.lowerlize)(modelName);
|
|
124
|
+
const modelRef = (0, import_constant.getFullModelRef)(modelName);
|
|
125
|
+
if (!hasTextField(modelRef))
|
|
126
|
+
continue;
|
|
127
|
+
const index = indexMap.get(indexName);
|
|
128
|
+
if (!index)
|
|
129
|
+
indexCreationNames.push(indexName);
|
|
130
|
+
else if (index.primaryKey !== "id")
|
|
131
|
+
indexUpdateNames.push(indexName);
|
|
132
|
+
}
|
|
133
|
+
for (const indexName of indexCreationNames)
|
|
134
|
+
await this.meili.createIndex(indexName, { primaryKey: "id" });
|
|
135
|
+
for (const indexName of indexUpdateNames)
|
|
136
|
+
await this.meili.updateIndex(indexName, { primaryKey: "id" });
|
|
137
|
+
for (const modelName of databaseModelNames) {
|
|
138
|
+
const indexName = (0, import_common.lowerlize)(modelName);
|
|
139
|
+
const model = this.connection.models[modelName];
|
|
140
|
+
const modelRef = (0, import_constant.getFullModelRef)(modelName);
|
|
141
|
+
if (!hasTextField(modelRef))
|
|
142
|
+
continue;
|
|
143
|
+
const searchIndex = this.meili.index(indexName);
|
|
144
|
+
const { stringTextFields, scalarTextFields, allSearchFields, allFilterFields } = getTextFieldKeys(modelRef);
|
|
145
|
+
const settings = await searchIndex.getSettings();
|
|
146
|
+
const allSearchFieldSet = new Set(allSearchFields);
|
|
147
|
+
const allFilterFieldSet = new Set(allFilterFields);
|
|
148
|
+
const searchFieldSet = new Set(settings.searchableAttributes);
|
|
149
|
+
const filterFieldSet = new Set(settings.filterableAttributes);
|
|
150
|
+
const needUpdateSetting = !allSearchFields.every((field) => searchFieldSet.has(field)) || !allFilterFields.every((field) => filterFieldSet.has(field)) || !settings.searchableAttributes?.every((field) => allSearchFieldSet.has(field)) || !settings.filterableAttributes?.every((field) => allFilterFieldSet.has(field));
|
|
151
|
+
if (needUpdateSetting) {
|
|
152
|
+
this.logger.info(`update index settings (${modelName})`);
|
|
153
|
+
await searchIndex.updateSettings({
|
|
154
|
+
searchableAttributes: allSearchFields,
|
|
155
|
+
filterableAttributes: allFilterFields,
|
|
156
|
+
sortableAttributes: getSortableAttributes(indexName)
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
const stringTextFieldSet = new Set(stringTextFields);
|
|
160
|
+
const scalarTextFieldSet = new Set(scalarTextFields);
|
|
161
|
+
const filterText = makeTextFilter(modelRef);
|
|
162
|
+
model.watch().on("change", async (data) => {
|
|
163
|
+
try {
|
|
164
|
+
const id = data.documentKey._id.toString();
|
|
165
|
+
if (data.operationType === "delete") {
|
|
166
|
+
this.logger.trace(`delete text doc (${modelName}): ${id}`);
|
|
167
|
+
return await searchIndex.deleteDocument(id);
|
|
168
|
+
} else if (data.operationType === "insert") {
|
|
169
|
+
this.logger.trace(`insert text doc (${modelName}): ${data.documentKey._id}`);
|
|
170
|
+
if (!data.fullDocument)
|
|
171
|
+
throw new Error("No fullDocument");
|
|
172
|
+
const textFilteredData = filterText(data.fullDocument);
|
|
173
|
+
return await searchIndex.addDocuments([textFilteredData]);
|
|
174
|
+
} else if (data.operationType === "update") {
|
|
175
|
+
const updatedFields = data.updateDescription?.updatedFields ?? {};
|
|
176
|
+
const isRemoved = !!updatedFields.removedAt;
|
|
177
|
+
if (isRemoved) {
|
|
178
|
+
this.logger.trace(`remove text doc (${modelName}): ${id}`);
|
|
179
|
+
return await searchIndex.deleteDocument(id);
|
|
180
|
+
}
|
|
181
|
+
this.logger.trace(`update text doc (${modelName}): ${data.documentKey._id}`);
|
|
182
|
+
const updatedFieldKeys = Object.keys(updatedFields);
|
|
183
|
+
const removedFieldKeys = data.updateDescription?.removedFields ?? [];
|
|
184
|
+
const isScalarTextFieldUpdated = [...updatedFieldKeys, ...removedFieldKeys].map((key) => key.split(".")[0]).some((key) => scalarTextFieldSet.has(key));
|
|
185
|
+
if (isScalarTextFieldUpdated) {
|
|
186
|
+
const doc = await model.findById(data.documentKey._id);
|
|
187
|
+
if (!doc)
|
|
188
|
+
this.logger.error(`No doc for ${data.documentKey._id}`);
|
|
189
|
+
const textFilteredData = filterText(doc, { id });
|
|
190
|
+
return await searchIndex.updateDocuments([textFilteredData]);
|
|
191
|
+
} else {
|
|
192
|
+
const updateKeys = updatedFieldKeys.filter((key) => stringTextFieldSet.has(key));
|
|
193
|
+
const removeKeys = removedFieldKeys.filter((key) => stringTextFieldSet.has(key));
|
|
194
|
+
if (!updateKeys.length && !removeKeys.length)
|
|
195
|
+
return;
|
|
196
|
+
const textFilteredData = Object.fromEntries([
|
|
197
|
+
["id", id],
|
|
198
|
+
...updateKeys.map((key) => [key, updatedFields[key]]),
|
|
199
|
+
...removeKeys.map((key) => [key, null])
|
|
200
|
+
]);
|
|
201
|
+
return await searchIndex.updateDocuments([textFilteredData]);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} catch (e) {
|
|
205
|
+
this.logger.error(e);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
SearchDaemon = __decorateClass([
|
|
212
|
+
(0, import_common2.Injectable)(),
|
|
213
|
+
__decorateParam(0, (0, import_mongoose.InjectConnection)()),
|
|
214
|
+
__decorateParam(1, (0, import_common2.Inject)("MEILI_CLIENT"))
|
|
215
|
+
], SearchDaemon);
|
|
216
|
+
let SearchDaemonModule = class {
|
|
217
|
+
};
|
|
218
|
+
SearchDaemonModule = __decorateClass([
|
|
219
|
+
(0, import_common2.Global)(),
|
|
220
|
+
(0, import_common2.Module)({
|
|
221
|
+
providers: [SearchDaemon]
|
|
222
|
+
})
|
|
223
|
+
], SearchDaemonModule);
|
package/src/types.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
package/src/websocket.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
19
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
20
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
21
|
+
if (decorator = decorators[i])
|
|
22
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
23
|
+
if (kind && result)
|
|
24
|
+
__defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var websocket_exports = {};
|
|
28
|
+
__export(websocket_exports, {
|
|
29
|
+
websocketOf: () => websocketOf,
|
|
30
|
+
websocketServerOf: () => websocketServerOf
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(websocket_exports);
|
|
33
|
+
var import_common = require("@akanjs/common");
|
|
34
|
+
var import_nest = require("@akanjs/nest");
|
|
35
|
+
var import_service = require("@akanjs/service");
|
|
36
|
+
var import_signal = require("@akanjs/signal");
|
|
37
|
+
var import_common2 = require("@nestjs/common");
|
|
38
|
+
var import_websockets = require("@nestjs/websockets");
|
|
39
|
+
var import_operators = require("rxjs/operators");
|
|
40
|
+
const internalArgMap = { Account: import_nest.Account, UserIp: import_nest.UserIp, Access: import_nest.Access, Self: import_nest.Self, Me: import_nest.Me, Ws: import_nest.Ws };
|
|
41
|
+
let TransformInterceptor = class {
|
|
42
|
+
intercept(context, next) {
|
|
43
|
+
const [, gqlKey] = [context.getArgByIndex(1), context.getArgByIndex(3)];
|
|
44
|
+
return next.handle().pipe((0, import_operators.map)((data) => ({ event: gqlKey, data })));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
TransformInterceptor = __decorateClass([
|
|
48
|
+
(0, import_common2.Injectable)()
|
|
49
|
+
], TransformInterceptor);
|
|
50
|
+
const makeRoomId = (gqlKey, argValues) => `${gqlKey}-${argValues.join("-")}`;
|
|
51
|
+
const getPubsubInterceptor = (argMetas) => {
|
|
52
|
+
let PubsubInterceptor = class {
|
|
53
|
+
async intercept(context, next) {
|
|
54
|
+
const [socket, { __subscribe__, ...body }, gqlKey] = [context.getArgByIndex(0), context.getArgByIndex(1), context.getArgByIndex(3)];
|
|
55
|
+
const roomId = makeRoomId(
|
|
56
|
+
gqlKey,
|
|
57
|
+
argMetas.map((argMeta) => body[argMeta.name])
|
|
58
|
+
);
|
|
59
|
+
if (__subscribe__)
|
|
60
|
+
await socket.join(roomId);
|
|
61
|
+
else
|
|
62
|
+
await socket.leave(roomId);
|
|
63
|
+
return next.handle().pipe((0, import_operators.map)(() => ({ event: gqlKey, data: { roomId, __subscribe__ } })));
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
PubsubInterceptor = __decorateClass([
|
|
67
|
+
(0, import_common2.Injectable)()
|
|
68
|
+
], PubsubInterceptor);
|
|
69
|
+
return PubsubInterceptor;
|
|
70
|
+
};
|
|
71
|
+
const websocketOf = (sigRef, allSrvs) => {
|
|
72
|
+
const sigMeta = (0, import_signal.getSigMeta)(sigRef);
|
|
73
|
+
class WsGateway {
|
|
74
|
+
__sigRef__ = sigRef;
|
|
75
|
+
}
|
|
76
|
+
Object.keys(allSrvs).forEach((srv) => {
|
|
77
|
+
if (!(0, import_service.isServiceEnabled)(allSrvs[srv]))
|
|
78
|
+
return;
|
|
79
|
+
(0, import_common2.Inject)(allSrvs[srv])(WsGateway.prototype, (0, import_common.lowerlize)(srv));
|
|
80
|
+
});
|
|
81
|
+
const messageGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Message");
|
|
82
|
+
for (const gqlMeta of messageGqlMetas) {
|
|
83
|
+
const descriptor = { ...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {} };
|
|
84
|
+
Object.defineProperty(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
85
|
+
const [argMetas, internalArgMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
86
|
+
argMetas.forEach((argMeta) => {
|
|
87
|
+
if (argMeta.type !== "Msg")
|
|
88
|
+
throw new Error(`Argument of Message should be Msg ${sigMeta.refName}-${gqlMeta.key}-${argMeta.name}`);
|
|
89
|
+
(0, import_websockets.MessageBody)(argMeta.name, ...(0, import_nest.getBodyPipes)(argMeta))(WsGateway.prototype, gqlMeta.key, argMeta.idx);
|
|
90
|
+
});
|
|
91
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
92
|
+
const internalDecorator = internalArgMap[internalArgMeta.type];
|
|
93
|
+
internalDecorator(internalArgMeta.option ?? {})(WsGateway.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
94
|
+
});
|
|
95
|
+
(0, import_common2.UseInterceptors)(TransformInterceptor)(WsGateway.prototype, gqlMeta.key, gqlMeta.descriptor);
|
|
96
|
+
(0, import_websockets.SubscribeMessage)(gqlMeta.key)(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
97
|
+
}
|
|
98
|
+
const pubsubGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Pubsub");
|
|
99
|
+
for (const gqlMeta of pubsubGqlMetas) {
|
|
100
|
+
const descriptor = { ...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {} };
|
|
101
|
+
Object.defineProperty(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
102
|
+
const [argMetas, internalArgMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
103
|
+
argMetas.forEach((argMeta) => {
|
|
104
|
+
if (argMeta.type !== "Room")
|
|
105
|
+
throw new Error(`Argument of Message should be Room ${sigMeta.refName}-${gqlMeta.key}-${argMeta.name}`);
|
|
106
|
+
(0, import_websockets.MessageBody)(argMeta.name, ...(0, import_nest.getBodyPipes)(argMeta))(WsGateway.prototype, gqlMeta.key, argMeta.idx);
|
|
107
|
+
});
|
|
108
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
109
|
+
const internalDecorator = internalArgMap[internalArgMeta.type];
|
|
110
|
+
internalDecorator(internalArgMeta.option ?? {})(WsGateway.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
111
|
+
});
|
|
112
|
+
(0, import_common2.UseInterceptors)(getPubsubInterceptor(argMetas))(WsGateway.prototype, gqlMeta.key, gqlMeta.descriptor);
|
|
113
|
+
(0, import_websockets.SubscribeMessage)(gqlMeta.key)(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
114
|
+
}
|
|
115
|
+
(0, import_websockets.WebSocketGateway)({ cors: { origin: "*" }, transports: ["websocket"] })(WsGateway);
|
|
116
|
+
return WsGateway;
|
|
117
|
+
};
|
|
118
|
+
const websocketServerOf = (sigRef) => {
|
|
119
|
+
const pubsubGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Pubsub");
|
|
120
|
+
let Websocket = class {
|
|
121
|
+
server;
|
|
122
|
+
};
|
|
123
|
+
__decorateClass([
|
|
124
|
+
(0, import_websockets.WebSocketServer)()
|
|
125
|
+
], Websocket.prototype, "server", 2);
|
|
126
|
+
Websocket = __decorateClass([
|
|
127
|
+
(0, import_common2.Injectable)(),
|
|
128
|
+
(0, import_websockets.WebSocketGateway)({ cors: { origin: "*" }, transports: ["websocket"] })
|
|
129
|
+
], Websocket);
|
|
130
|
+
for (const gqlMeta of pubsubGqlMetas) {
|
|
131
|
+
const [argMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
132
|
+
Websocket.prototype[gqlMeta.key] = function(...args) {
|
|
133
|
+
const roomId = makeRoomId(
|
|
134
|
+
gqlMeta.key,
|
|
135
|
+
argMetas.map((argMeta) => args[argMeta.idx])
|
|
136
|
+
);
|
|
137
|
+
this.server.to(roomId).emit(roomId, args.at(-1));
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return Websocket;
|
|
141
|
+
};
|