@akanjs/server 0.0.4
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/README.md +11 -0
- package/index.d.ts +1 -0
- package/index.js +21 -0
- package/package.json +68 -0
- package/src/boot.d.ts +18 -0
- package/src/boot.js +200 -0
- package/src/controller.d.ts +6 -0
- package/src/controller.js +99 -0
- package/src/dataLoader.d.ts +26 -0
- package/src/dataLoader.js +152 -0
- package/src/database.d.ts +114 -0
- package/src/database.js +312 -0
- package/src/dbDecorators.d.ts +68 -0
- package/src/dbDecorators.js +130 -0
- package/src/gql.d.ts +14 -0
- package/src/gql.js +152 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +47 -0
- package/src/module.d.ts +72 -0
- package/src/module.js +217 -0
- package/src/processor.d.ts +8 -0
- package/src/processor.js +89 -0
- package/src/resolver.d.ts +6 -0
- package/src/resolver.js +142 -0
- package/src/schema.d.ts +33 -0
- package/src/schema.js +343 -0
- package/src/searchDaemon.d.ts +7 -0
- package/src/searchDaemon.js +228 -0
- package/src/serviceDecorators.d.ts +178 -0
- package/src/serviceDecorators.js +491 -0
- package/src/types.d.ts +14 -0
- package/src/types.js +15 -0
- package/src/websocket.d.ts +17 -0
- package/src/websocket.js +143 -0
|
@@ -0,0 +1,491 @@
|
|
|
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 serviceDecorators_exports = {};
|
|
28
|
+
__export(serviceDecorators_exports, {
|
|
29
|
+
Cache: () => Cache,
|
|
30
|
+
Cron: () => Cron,
|
|
31
|
+
Db: () => Db,
|
|
32
|
+
DbService: () => DbService,
|
|
33
|
+
ExtendedSettingService: () => ExtendedSettingService,
|
|
34
|
+
ExtendedSummaryService: () => ExtendedSummaryService,
|
|
35
|
+
ExtendedUserService: () => ExtendedUserService,
|
|
36
|
+
Interval: () => Interval,
|
|
37
|
+
LogService: () => LogService,
|
|
38
|
+
MixSrvs: () => MixSrvs,
|
|
39
|
+
Queue: () => Queue,
|
|
40
|
+
Service: () => Service,
|
|
41
|
+
ServiceStorage: () => ServiceStorage,
|
|
42
|
+
Srv: () => Srv,
|
|
43
|
+
Transaction: () => Transaction,
|
|
44
|
+
Try: () => Try,
|
|
45
|
+
Use: () => Use,
|
|
46
|
+
Websocket: () => Websocket,
|
|
47
|
+
serviceOf: () => serviceOf
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(serviceDecorators_exports);
|
|
50
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
51
|
+
var import_common = require("@akanjs/common");
|
|
52
|
+
var import_constant = require("@akanjs/constant");
|
|
53
|
+
var import_common2 = require("@nestjs/common");
|
|
54
|
+
var import_mongoose = require("@nestjs/mongoose");
|
|
55
|
+
var import_schedule = require("@nestjs/schedule");
|
|
56
|
+
class ServiceStorage {
|
|
57
|
+
}
|
|
58
|
+
const getServiceRefs = (refName) => {
|
|
59
|
+
return Reflect.getMetadata(refName, ServiceStorage.prototype) ?? [];
|
|
60
|
+
};
|
|
61
|
+
const setServiceRefs = (refName, services) => {
|
|
62
|
+
Reflect.defineMetadata(refName, services, ServiceStorage.prototype);
|
|
63
|
+
};
|
|
64
|
+
const isServiceDefined = (srvRef) => {
|
|
65
|
+
return Reflect.getMetadata("service", srvRef.prototype) ?? false;
|
|
66
|
+
};
|
|
67
|
+
const setServiceDefined = (srvRef) => {
|
|
68
|
+
Reflect.defineMetadata("service", true, srvRef.prototype);
|
|
69
|
+
};
|
|
70
|
+
const getServiceInjectMetaMapOnPrototype = (prototype) => {
|
|
71
|
+
return Reflect.getMetadata("inject", prototype) ?? /* @__PURE__ */ new Map();
|
|
72
|
+
};
|
|
73
|
+
const setServiceInjectMetaMapOnPrototype = (prototype, injectMetaMap) => {
|
|
74
|
+
Reflect.defineMetadata("inject", injectMetaMap, prototype);
|
|
75
|
+
};
|
|
76
|
+
function Service(name) {
|
|
77
|
+
return function(target) {
|
|
78
|
+
const services = getServiceRefs(name);
|
|
79
|
+
setServiceRefs(name, [...services, target]);
|
|
80
|
+
Reflect.defineMetadata(name, [...services, target], ServiceStorage.prototype);
|
|
81
|
+
return target;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function Srv(name) {
|
|
85
|
+
return function(prototype, key) {
|
|
86
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
87
|
+
metadataMap.set(key, { type: "Srv", key, name: name ?? (0, import_common.capitalize)(key) });
|
|
88
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function Use(name) {
|
|
92
|
+
return function(prototype, key) {
|
|
93
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
94
|
+
metadataMap.set(key, { type: "Use", key, name: name ?? (0, import_common.capitalize)(key) });
|
|
95
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function Queue(name) {
|
|
99
|
+
return function(prototype, key) {
|
|
100
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
101
|
+
metadataMap.set(key, { type: "Queue", key, name: name ?? (0, import_common.capitalize)(key) });
|
|
102
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function Websocket(name) {
|
|
106
|
+
return function(prototype, key) {
|
|
107
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
108
|
+
metadataMap.set(key, { type: "Websocket", key, name: name ?? (0, import_common.capitalize)(key) });
|
|
109
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function Db(name) {
|
|
113
|
+
return function(prototype, key) {
|
|
114
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
115
|
+
metadataMap.set(key, { type: "Db", key, name });
|
|
116
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const serviceOf = (srvRef) => {
|
|
120
|
+
if (isServiceDefined(srvRef))
|
|
121
|
+
return srvRef;
|
|
122
|
+
const injectMetaMap = getServiceInjectMetaMapOnPrototype(srvRef.prototype);
|
|
123
|
+
for (const injectMeta of [...injectMetaMap.values()]) {
|
|
124
|
+
if (injectMeta.type === "Db")
|
|
125
|
+
(0, import_mongoose.InjectModel)(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
126
|
+
else if (injectMeta.type === "Use")
|
|
127
|
+
(0, import_common2.Inject)(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
128
|
+
else if (injectMeta.type === "Srv") {
|
|
129
|
+
const services = getServiceRefs(injectMeta.name);
|
|
130
|
+
if (!services.length)
|
|
131
|
+
throw new Error(`Service ${injectMeta.name} not found`);
|
|
132
|
+
(0, import_common2.Inject)(services.at(-1))(srvRef.prototype, injectMeta.key);
|
|
133
|
+
} else if (injectMeta.type === "Queue")
|
|
134
|
+
(0, import_common2.Inject)(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
135
|
+
else
|
|
136
|
+
(0, import_common2.Inject)(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
137
|
+
}
|
|
138
|
+
(0, import_mongoose.InjectConnection)()(srvRef.prototype, "connection");
|
|
139
|
+
(0, import_common2.Injectable)()(srvRef);
|
|
140
|
+
setServiceDefined(srvRef);
|
|
141
|
+
return srvRef;
|
|
142
|
+
};
|
|
143
|
+
function MixSrvs(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20) {
|
|
144
|
+
class Mix extends t1 {
|
|
145
|
+
}
|
|
146
|
+
const injectMetadataMap = new Map(
|
|
147
|
+
[t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20].filter((t) => !!t).reduce((acc, srvRef) => {
|
|
148
|
+
const injectMetadataMap2 = getServiceInjectMetaMapOnPrototype(srvRef);
|
|
149
|
+
(0, import_common.applyMixins)(Mix, [srvRef]);
|
|
150
|
+
return [...acc, ...injectMetadataMap2];
|
|
151
|
+
}, [])
|
|
152
|
+
);
|
|
153
|
+
Reflect.defineMetadata("service", false, Mix.prototype);
|
|
154
|
+
Reflect.defineMetadata("inject", injectMetadataMap, Mix.prototype);
|
|
155
|
+
return Mix;
|
|
156
|
+
}
|
|
157
|
+
const LogService = (name) => {
|
|
158
|
+
class LogService2 {
|
|
159
|
+
logger = new import_common.Logger(name);
|
|
160
|
+
}
|
|
161
|
+
return LogService2;
|
|
162
|
+
};
|
|
163
|
+
const DbService = (database, sigRef) => {
|
|
164
|
+
const [modelName, className] = [database.refName, (0, import_common.capitalize)(database.refName)];
|
|
165
|
+
class DbService2 {
|
|
166
|
+
logger = new import_common.Logger(`${modelName}Service`);
|
|
167
|
+
__databaseModel;
|
|
168
|
+
async __list(query, queryOption) {
|
|
169
|
+
return await this.__databaseModel.__list(query, queryOption);
|
|
170
|
+
}
|
|
171
|
+
async __listIds(query, queryOption) {
|
|
172
|
+
return await this.__databaseModel.__listIds(query, queryOption);
|
|
173
|
+
}
|
|
174
|
+
async __find(query, queryOption) {
|
|
175
|
+
return await this.__databaseModel.__find(query, queryOption);
|
|
176
|
+
}
|
|
177
|
+
async __findId(query, queryOption) {
|
|
178
|
+
return await this.__databaseModel.__findId(query, queryOption);
|
|
179
|
+
}
|
|
180
|
+
async __pick(query, queryOption) {
|
|
181
|
+
return await this.__databaseModel.__pick(query, queryOption);
|
|
182
|
+
}
|
|
183
|
+
async __pickId(query, queryOption) {
|
|
184
|
+
return await this.__databaseModel.__pickId(query, queryOption);
|
|
185
|
+
}
|
|
186
|
+
async __exists(query) {
|
|
187
|
+
return await this.__databaseModel.__exists(query);
|
|
188
|
+
}
|
|
189
|
+
async __count(query) {
|
|
190
|
+
return await this.__databaseModel.__count(query);
|
|
191
|
+
}
|
|
192
|
+
async __insight(query) {
|
|
193
|
+
return await this.__databaseModel.__insight(query);
|
|
194
|
+
}
|
|
195
|
+
async __search(searchText, queryOption) {
|
|
196
|
+
return await this.__databaseModel[`search${className}`](searchText, queryOption);
|
|
197
|
+
}
|
|
198
|
+
async __searchDocs(searchText, queryOption) {
|
|
199
|
+
return await this.__databaseModel[`searchDocs${className}`](searchText, queryOption);
|
|
200
|
+
}
|
|
201
|
+
async __searchCount(searchText) {
|
|
202
|
+
return await this.__databaseModel[`searchCount${className}`](searchText);
|
|
203
|
+
}
|
|
204
|
+
async _preCreate(data) {
|
|
205
|
+
return data;
|
|
206
|
+
}
|
|
207
|
+
async _postCreate(doc) {
|
|
208
|
+
return doc;
|
|
209
|
+
}
|
|
210
|
+
async _preUpdate(id, data) {
|
|
211
|
+
return data;
|
|
212
|
+
}
|
|
213
|
+
async _postUpdate(doc) {
|
|
214
|
+
return doc;
|
|
215
|
+
}
|
|
216
|
+
async _preRemove(id) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
async _postRemove(doc) {
|
|
220
|
+
return doc;
|
|
221
|
+
}
|
|
222
|
+
async [`get${className}`](id) {
|
|
223
|
+
return await this.__databaseModel[`get${className}`](id);
|
|
224
|
+
}
|
|
225
|
+
async [`load${className}`](id) {
|
|
226
|
+
return await this.__databaseModel[`load${className}`](id);
|
|
227
|
+
}
|
|
228
|
+
async [`load${className}Many`](ids) {
|
|
229
|
+
return await this.__databaseModel[`load${className}Many`](ids);
|
|
230
|
+
}
|
|
231
|
+
async [`create${className}`](data) {
|
|
232
|
+
const input = await this._preCreate(data);
|
|
233
|
+
const doc = await this.__databaseModel[`create${className}`](input);
|
|
234
|
+
return await this._postCreate(doc);
|
|
235
|
+
}
|
|
236
|
+
async [`update${className}`](id, data) {
|
|
237
|
+
const input = await this._preUpdate(id, data);
|
|
238
|
+
const doc = await this.__databaseModel[`update${className}`](id, input);
|
|
239
|
+
return await this._postUpdate(doc);
|
|
240
|
+
}
|
|
241
|
+
async [`remove${className}`](id) {
|
|
242
|
+
await this._preRemove(id);
|
|
243
|
+
const doc = await this.__databaseModel[`remove${className}`](id);
|
|
244
|
+
return await this._postRemove(doc);
|
|
245
|
+
}
|
|
246
|
+
async [`search${className}`](query, queryOption) {
|
|
247
|
+
return await this.__databaseModel[`search${className}`](query, queryOption);
|
|
248
|
+
}
|
|
249
|
+
async [`searchDocs${className}`](query, queryOption) {
|
|
250
|
+
return await this.__databaseModel[`searchDocs${className}`](query, queryOption);
|
|
251
|
+
}
|
|
252
|
+
async [`searchCount${className}`](query) {
|
|
253
|
+
return await this.__databaseModel[`searchCount${className}`](query);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
__decorateClass([
|
|
257
|
+
Use(`${modelName}Model`)
|
|
258
|
+
], DbService2.prototype, "__databaseModel", 2);
|
|
259
|
+
const getQueryDataFromKey = (queryKey, args) => {
|
|
260
|
+
const lastArg = args.at(-1);
|
|
261
|
+
const hasQueryOption = lastArg && typeof lastArg === "object" && (typeof lastArg.select === "object" || typeof lastArg.skip === "number" || typeof lastArg.limit === "number" || typeof lastArg.sort === "string");
|
|
262
|
+
const queryFn = (0, import_constant.getFilterQuery)(database.Filter, queryKey);
|
|
263
|
+
const query = queryFn(...hasQueryOption ? args.slice(0, -1) : args);
|
|
264
|
+
const queryOption = hasQueryOption ? lastArg : {};
|
|
265
|
+
return { query, queryOption };
|
|
266
|
+
};
|
|
267
|
+
const filterKeyMetaMap = (0, import_constant.getFilterKeyMetaMapOnPrototype)(database.Filter.prototype);
|
|
268
|
+
const queryKeys = [...filterKeyMetaMap.keys()];
|
|
269
|
+
queryKeys.forEach((queryKey) => {
|
|
270
|
+
const queryFn = (0, import_constant.getFilterQuery)(database.Filter, queryKey);
|
|
271
|
+
DbService2.prototype[`list${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
272
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
273
|
+
return this.__list(query, queryOption);
|
|
274
|
+
};
|
|
275
|
+
DbService2.prototype[`listIds${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
276
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
277
|
+
return this.__listIds(query, queryOption);
|
|
278
|
+
};
|
|
279
|
+
DbService2.prototype[`find${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
280
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
281
|
+
return this.__find(query, queryOption);
|
|
282
|
+
};
|
|
283
|
+
DbService2.prototype[`findId${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
284
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
285
|
+
return this.__findId(query, queryOption);
|
|
286
|
+
};
|
|
287
|
+
DbService2.prototype[`pick${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
288
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
289
|
+
return this.__pick(query, queryOption);
|
|
290
|
+
};
|
|
291
|
+
DbService2.prototype[`pickId${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
292
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
293
|
+
return this.__pickId(query, queryOption);
|
|
294
|
+
};
|
|
295
|
+
DbService2.prototype[`exists${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
296
|
+
const query = queryFn(...args);
|
|
297
|
+
return this.__exists(query);
|
|
298
|
+
};
|
|
299
|
+
DbService2.prototype[`count${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
300
|
+
const query = queryFn(...args);
|
|
301
|
+
return this.__count(query);
|
|
302
|
+
};
|
|
303
|
+
DbService2.prototype[`insight${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
304
|
+
const query = queryFn(...args);
|
|
305
|
+
return this.__insight(query);
|
|
306
|
+
};
|
|
307
|
+
});
|
|
308
|
+
Use(`${modelName}Model`)(DbService2.prototype, `${modelName}Model`);
|
|
309
|
+
return DbService2;
|
|
310
|
+
};
|
|
311
|
+
const ExtendedUserService = (database, srvRef, sigRef) => {
|
|
312
|
+
const filterKeyMetaMap = (0, import_constant.getFilterKeyMetaMapOnPrototype)(database.Filter.prototype);
|
|
313
|
+
const queryKeys = [...filterKeyMetaMap.keys()];
|
|
314
|
+
queryKeys.forEach((queryKey) => {
|
|
315
|
+
const queryFn = (0, import_constant.getFilterQuery)(database.Filter, queryKey);
|
|
316
|
+
srvRef.prototype[`list${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
317
|
+
const queryOption = args.at(-1);
|
|
318
|
+
const hasQueryOption = typeof queryOption === "object" && (typeof queryOption.select === "object" || typeof queryOption.skip === "number" || typeof queryOption.limit === "number" || typeof queryOption.sort === "string");
|
|
319
|
+
const query = queryFn(...hasQueryOption ? args.slice(0, -1) : args);
|
|
320
|
+
return this.__list(query, queryOption);
|
|
321
|
+
};
|
|
322
|
+
srvRef.prototype[`insight${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
323
|
+
const query = queryFn(...args);
|
|
324
|
+
return this.__insight(query);
|
|
325
|
+
};
|
|
326
|
+
});
|
|
327
|
+
return srvRef;
|
|
328
|
+
};
|
|
329
|
+
const ExtendedSummaryService = (database, srvRef, sigRef) => {
|
|
330
|
+
return srvRef;
|
|
331
|
+
};
|
|
332
|
+
const ExtendedSettingService = (database, srvRef, sigRef) => {
|
|
333
|
+
return srvRef;
|
|
334
|
+
};
|
|
335
|
+
const Try = () => {
|
|
336
|
+
return function(target, key, descriptor) {
|
|
337
|
+
const originMethod = descriptor.value;
|
|
338
|
+
descriptor.value = async function(...args) {
|
|
339
|
+
try {
|
|
340
|
+
const result = await originMethod.apply(this, args);
|
|
341
|
+
return result;
|
|
342
|
+
} catch (e) {
|
|
343
|
+
this.logger?.warn(`${key} action error return: ${e}`);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
const Cron = (cronTime, { lock = true, serverMode, enabled = true } = {}) => {
|
|
349
|
+
return function(target, key, descriptor) {
|
|
350
|
+
const originMethod = descriptor.value;
|
|
351
|
+
let isRunning = false;
|
|
352
|
+
descriptor.value = async function(...args) {
|
|
353
|
+
if (lock && isRunning)
|
|
354
|
+
return this.logger?.warn(`Cronjob-${key} is already running, skipped`);
|
|
355
|
+
try {
|
|
356
|
+
isRunning = true;
|
|
357
|
+
this.logger?.verbose(`Cron Job-${key} started`);
|
|
358
|
+
const res = await originMethod.apply(this, args);
|
|
359
|
+
this.logger?.verbose(`Cron Job-${key} finished`);
|
|
360
|
+
isRunning = false;
|
|
361
|
+
return res;
|
|
362
|
+
} catch (e) {
|
|
363
|
+
this.logger?.error(`Cron Job-${key} error return: ${e}`);
|
|
364
|
+
isRunning = false;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
if (!enabled)
|
|
368
|
+
return;
|
|
369
|
+
if (!serverMode || process.env.SERVER_MODE === "all" || serverMode === process.env.SERVER_MODE)
|
|
370
|
+
(0, import_schedule.Cron)(cronTime)(target, key, descriptor);
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
const getIntervalMetaMap = (prototype) => {
|
|
374
|
+
return Reflect.getMetadata("serviceInterval", prototype) ?? /* @__PURE__ */ new Map();
|
|
375
|
+
};
|
|
376
|
+
const setIntervalMetaMap = (prototype, intervalMetaMap) => {
|
|
377
|
+
Reflect.defineMetadata("serviceInterval", intervalMetaMap, prototype);
|
|
378
|
+
};
|
|
379
|
+
const Interval = (ms, { lock = true, serverMode, enabled = true } = {}) => {
|
|
380
|
+
return function(target, key, descriptor) {
|
|
381
|
+
const intervalMetaMap = getIntervalMetaMap(target);
|
|
382
|
+
if (intervalMetaMap.has(key))
|
|
383
|
+
return descriptor;
|
|
384
|
+
intervalMetaMap.set(key, descriptor.value);
|
|
385
|
+
setIntervalMetaMap(target, intervalMetaMap);
|
|
386
|
+
const originMethod = descriptor.value;
|
|
387
|
+
let isRunning = false;
|
|
388
|
+
descriptor.value = async function(...args) {
|
|
389
|
+
if (lock && isRunning)
|
|
390
|
+
return this.logger?.warn(`Cronjob-${key} is already running, skipped`);
|
|
391
|
+
try {
|
|
392
|
+
isRunning = true;
|
|
393
|
+
this.logger?.verbose(`Interval Job-${key} started`);
|
|
394
|
+
const res = await originMethod.apply(this, args);
|
|
395
|
+
this.logger?.verbose(`Interval Job-${key} finished`);
|
|
396
|
+
isRunning = false;
|
|
397
|
+
return res;
|
|
398
|
+
} catch (e) {
|
|
399
|
+
this.logger?.error(`Cronjob-${key} error return: ${e}`);
|
|
400
|
+
isRunning = false;
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
if (!enabled)
|
|
404
|
+
return;
|
|
405
|
+
if (!serverMode || process.env.SERVER_MODE === "all" || serverMode === process.env.SERVER_MODE)
|
|
406
|
+
(0, import_schedule.Interval)(ms)(target, key, descriptor);
|
|
407
|
+
};
|
|
408
|
+
};
|
|
409
|
+
const Transaction = () => {
|
|
410
|
+
return function(target, key, descriptor) {
|
|
411
|
+
const originMethod = descriptor.value;
|
|
412
|
+
descriptor.value = function(...args) {
|
|
413
|
+
if (!this.connection)
|
|
414
|
+
throw new Error(`No Connection in function ${key}`);
|
|
415
|
+
return new Promise((resolve, reject) => {
|
|
416
|
+
this.connection.transaction(async () => {
|
|
417
|
+
const res = await originMethod.apply(this, args);
|
|
418
|
+
resolve(res);
|
|
419
|
+
}).catch(reject);
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
return descriptor;
|
|
423
|
+
};
|
|
424
|
+
};
|
|
425
|
+
const Cache = (timeout = 1e3, getCacheKey) => {
|
|
426
|
+
return function(target, key, descriptor) {
|
|
427
|
+
const originMethod = descriptor.value;
|
|
428
|
+
const cacheMap = /* @__PURE__ */ new Map();
|
|
429
|
+
const timerMap = /* @__PURE__ */ new Map();
|
|
430
|
+
descriptor.value = async function(...args) {
|
|
431
|
+
const classType = this.__model ? "doc" : this.__databaseModel ? "service" : "class";
|
|
432
|
+
const model = this.__model ?? this.__databaseModel?.__model;
|
|
433
|
+
const cache = this.__cache ?? this.__databaseModel?.__cache;
|
|
434
|
+
const getCacheKeyFn = getCacheKey ?? JSON.stringify;
|
|
435
|
+
const cacheKey = `${classType}:${model.modelName}:${key}:${getCacheKeyFn(...args)}`;
|
|
436
|
+
const getCache = async (cacheKey2) => {
|
|
437
|
+
if (classType === "class")
|
|
438
|
+
return cacheMap.get(cacheKey2);
|
|
439
|
+
const cached = await cache.get(cacheKey2);
|
|
440
|
+
if (cached)
|
|
441
|
+
return JSON.parse(cached);
|
|
442
|
+
return null;
|
|
443
|
+
};
|
|
444
|
+
const setCache = async (cacheKey2, value) => {
|
|
445
|
+
if (classType === "class") {
|
|
446
|
+
const existingTimer = timerMap.get(cacheKey2);
|
|
447
|
+
if (existingTimer)
|
|
448
|
+
clearTimeout(existingTimer);
|
|
449
|
+
cacheMap.set(cacheKey2, value);
|
|
450
|
+
const timer = setTimeout(() => {
|
|
451
|
+
cacheMap.delete(cacheKey2);
|
|
452
|
+
timerMap.delete(cacheKey2);
|
|
453
|
+
}, timeout);
|
|
454
|
+
timerMap.set(cacheKey2, timer);
|
|
455
|
+
} else
|
|
456
|
+
await cache.set(cacheKey2, JSON.stringify(value), { PX: timeout });
|
|
457
|
+
};
|
|
458
|
+
const cachedData = await getCache(cacheKey);
|
|
459
|
+
if (cachedData) {
|
|
460
|
+
this.logger?.trace(`${model.modelName} cache hit: ${cacheKey}`);
|
|
461
|
+
return cachedData;
|
|
462
|
+
}
|
|
463
|
+
const result = await originMethod.apply(this, args);
|
|
464
|
+
await setCache(cacheKey, result);
|
|
465
|
+
this.logger?.trace(`${model.modelName} cache set: ${cacheKey}`);
|
|
466
|
+
return result;
|
|
467
|
+
};
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
471
|
+
0 && (module.exports = {
|
|
472
|
+
Cache,
|
|
473
|
+
Cron,
|
|
474
|
+
Db,
|
|
475
|
+
DbService,
|
|
476
|
+
ExtendedSettingService,
|
|
477
|
+
ExtendedSummaryService,
|
|
478
|
+
ExtendedUserService,
|
|
479
|
+
Interval,
|
|
480
|
+
LogService,
|
|
481
|
+
MixSrvs,
|
|
482
|
+
Queue,
|
|
483
|
+
Service,
|
|
484
|
+
ServiceStorage,
|
|
485
|
+
Srv,
|
|
486
|
+
Transaction,
|
|
487
|
+
Try,
|
|
488
|
+
Use,
|
|
489
|
+
Websocket,
|
|
490
|
+
serviceOf
|
|
491
|
+
});
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReadStream } from "fs";
|
|
2
|
+
import type { Readable } from "stream";
|
|
3
|
+
export interface FileStream {
|
|
4
|
+
filename: string;
|
|
5
|
+
mimetype: string;
|
|
6
|
+
encoding: string;
|
|
7
|
+
createReadStream(): ReadStream | Readable;
|
|
8
|
+
}
|
|
9
|
+
export interface LocalFile {
|
|
10
|
+
filename: string;
|
|
11
|
+
mimetype: string;
|
|
12
|
+
encoding: string;
|
|
13
|
+
localPath: string;
|
|
14
|
+
}
|
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);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Type } from "@akanjs/base";
|
|
2
|
+
import type { Server } from "socket.io";
|
|
3
|
+
export interface Response<T> {
|
|
4
|
+
data: T;
|
|
5
|
+
}
|
|
6
|
+
export declare const websocketOf: (sigRef: Type, allSrvs: {
|
|
7
|
+
[key: string]: Type;
|
|
8
|
+
}) => {
|
|
9
|
+
new (): {
|
|
10
|
+
__sigRef__: Type;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare const websocketServerOf: (sigRef: Type) => {
|
|
14
|
+
new (): {
|
|
15
|
+
server: Server;
|
|
16
|
+
};
|
|
17
|
+
};
|
package/src/websocket.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
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_signal = require("@akanjs/signal");
|
|
36
|
+
var import_common2 = require("@nestjs/common");
|
|
37
|
+
var import_websockets = require("@nestjs/websockets");
|
|
38
|
+
var import_operators = require("rxjs/operators");
|
|
39
|
+
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 };
|
|
40
|
+
let TransformInterceptor = class {
|
|
41
|
+
intercept(context, next) {
|
|
42
|
+
const [, gqlKey] = [context.getArgByIndex(1), context.getArgByIndex(3)];
|
|
43
|
+
return next.handle().pipe((0, import_operators.map)((data) => ({ event: gqlKey, data })));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
TransformInterceptor = __decorateClass([
|
|
47
|
+
(0, import_common2.Injectable)()
|
|
48
|
+
], TransformInterceptor);
|
|
49
|
+
const makeRoomId = (gqlKey, argValues) => `${gqlKey}-${argValues.join("-")}`;
|
|
50
|
+
const getPubsubInterceptor = (argMetas) => {
|
|
51
|
+
let PubsubInterceptor = class {
|
|
52
|
+
async intercept(context, next) {
|
|
53
|
+
const [socket, { __subscribe__, ...body }, gqlKey] = [context.getArgByIndex(0), context.getArgByIndex(1), context.getArgByIndex(3)];
|
|
54
|
+
const roomId = makeRoomId(
|
|
55
|
+
gqlKey,
|
|
56
|
+
argMetas.map((argMeta) => body[argMeta.name])
|
|
57
|
+
);
|
|
58
|
+
if (__subscribe__)
|
|
59
|
+
await socket.join(roomId);
|
|
60
|
+
else
|
|
61
|
+
await socket.leave(roomId);
|
|
62
|
+
return next.handle().pipe((0, import_operators.map)(() => ({ event: gqlKey, data: { roomId, __subscribe__ } })));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
PubsubInterceptor = __decorateClass([
|
|
66
|
+
(0, import_common2.Injectable)()
|
|
67
|
+
], PubsubInterceptor);
|
|
68
|
+
return PubsubInterceptor;
|
|
69
|
+
};
|
|
70
|
+
const websocketOf = (sigRef, allSrvs) => {
|
|
71
|
+
const sigMeta = (0, import_signal.getSigMeta)(sigRef);
|
|
72
|
+
class WsGateway {
|
|
73
|
+
__sigRef__ = sigRef;
|
|
74
|
+
}
|
|
75
|
+
Object.keys(allSrvs).forEach((srv) => {
|
|
76
|
+
(0, import_common2.Inject)(allSrvs[srv])(WsGateway.prototype, (0, import_common.lowerlize)(srv));
|
|
77
|
+
});
|
|
78
|
+
const messageGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Message");
|
|
79
|
+
for (const gqlMeta of messageGqlMetas) {
|
|
80
|
+
const descriptor = { ...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {} };
|
|
81
|
+
Object.defineProperty(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
82
|
+
const [argMetas, internalArgMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
83
|
+
argMetas.forEach((argMeta) => {
|
|
84
|
+
if (argMeta.type !== "Msg")
|
|
85
|
+
throw new Error(`Argument of Message should be Msg ${sigMeta.refName}-${gqlMeta.key}-${argMeta.name}`);
|
|
86
|
+
(0, import_websockets.MessageBody)(argMeta.name, ...(0, import_nest.getBodyPipes)(argMeta))(WsGateway.prototype, gqlMeta.key, argMeta.idx);
|
|
87
|
+
});
|
|
88
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
89
|
+
const internalDecorator = internalArgMap[internalArgMeta.type];
|
|
90
|
+
internalDecorator(internalArgMeta.option ?? {})(WsGateway.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
91
|
+
});
|
|
92
|
+
(0, import_common2.UseInterceptors)(TransformInterceptor)(WsGateway.prototype, gqlMeta.key, gqlMeta.descriptor);
|
|
93
|
+
(0, import_websockets.SubscribeMessage)(gqlMeta.key)(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
94
|
+
}
|
|
95
|
+
const pubsubGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Pubsub");
|
|
96
|
+
for (const gqlMeta of pubsubGqlMetas) {
|
|
97
|
+
const descriptor = { ...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {} };
|
|
98
|
+
Object.defineProperty(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
99
|
+
const [argMetas, internalArgMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
100
|
+
argMetas.forEach((argMeta) => {
|
|
101
|
+
if (argMeta.type !== "Room")
|
|
102
|
+
throw new Error(`Argument of Message should be Msg ${sigMeta.refName}-${gqlMeta.key}-${argMeta.name}`);
|
|
103
|
+
(0, import_websockets.MessageBody)(argMeta.name, ...(0, import_nest.getBodyPipes)(argMeta))(WsGateway.prototype, gqlMeta.key, argMeta.idx);
|
|
104
|
+
});
|
|
105
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
106
|
+
const internalDecorator = internalArgMap[internalArgMeta.type];
|
|
107
|
+
internalDecorator(internalArgMeta.option ?? {})(WsGateway.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
108
|
+
});
|
|
109
|
+
(0, import_common2.UseInterceptors)(getPubsubInterceptor(argMetas))(WsGateway.prototype, gqlMeta.key, gqlMeta.descriptor);
|
|
110
|
+
(0, import_websockets.SubscribeMessage)(gqlMeta.key)(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
111
|
+
}
|
|
112
|
+
(0, import_websockets.WebSocketGateway)({ cors: { origin: "*" }, transports: ["websocket"] })(WsGateway);
|
|
113
|
+
return WsGateway;
|
|
114
|
+
};
|
|
115
|
+
const websocketServerOf = (sigRef) => {
|
|
116
|
+
const pubsubGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Pubsub");
|
|
117
|
+
let Websocket = class {
|
|
118
|
+
server;
|
|
119
|
+
};
|
|
120
|
+
__decorateClass([
|
|
121
|
+
(0, import_websockets.WebSocketServer)()
|
|
122
|
+
], Websocket.prototype, "server", 2);
|
|
123
|
+
Websocket = __decorateClass([
|
|
124
|
+
(0, import_common2.Injectable)(),
|
|
125
|
+
(0, import_websockets.WebSocketGateway)({ cors: { origin: "*" }, transports: ["websocket"] })
|
|
126
|
+
], Websocket);
|
|
127
|
+
for (const gqlMeta of pubsubGqlMetas) {
|
|
128
|
+
const [argMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
129
|
+
Websocket.prototype[gqlMeta.key] = function(...args) {
|
|
130
|
+
const roomId = makeRoomId(
|
|
131
|
+
gqlMeta.key,
|
|
132
|
+
argMetas.map((argMeta) => args[argMeta.idx])
|
|
133
|
+
);
|
|
134
|
+
this.server.to(roomId).emit(roomId, args.at(-1));
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return Websocket;
|
|
138
|
+
};
|
|
139
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
140
|
+
0 && (module.exports = {
|
|
141
|
+
websocketOf,
|
|
142
|
+
websocketServerOf
|
|
143
|
+
});
|