@akanjs/server 0.0.53 → 0.0.55
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/boot.mjs +191 -0
- package/src/controller.mjs +109 -0
- package/src/gql.mjs +123 -0
- package/src/index.mjs +10 -0
- package/src/module.mjs +229 -0
- package/src/processor.mjs +73 -0
- package/src/resolver.mjs +118 -0
- package/src/schema.mjs +219 -0
- package/src/searchDaemon.mjs +212 -0
- package/src/types.mjs +0 -0
- package/src/websocket.mjs +124 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { lowerlize } from "@akanjs/common";
|
|
2
|
+
import { isServiceEnabled } from "@akanjs/service";
|
|
3
|
+
import {
|
|
4
|
+
deserializeArg,
|
|
5
|
+
getArgMetas,
|
|
6
|
+
getGqlMetas,
|
|
7
|
+
getSigMeta
|
|
8
|
+
} from "@akanjs/signal";
|
|
9
|
+
import { Process, Processor } from "@nestjs/bull";
|
|
10
|
+
import { Inject } from "@nestjs/common";
|
|
11
|
+
const convertProcessFunction = (gqlMeta, argMetas, internalArgMetas, fn) => {
|
|
12
|
+
return async function(job, done) {
|
|
13
|
+
const args = [];
|
|
14
|
+
argMetas.forEach((argMeta) => {
|
|
15
|
+
if (argMeta.type === "Msg")
|
|
16
|
+
args[argMeta.idx] = deserializeArg(argMeta, job.data[argMeta.idx]);
|
|
17
|
+
else
|
|
18
|
+
throw new Error(`Invalid ArgMeta Type ${argMeta.type}`);
|
|
19
|
+
});
|
|
20
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
21
|
+
if (internalArgMeta.type === "Job")
|
|
22
|
+
args[internalArgMeta.idx] = job;
|
|
23
|
+
else
|
|
24
|
+
throw new Error(`Invalid InternalArgMeta Type ${internalArgMeta.type}`);
|
|
25
|
+
});
|
|
26
|
+
this.logger?.log(`Process-${gqlMeta.key} started`);
|
|
27
|
+
const result = await fn.apply(this, args);
|
|
28
|
+
this.logger?.log(`Process-${gqlMeta.key} finished`);
|
|
29
|
+
done(null, result);
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
const processorOf = (sigRef, allSrvs) => {
|
|
33
|
+
const sigMeta = getSigMeta(sigRef);
|
|
34
|
+
const serverMode = process.env.SERVER_MODE ?? "federation";
|
|
35
|
+
const gqlMetas = getGqlMetas(sigRef).filter((gqlMeta) => gqlMeta.type === "Process").filter(
|
|
36
|
+
(gqlMeta) => gqlMeta.signalOption.serverType === "all" || serverMode === "all" || gqlMeta.signalOption.serverType === serverMode
|
|
37
|
+
);
|
|
38
|
+
class QueueProcessor {
|
|
39
|
+
}
|
|
40
|
+
Object.keys(allSrvs).forEach((srv) => {
|
|
41
|
+
if (!isServiceEnabled(allSrvs[srv]))
|
|
42
|
+
return;
|
|
43
|
+
Inject(allSrvs[srv])(QueueProcessor.prototype, lowerlize(srv));
|
|
44
|
+
});
|
|
45
|
+
for (const gqlMeta of gqlMetas) {
|
|
46
|
+
const [argMetas, internalArgMetas] = getArgMetas(sigRef, gqlMeta.key);
|
|
47
|
+
const descriptor = { ...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {} };
|
|
48
|
+
descriptor.value = convertProcessFunction(
|
|
49
|
+
gqlMeta,
|
|
50
|
+
argMetas,
|
|
51
|
+
internalArgMetas,
|
|
52
|
+
descriptor.value
|
|
53
|
+
);
|
|
54
|
+
Object.defineProperty(QueueProcessor.prototype, gqlMeta.key, descriptor);
|
|
55
|
+
Process(gqlMeta.key)(QueueProcessor.prototype, gqlMeta.key, descriptor);
|
|
56
|
+
}
|
|
57
|
+
Processor(sigMeta.refName)(QueueProcessor);
|
|
58
|
+
return QueueProcessor;
|
|
59
|
+
};
|
|
60
|
+
const queueOf = (sigRef, queue) => {
|
|
61
|
+
const sigMeta = getSigMeta(sigRef);
|
|
62
|
+
const gqlMetas = getGqlMetas(sigRef).filter((gqlMeta) => gqlMeta.type === "Process");
|
|
63
|
+
for (const gqlMeta of gqlMetas) {
|
|
64
|
+
if (queue[gqlMeta.key])
|
|
65
|
+
throw new Error(`Queue already has ${gqlMeta.key} in ${sigMeta.refName}`);
|
|
66
|
+
queue[gqlMeta.key] = (...args) => queue.add(gqlMeta.key, args);
|
|
67
|
+
}
|
|
68
|
+
return queue;
|
|
69
|
+
};
|
|
70
|
+
export {
|
|
71
|
+
processorOf,
|
|
72
|
+
queueOf
|
|
73
|
+
};
|
package/src/resolver.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { arraiedModel, Float, getNonArrayModel, ID, Int, JSON, Upload } from "@akanjs/base";
|
|
2
|
+
import { capitalize, lowerlize } from "@akanjs/common";
|
|
3
|
+
import { getClassMeta, getFieldMetas } from "@akanjs/constant";
|
|
4
|
+
import { Access, Account, guards, Me, Req, Res, Self, UserIp } from "@akanjs/nest";
|
|
5
|
+
import { isServiceEnabled } from "@akanjs/service";
|
|
6
|
+
import {
|
|
7
|
+
copySignal,
|
|
8
|
+
getArgMetas,
|
|
9
|
+
getGqlMetas,
|
|
10
|
+
getResolveFieldMetas,
|
|
11
|
+
getSigMeta
|
|
12
|
+
} from "@akanjs/signal";
|
|
13
|
+
import { Inject, UseGuards } from "@nestjs/common";
|
|
14
|
+
import * as Nest from "@nestjs/graphql";
|
|
15
|
+
import GraphQLJson from "graphql-type-json";
|
|
16
|
+
import { GraphQLUpload } from "graphql-upload";
|
|
17
|
+
import { generateGql, generateGqlInput } from "./gql";
|
|
18
|
+
const scalarNestReturnMap = /* @__PURE__ */ new Map([
|
|
19
|
+
[Upload, GraphQLUpload],
|
|
20
|
+
[ID, Nest.ID],
|
|
21
|
+
[Int, Nest.Int],
|
|
22
|
+
[Float, Nest.Float],
|
|
23
|
+
[JSON, GraphQLJson],
|
|
24
|
+
[Boolean, Boolean],
|
|
25
|
+
[Date, Date],
|
|
26
|
+
[String, String],
|
|
27
|
+
[Map, GraphQLJson]
|
|
28
|
+
]);
|
|
29
|
+
const getNestReturn = (returns, type = "object") => {
|
|
30
|
+
const [model, arrDepth] = getNonArrayModel(returns());
|
|
31
|
+
const modelRef = scalarNestReturnMap.get(model) ?? (type === "object" ? generateGql(model) : generateGqlInput(model));
|
|
32
|
+
return () => arraiedModel(modelRef, arrDepth);
|
|
33
|
+
};
|
|
34
|
+
const internalArgMap = {
|
|
35
|
+
Parent: Nest.Parent,
|
|
36
|
+
Account,
|
|
37
|
+
UserIp,
|
|
38
|
+
Access,
|
|
39
|
+
Self,
|
|
40
|
+
Me,
|
|
41
|
+
Req,
|
|
42
|
+
Res
|
|
43
|
+
};
|
|
44
|
+
const resolverOf = (sigRef, allSrvs) => {
|
|
45
|
+
const Rsv = copySignal(sigRef);
|
|
46
|
+
const sigMeta = getSigMeta(Rsv);
|
|
47
|
+
const gqlMetas = getGqlMetas(Rsv);
|
|
48
|
+
Object.keys(allSrvs).forEach((srv) => {
|
|
49
|
+
if (!isServiceEnabled(allSrvs[srv]))
|
|
50
|
+
return;
|
|
51
|
+
Inject(allSrvs[srv])(Rsv.prototype, lowerlize(srv));
|
|
52
|
+
});
|
|
53
|
+
for (const gqlMeta of gqlMetas) {
|
|
54
|
+
if (gqlMeta.guards.some((guard) => guard === "None") || gqlMeta.signalOption.onlyFor === "restapi" || !["Query", "Mutation"].includes(gqlMeta.type))
|
|
55
|
+
continue;
|
|
56
|
+
else if (gqlMeta.signalOption.sso)
|
|
57
|
+
continue;
|
|
58
|
+
const [argMetas, internalArgMetas] = getArgMetas(Rsv, gqlMeta.key);
|
|
59
|
+
const descriptor = Object.getOwnPropertyDescriptor(Rsv.prototype, gqlMeta.key) ?? {};
|
|
60
|
+
for (const argMeta of argMetas) {
|
|
61
|
+
Nest.Args({
|
|
62
|
+
name: argMeta.name,
|
|
63
|
+
type: getNestReturn(argMeta.returns, "input"),
|
|
64
|
+
...argMeta.argsOption
|
|
65
|
+
})(Rsv.prototype, gqlMeta.key, argMeta.idx);
|
|
66
|
+
}
|
|
67
|
+
for (const internalArgMeta of internalArgMetas) {
|
|
68
|
+
const decorate = internalArgMap[internalArgMeta.type];
|
|
69
|
+
decorate(internalArgMeta.option ?? {})(Rsv.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
70
|
+
}
|
|
71
|
+
UseGuards(...gqlMeta.guards.map((guard) => guards[guard]))(Rsv.prototype, gqlMeta.key, descriptor);
|
|
72
|
+
if (gqlMeta.type === "Query")
|
|
73
|
+
Nest.Query(getNestReturn(gqlMeta.returns), gqlMeta.signalOption)(Rsv.prototype, gqlMeta.key, descriptor);
|
|
74
|
+
else if (gqlMeta.type === "Mutation")
|
|
75
|
+
Nest.Mutation(getNestReturn(gqlMeta.returns), gqlMeta.signalOption)(Rsv.prototype, gqlMeta.key, descriptor);
|
|
76
|
+
}
|
|
77
|
+
const resolveFieldMetas = getResolveFieldMetas(Rsv);
|
|
78
|
+
if (sigMeta.returns) {
|
|
79
|
+
const modelRef = sigMeta.returns();
|
|
80
|
+
const fieldMetas = getFieldMetas(modelRef);
|
|
81
|
+
fieldMetas.filter((fieldMeta) => fieldMeta.isClass && !fieldMeta.isScalar).forEach((fieldMeta) => {
|
|
82
|
+
const classMeta = getClassMeta(fieldMeta.modelRef);
|
|
83
|
+
const modelName = lowerlize(classMeta.type === "light" ? classMeta.refName.slice(5) : classMeta.refName);
|
|
84
|
+
const className = capitalize(modelName);
|
|
85
|
+
const serviceName = `${modelName}Service`;
|
|
86
|
+
Rsv.prototype[fieldMeta.key] = async function(parent) {
|
|
87
|
+
const service = this[serviceName];
|
|
88
|
+
return fieldMeta.arrDepth ? await service[`load${className}Many`](parent[fieldMeta.key]) : await service[`load${className}`](parent[fieldMeta.key]);
|
|
89
|
+
};
|
|
90
|
+
Nest.Parent()(Rsv.prototype, fieldMeta.key, 0);
|
|
91
|
+
Nest.ResolveField(getNestReturn(() => arraiedModel(fieldMeta.modelRef, fieldMeta.arrDepth)))(
|
|
92
|
+
Rsv.prototype,
|
|
93
|
+
fieldMeta.key,
|
|
94
|
+
Object.getOwnPropertyDescriptor(Rsv.prototype, fieldMeta.key) ?? {}
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
for (const resolveFieldMeta of resolveFieldMetas) {
|
|
99
|
+
const [, internalArgMetas] = getArgMetas(Rsv, resolveFieldMeta.key);
|
|
100
|
+
for (const internalArgMeta of internalArgMetas) {
|
|
101
|
+
const decorate = internalArgMap[internalArgMeta.type];
|
|
102
|
+
decorate(internalArgMeta.option ?? {})(Rsv.prototype, resolveFieldMeta.key, internalArgMeta.idx);
|
|
103
|
+
}
|
|
104
|
+
Nest.ResolveField(getNestReturn(resolveFieldMeta.returns))(
|
|
105
|
+
Rsv.prototype,
|
|
106
|
+
resolveFieldMeta.key,
|
|
107
|
+
Object.getOwnPropertyDescriptor(Rsv.prototype, resolveFieldMeta.key) ?? {}
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (sigMeta.returns)
|
|
111
|
+
Nest.Resolver(getNestReturn(sigMeta.returns))(Rsv);
|
|
112
|
+
else
|
|
113
|
+
Nest.Resolver()(Rsv);
|
|
114
|
+
return Rsv;
|
|
115
|
+
};
|
|
116
|
+
export {
|
|
117
|
+
resolverOf
|
|
118
|
+
};
|
package/src/schema.mjs
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { arraiedModel, dayjs, Enum, Float, ID, Int, JSON } from "@akanjs/base";
|
|
2
|
+
import { isDayjs } from "@akanjs/common";
|
|
3
|
+
import { getClassMeta, getFieldMetas, getFullModelRef, getInputModelRef } from "@akanjs/constant";
|
|
4
|
+
import { getDefaultSchemaOptions, ObjectId } from "@akanjs/document";
|
|
5
|
+
import { makeDefault } from "@akanjs/signal";
|
|
6
|
+
import { Schema, Types } from "mongoose";
|
|
7
|
+
import { applyNestField } from ".";
|
|
8
|
+
class ScalarSchemaStorage {
|
|
9
|
+
}
|
|
10
|
+
class SchemaStorage {
|
|
11
|
+
}
|
|
12
|
+
const scalarMongoTypeMap = /* @__PURE__ */ new Map([
|
|
13
|
+
[ID, ObjectId],
|
|
14
|
+
[Int, Number],
|
|
15
|
+
[Float, Number],
|
|
16
|
+
[JSON, Schema.Types.Mixed],
|
|
17
|
+
[Map, Map],
|
|
18
|
+
[String, String],
|
|
19
|
+
[Boolean, Boolean],
|
|
20
|
+
[Date, Date]
|
|
21
|
+
]);
|
|
22
|
+
const applyMongoProp = (schemaProps, fieldMeta) => {
|
|
23
|
+
if (["id", "createdAt", "updatedAt"].includes(fieldMeta.key) || fieldMeta.fieldType === "resolve")
|
|
24
|
+
return;
|
|
25
|
+
const type = fieldMeta.isClass ? fieldMeta.isScalar ? createSchema(fieldMeta.modelRef) : ObjectId : scalarMongoTypeMap.get(fieldMeta.modelRef) ?? fieldMeta.modelRef;
|
|
26
|
+
let prop = {};
|
|
27
|
+
if (fieldMeta.optArrDepth) {
|
|
28
|
+
prop.type = type;
|
|
29
|
+
prop.required = true;
|
|
30
|
+
if (fieldMeta.isClass && !fieldMeta.refPath)
|
|
31
|
+
prop.ref = getClassMeta(fieldMeta.modelRef).refName;
|
|
32
|
+
if (fieldMeta.refPath)
|
|
33
|
+
prop.refPath = fieldMeta.refPath;
|
|
34
|
+
if (typeof fieldMeta.min === "number")
|
|
35
|
+
prop.min = fieldMeta.min;
|
|
36
|
+
if (typeof fieldMeta.max === "number")
|
|
37
|
+
prop.max = fieldMeta.max;
|
|
38
|
+
if (fieldMeta.enum)
|
|
39
|
+
prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
|
|
40
|
+
if (typeof fieldMeta.minlength === "number")
|
|
41
|
+
prop.minlength = fieldMeta.minlength;
|
|
42
|
+
if (typeof fieldMeta.maxlength === "number")
|
|
43
|
+
prop.maxlength = fieldMeta.maxlength;
|
|
44
|
+
if (fieldMeta.validate) {
|
|
45
|
+
prop.validate = function(value) {
|
|
46
|
+
return fieldMeta.validate?.(fieldMeta.name === "Date" && !!value ? dayjs() : value, this) ?? true;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
prop = { type: arraiedModel(prop, fieldMeta.optArrDepth), default: [], required: true };
|
|
50
|
+
if (fieldMeta.modelRef.prototype === Date.prototype) {
|
|
51
|
+
prop.get = (dates) => dates.map((date) => dayjs(date));
|
|
52
|
+
prop.set = (days) => days.map((day) => day.toDate());
|
|
53
|
+
}
|
|
54
|
+
if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === ID.prototype) {
|
|
55
|
+
prop.get = (ids) => ids.map((id) => id.toString());
|
|
56
|
+
prop.set = (ids) => ids.map((id) => new Types.ObjectId(id));
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
prop.type = arraiedModel(type, fieldMeta.arrDepth);
|
|
60
|
+
prop.required = !fieldMeta.nullable;
|
|
61
|
+
if (fieldMeta.isMap) {
|
|
62
|
+
prop.of = scalarMongoTypeMap.get(fieldMeta.of) ?? createSchema(fieldMeta.of);
|
|
63
|
+
if (!fieldMeta.default)
|
|
64
|
+
prop.default = /* @__PURE__ */ new Map();
|
|
65
|
+
}
|
|
66
|
+
if (fieldMeta.default !== null) {
|
|
67
|
+
if (typeof fieldMeta.default === "function")
|
|
68
|
+
prop.default = function() {
|
|
69
|
+
const def = fieldMeta.default(this);
|
|
70
|
+
return isDayjs(def) ? def.toDate() : def;
|
|
71
|
+
};
|
|
72
|
+
else
|
|
73
|
+
prop.default = isDayjs(fieldMeta.default) ? fieldMeta.default.toDate() : fieldMeta.default instanceof Enum ? [...fieldMeta.default.values] : fieldMeta.default;
|
|
74
|
+
}
|
|
75
|
+
if (typeof fieldMeta.immutable !== "undefined")
|
|
76
|
+
prop.immutable = fieldMeta.immutable;
|
|
77
|
+
if (fieldMeta.isClass && !fieldMeta.refPath)
|
|
78
|
+
prop.ref = getClassMeta(fieldMeta.modelRef).refName;
|
|
79
|
+
if (fieldMeta.refPath)
|
|
80
|
+
prop.refPath = fieldMeta.refPath;
|
|
81
|
+
if (typeof fieldMeta.min === "number")
|
|
82
|
+
prop.min = fieldMeta.min;
|
|
83
|
+
if (typeof fieldMeta.max === "number")
|
|
84
|
+
prop.max = fieldMeta.max;
|
|
85
|
+
if (fieldMeta.enum)
|
|
86
|
+
prop.enum = [...fieldMeta.enum.values, ...fieldMeta.nullable ? [null] : []];
|
|
87
|
+
if (typeof fieldMeta.select === "boolean")
|
|
88
|
+
prop.select = fieldMeta.select;
|
|
89
|
+
if (typeof fieldMeta.minlength === "number")
|
|
90
|
+
prop.minlength = fieldMeta.minlength;
|
|
91
|
+
if (typeof fieldMeta.maxlength === "number")
|
|
92
|
+
prop.maxlength = fieldMeta.maxlength;
|
|
93
|
+
if (fieldMeta.nullable) {
|
|
94
|
+
prop.get = (v) => v === void 0 ? void 0 : v;
|
|
95
|
+
prop.set = (v) => v === null ? void 0 : v;
|
|
96
|
+
}
|
|
97
|
+
if (fieldMeta.modelRef.prototype === Date.prototype) {
|
|
98
|
+
prop.get = (date) => date ? dayjs(date) : void 0;
|
|
99
|
+
prop.set = (day) => day ? dayjs(day).toDate() : void 0;
|
|
100
|
+
}
|
|
101
|
+
if (fieldMeta.isClass && !fieldMeta.isScalar || fieldMeta.modelRef.prototype === ID.prototype) {
|
|
102
|
+
prop.get = (id) => id ? id.toString() : void 0;
|
|
103
|
+
prop.set = (id) => id ? new Types.ObjectId(id) : void 0;
|
|
104
|
+
}
|
|
105
|
+
if (fieldMeta.isClass && fieldMeta.isScalar && fieldMeta.default === null && !fieldMeta.nullable) {
|
|
106
|
+
prop.default = makeDefault(fieldMeta.modelRef);
|
|
107
|
+
}
|
|
108
|
+
if (fieldMeta.validate) {
|
|
109
|
+
prop.validate = function(value) {
|
|
110
|
+
return fieldMeta.validate?.(fieldMeta.name === "Date" && !!value ? dayjs() : value, this) ?? true;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
schemaProps[fieldMeta.key] = prop;
|
|
115
|
+
};
|
|
116
|
+
const createSchema = (modelRef) => {
|
|
117
|
+
const classMeta = getClassMeta(modelRef);
|
|
118
|
+
const schemaMeta = Reflect.getMetadata(classMeta.refName, ScalarSchemaStorage.prototype);
|
|
119
|
+
if (schemaMeta)
|
|
120
|
+
return schemaMeta;
|
|
121
|
+
const fieldMetas = getFieldMetas(modelRef);
|
|
122
|
+
const schemaProps = {};
|
|
123
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
124
|
+
applyMongoProp(schemaProps, fieldMeta);
|
|
125
|
+
});
|
|
126
|
+
const schema = new Schema(schemaProps);
|
|
127
|
+
Reflect.defineMetadata(classMeta.refName, schema, ScalarSchemaStorage.prototype);
|
|
128
|
+
return schema;
|
|
129
|
+
};
|
|
130
|
+
const schemaOf = (modelRef, docRef, middleware) => {
|
|
131
|
+
const classMeta = getClassMeta(docRef);
|
|
132
|
+
const schemaMeta = Reflect.getMetadata(classMeta.refName, SchemaStorage.prototype);
|
|
133
|
+
if (schemaMeta)
|
|
134
|
+
return schemaMeta;
|
|
135
|
+
const fieldMetas = getFieldMetas(docRef);
|
|
136
|
+
const schemaProps = {
|
|
137
|
+
createdAt: {
|
|
138
|
+
type: Date,
|
|
139
|
+
get: (date) => date ? dayjs(date) : date,
|
|
140
|
+
set: (day) => day ? dayjs(day).toDate() : day
|
|
141
|
+
},
|
|
142
|
+
updatedAt: {
|
|
143
|
+
type: Date,
|
|
144
|
+
get: (date) => date ? dayjs(date) : date,
|
|
145
|
+
set: (day) => day ? dayjs(day).toDate() : day
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
149
|
+
applyMongoProp(schemaProps, fieldMeta);
|
|
150
|
+
});
|
|
151
|
+
const schema = new Schema(schemaProps, getDefaultSchemaOptions());
|
|
152
|
+
schema.methods.refresh = async function() {
|
|
153
|
+
Object.assign(this, await this.constructor.findById(this._id));
|
|
154
|
+
return this;
|
|
155
|
+
};
|
|
156
|
+
Object.getOwnPropertyNames(docRef.prototype).forEach((name) => {
|
|
157
|
+
if (name === "constructor")
|
|
158
|
+
return;
|
|
159
|
+
schema.methods[name] = Object.getOwnPropertyDescriptor(docRef.prototype, name)?.value;
|
|
160
|
+
});
|
|
161
|
+
schema.pre("save", async function(next) {
|
|
162
|
+
const model = this.constructor;
|
|
163
|
+
if (this.isNew)
|
|
164
|
+
model.addSummary(["total", this.status]);
|
|
165
|
+
else if (!!this.removedAt && this.isModified("removedAt"))
|
|
166
|
+
model.subSummary(["total", this.status]);
|
|
167
|
+
next();
|
|
168
|
+
});
|
|
169
|
+
const onSchema = Object.getOwnPropertyDescriptor(middleware.prototype, "onSchema")?.value;
|
|
170
|
+
onSchema?.(schema);
|
|
171
|
+
schema.index({ removedAt: -1 });
|
|
172
|
+
Reflect.defineMetadata(classMeta.refName, schema, SchemaStorage.prototype);
|
|
173
|
+
return schema;
|
|
174
|
+
};
|
|
175
|
+
const addSchema = (modelRef, docRef, inputRef, middleware) => {
|
|
176
|
+
const originDocClassMeta = getClassMeta(docRef);
|
|
177
|
+
const originInputClassMeta = getClassMeta(inputRef);
|
|
178
|
+
const originDoc = getFullModelRef(originDocClassMeta.refName);
|
|
179
|
+
const originInput = getInputModelRef(originInputClassMeta.refName);
|
|
180
|
+
const classMeta = getClassMeta(docRef);
|
|
181
|
+
const modelSchema = Reflect.getMetadata(classMeta.refName, SchemaStorage.prototype);
|
|
182
|
+
if (!modelSchema)
|
|
183
|
+
throw new Error(`Schema of ${classMeta.refName} not found`);
|
|
184
|
+
const fieldMetas = getFieldMetas(docRef);
|
|
185
|
+
const schemaProps = {
|
|
186
|
+
createdAt: {
|
|
187
|
+
type: Date,
|
|
188
|
+
get: (date) => date ? dayjs(date) : date,
|
|
189
|
+
set: (day) => day ? dayjs(day).toDate() : day
|
|
190
|
+
},
|
|
191
|
+
updatedAt: {
|
|
192
|
+
type: Date,
|
|
193
|
+
get: (date) => date ? dayjs(date) : date,
|
|
194
|
+
set: (day) => day ? dayjs(day).toDate() : day
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
198
|
+
applyMongoProp(schemaProps, fieldMeta);
|
|
199
|
+
applyNestField(originDoc, fieldMeta);
|
|
200
|
+
});
|
|
201
|
+
const inputFieldMetas = getFieldMetas(inputRef);
|
|
202
|
+
inputFieldMetas.forEach((fieldMeta) => {
|
|
203
|
+
applyNestField(originInput, fieldMeta, "input");
|
|
204
|
+
});
|
|
205
|
+
const schema = new Schema(schemaProps, getDefaultSchemaOptions());
|
|
206
|
+
modelSchema.add(schema);
|
|
207
|
+
Object.getOwnPropertyNames(docRef.prototype).forEach((name) => {
|
|
208
|
+
if (name === "constructor")
|
|
209
|
+
return;
|
|
210
|
+
modelSchema.methods[name] = Object.getOwnPropertyDescriptor(docRef.prototype, name)?.value;
|
|
211
|
+
});
|
|
212
|
+
const onSchema = Object.getOwnPropertyDescriptor(middleware.prototype, "onSchema")?.value;
|
|
213
|
+
onSchema?.(modelSchema);
|
|
214
|
+
return modelSchema;
|
|
215
|
+
};
|
|
216
|
+
export {
|
|
217
|
+
addSchema,
|
|
218
|
+
schemaOf
|
|
219
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
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 { Logger, lowerlize } from "@akanjs/common";
|
|
14
|
+
import {
|
|
15
|
+
getCnstMeta,
|
|
16
|
+
getFieldMetaMap,
|
|
17
|
+
getFieldMetas,
|
|
18
|
+
getFilterSortMap,
|
|
19
|
+
getFullModelRef
|
|
20
|
+
} from "@akanjs/constant";
|
|
21
|
+
import { getAllDatabaseModelNames } from "@akanjs/document";
|
|
22
|
+
import { Global, Inject, Injectable, Module } from "@nestjs/common";
|
|
23
|
+
import { InjectConnection } from "@nestjs/mongoose";
|
|
24
|
+
const hasTextField = (modelRef) => {
|
|
25
|
+
const fieldMetas = getFieldMetas(modelRef);
|
|
26
|
+
return fieldMetas.some(
|
|
27
|
+
(fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
const getTextFieldKeys = (modelRef) => {
|
|
31
|
+
const allSearchFields = [];
|
|
32
|
+
const allFilterFields = [];
|
|
33
|
+
const fieldMetaMap = getFieldMetaMap(modelRef);
|
|
34
|
+
const fieldMetas = [...fieldMetaMap.values()];
|
|
35
|
+
const stringTextFields = fieldMetas.filter((fieldMeta) => !!fieldMeta.text).map((fieldMeta) => {
|
|
36
|
+
if (fieldMeta.text === "filter")
|
|
37
|
+
allFilterFields.push(fieldMeta.key);
|
|
38
|
+
else if (fieldMeta.text === "search")
|
|
39
|
+
allSearchFields.push(fieldMeta.key);
|
|
40
|
+
return fieldMeta.key;
|
|
41
|
+
});
|
|
42
|
+
const scalarTextFields = fieldMetas.filter(
|
|
43
|
+
(fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)
|
|
44
|
+
).map((fieldMeta) => fieldMeta.key);
|
|
45
|
+
const deepFields = scalarTextFields.map((key) => {
|
|
46
|
+
const fieldMeta = fieldMetaMap.get(key);
|
|
47
|
+
if (!fieldMeta)
|
|
48
|
+
throw new Error(`No fieldMeta for ${key}`);
|
|
49
|
+
const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(
|
|
50
|
+
fieldMeta.modelRef
|
|
51
|
+
);
|
|
52
|
+
allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
|
|
53
|
+
allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
|
|
54
|
+
return [
|
|
55
|
+
...stringTextFields2.map((field) => `${key}.${field}`),
|
|
56
|
+
...allTextFields.map((field) => `${key}.${field}`)
|
|
57
|
+
];
|
|
58
|
+
}).flat();
|
|
59
|
+
return {
|
|
60
|
+
stringTextFields,
|
|
61
|
+
scalarTextFields,
|
|
62
|
+
allTextFields: [...stringTextFields, ...deepFields],
|
|
63
|
+
allSearchFields,
|
|
64
|
+
allFilterFields
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
const makeTextFilter = (modelRef) => {
|
|
68
|
+
const fieldMetaMap = getFieldMetaMap(modelRef);
|
|
69
|
+
const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
|
|
70
|
+
const filterData = (data, assignObj = {}) => {
|
|
71
|
+
if (Array.isArray(data))
|
|
72
|
+
return data.map((d) => filterData(d));
|
|
73
|
+
return Object.assign(
|
|
74
|
+
Object.fromEntries([
|
|
75
|
+
...stringTextFields.map((key) => [key, data[key]]),
|
|
76
|
+
...scalarTextFields.map((key) => {
|
|
77
|
+
const fieldMeta = fieldMetaMap.get(key);
|
|
78
|
+
if (!fieldMeta)
|
|
79
|
+
throw new Error(`No fieldMeta for ${key}`);
|
|
80
|
+
const filterFunc = makeTextFilter(fieldMeta.modelRef);
|
|
81
|
+
return [key, filterFunc(data[key])];
|
|
82
|
+
})
|
|
83
|
+
]),
|
|
84
|
+
assignObj
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
return filterData;
|
|
88
|
+
};
|
|
89
|
+
const getSortableAttributes = (refName) => {
|
|
90
|
+
const cnst = getCnstMeta(refName);
|
|
91
|
+
const sortMap = getFilterSortMap(cnst.Filter);
|
|
92
|
+
const sortFields = Object.values(sortMap).filter((val) => typeof val === "object").map((sort) => Object.keys(sort)).flat();
|
|
93
|
+
return [...new Set(sortFields)];
|
|
94
|
+
};
|
|
95
|
+
let SearchDaemon = class {
|
|
96
|
+
constructor(connection, meili) {
|
|
97
|
+
this.connection = connection;
|
|
98
|
+
this.meili = meili;
|
|
99
|
+
}
|
|
100
|
+
logger = new Logger("SearchDaemon");
|
|
101
|
+
async onModuleInit() {
|
|
102
|
+
const databaseModelNames = getAllDatabaseModelNames();
|
|
103
|
+
const indexes = (await this.meili.getIndexes({ limit: 1e3 })).results;
|
|
104
|
+
const indexMap = new Map(indexes.map((index) => [index.uid, index]));
|
|
105
|
+
const indexCreationNames = [];
|
|
106
|
+
const indexUpdateNames = [];
|
|
107
|
+
for (const modelName of databaseModelNames) {
|
|
108
|
+
const indexName = lowerlize(modelName);
|
|
109
|
+
const modelRef = getFullModelRef(modelName);
|
|
110
|
+
if (!hasTextField(modelRef))
|
|
111
|
+
continue;
|
|
112
|
+
const index = indexMap.get(indexName);
|
|
113
|
+
if (!index)
|
|
114
|
+
indexCreationNames.push(indexName);
|
|
115
|
+
else if (index.primaryKey !== "id")
|
|
116
|
+
indexUpdateNames.push(indexName);
|
|
117
|
+
}
|
|
118
|
+
for (const indexName of indexCreationNames)
|
|
119
|
+
await this.meili.createIndex(indexName, { primaryKey: "id" });
|
|
120
|
+
for (const indexName of indexUpdateNames)
|
|
121
|
+
await this.meili.updateIndex(indexName, { primaryKey: "id" });
|
|
122
|
+
for (const modelName of databaseModelNames) {
|
|
123
|
+
const indexName = lowerlize(modelName);
|
|
124
|
+
const model = this.connection.models[modelName];
|
|
125
|
+
const modelRef = getFullModelRef(modelName);
|
|
126
|
+
if (!hasTextField(modelRef))
|
|
127
|
+
continue;
|
|
128
|
+
const searchIndex = this.meili.index(indexName);
|
|
129
|
+
const { stringTextFields, scalarTextFields, allSearchFields, allFilterFields } = getTextFieldKeys(modelRef);
|
|
130
|
+
const settings = await searchIndex.getSettings();
|
|
131
|
+
const allSearchFieldSet = new Set(allSearchFields);
|
|
132
|
+
const allFilterFieldSet = new Set(allFilterFields);
|
|
133
|
+
const searchFieldSet = new Set(settings.searchableAttributes);
|
|
134
|
+
const filterFieldSet = new Set(settings.filterableAttributes);
|
|
135
|
+
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));
|
|
136
|
+
if (needUpdateSetting) {
|
|
137
|
+
this.logger.info(`update index settings (${modelName})`);
|
|
138
|
+
await searchIndex.updateSettings({
|
|
139
|
+
searchableAttributes: allSearchFields,
|
|
140
|
+
filterableAttributes: allFilterFields,
|
|
141
|
+
sortableAttributes: getSortableAttributes(indexName)
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const stringTextFieldSet = new Set(stringTextFields);
|
|
145
|
+
const scalarTextFieldSet = new Set(scalarTextFields);
|
|
146
|
+
const filterText = makeTextFilter(modelRef);
|
|
147
|
+
model.watch().on("change", async (data) => {
|
|
148
|
+
try {
|
|
149
|
+
const id = data.documentKey._id.toString();
|
|
150
|
+
if (data.operationType === "delete") {
|
|
151
|
+
this.logger.trace(`delete text doc (${modelName}): ${id}`);
|
|
152
|
+
return await searchIndex.deleteDocument(id);
|
|
153
|
+
} else if (data.operationType === "insert") {
|
|
154
|
+
this.logger.trace(`insert text doc (${modelName}): ${data.documentKey._id}`);
|
|
155
|
+
if (!data.fullDocument)
|
|
156
|
+
throw new Error("No fullDocument");
|
|
157
|
+
const textFilteredData = filterText(data.fullDocument);
|
|
158
|
+
return await searchIndex.addDocuments([textFilteredData]);
|
|
159
|
+
} else if (data.operationType === "update") {
|
|
160
|
+
const updatedFields = data.updateDescription?.updatedFields ?? {};
|
|
161
|
+
const isRemoved = !!updatedFields.removedAt;
|
|
162
|
+
if (isRemoved) {
|
|
163
|
+
this.logger.trace(`remove text doc (${modelName}): ${id}`);
|
|
164
|
+
return await searchIndex.deleteDocument(id);
|
|
165
|
+
}
|
|
166
|
+
this.logger.trace(`update text doc (${modelName}): ${data.documentKey._id}`);
|
|
167
|
+
const updatedFieldKeys = Object.keys(updatedFields);
|
|
168
|
+
const removedFieldKeys = data.updateDescription?.removedFields ?? [];
|
|
169
|
+
const isScalarTextFieldUpdated = [...updatedFieldKeys, ...removedFieldKeys].map((key) => key.split(".")[0]).some((key) => scalarTextFieldSet.has(key));
|
|
170
|
+
if (isScalarTextFieldUpdated) {
|
|
171
|
+
const doc = await model.findById(data.documentKey._id);
|
|
172
|
+
if (!doc)
|
|
173
|
+
this.logger.error(`No doc for ${data.documentKey._id}`);
|
|
174
|
+
const textFilteredData = filterText(doc, { id });
|
|
175
|
+
return await searchIndex.updateDocuments([textFilteredData]);
|
|
176
|
+
} else {
|
|
177
|
+
const updateKeys = updatedFieldKeys.filter((key) => stringTextFieldSet.has(key));
|
|
178
|
+
const removeKeys = removedFieldKeys.filter((key) => stringTextFieldSet.has(key));
|
|
179
|
+
if (!updateKeys.length && !removeKeys.length)
|
|
180
|
+
return;
|
|
181
|
+
const textFilteredData = Object.fromEntries([
|
|
182
|
+
["id", id],
|
|
183
|
+
...updateKeys.map((key) => [key, updatedFields[key]]),
|
|
184
|
+
...removeKeys.map((key) => [key, null])
|
|
185
|
+
]);
|
|
186
|
+
return await searchIndex.updateDocuments([textFilteredData]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
} catch (e) {
|
|
190
|
+
this.logger.error(e);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
SearchDaemon = __decorateClass([
|
|
197
|
+
Injectable(),
|
|
198
|
+
__decorateParam(0, InjectConnection()),
|
|
199
|
+
__decorateParam(1, Inject("MEILI_CLIENT"))
|
|
200
|
+
], SearchDaemon);
|
|
201
|
+
let SearchDaemonModule = class {
|
|
202
|
+
};
|
|
203
|
+
SearchDaemonModule = __decorateClass([
|
|
204
|
+
Global(),
|
|
205
|
+
Module({
|
|
206
|
+
providers: [SearchDaemon]
|
|
207
|
+
})
|
|
208
|
+
], SearchDaemonModule);
|
|
209
|
+
export {
|
|
210
|
+
SearchDaemonModule,
|
|
211
|
+
makeTextFilter
|
|
212
|
+
};
|
package/src/types.mjs
ADDED
|
File without changes
|