@akanjs/service 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/index.mjs +1 -0
- package/src/serviceDecorators.mjs +329 -0
package/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akanjs/service",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.55",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -18,5 +18,11 @@
|
|
|
18
18
|
"@nestjs/common": "^10.4.15",
|
|
19
19
|
"@nestjs/mongoose": "^10.1.0",
|
|
20
20
|
"reflect-metadata": "^0.2.2"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"require": "./index.js",
|
|
25
|
+
"import": "./index.mjs"
|
|
26
|
+
}
|
|
21
27
|
}
|
|
22
28
|
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./serviceDecorators";
|
|
@@ -0,0 +1,329 @@
|
|
|
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
|
+
import "reflect-metadata";
|
|
13
|
+
import { applyMixins, capitalize, Logger } from "@akanjs/common";
|
|
14
|
+
import {
|
|
15
|
+
getFilterKeyMetaMapOnPrototype,
|
|
16
|
+
getFilterQuery
|
|
17
|
+
} from "@akanjs/constant";
|
|
18
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
19
|
+
import { InjectConnection, InjectModel } from "@nestjs/mongoose";
|
|
20
|
+
class ServiceStorage {
|
|
21
|
+
}
|
|
22
|
+
const getServiceRefs = (refName) => {
|
|
23
|
+
return Reflect.getMetadata(refName, ServiceStorage.prototype) ?? [];
|
|
24
|
+
};
|
|
25
|
+
const setServiceRefs = (refName, services) => {
|
|
26
|
+
Reflect.defineMetadata(refName, services, ServiceStorage.prototype);
|
|
27
|
+
};
|
|
28
|
+
const isServiceDefined = (srvRef) => {
|
|
29
|
+
return Reflect.getMetadata("service", srvRef.prototype) ?? false;
|
|
30
|
+
};
|
|
31
|
+
const setServiceDefined = (srvRef) => {
|
|
32
|
+
Reflect.defineMetadata("service", true, srvRef.prototype);
|
|
33
|
+
};
|
|
34
|
+
const setServiceMeta = (srvRef, meta) => {
|
|
35
|
+
Reflect.defineMetadata("serviceMeta", meta, srvRef.prototype);
|
|
36
|
+
};
|
|
37
|
+
const getServiceMeta = (srvRef) => {
|
|
38
|
+
return Reflect.getMetadata("serviceMeta", srvRef.prototype);
|
|
39
|
+
};
|
|
40
|
+
const isServiceEnabled = (srvRef) => {
|
|
41
|
+
const meta = getServiceMeta(srvRef);
|
|
42
|
+
return meta?.enabled ?? false;
|
|
43
|
+
};
|
|
44
|
+
const getServiceInjectMetaMapOnPrototype = (prototype) => {
|
|
45
|
+
return Reflect.getMetadata("inject", prototype) ?? /* @__PURE__ */ new Map();
|
|
46
|
+
};
|
|
47
|
+
const setServiceInjectMetaMapOnPrototype = (prototype, injectMetaMap) => {
|
|
48
|
+
Reflect.defineMetadata("inject", injectMetaMap, prototype);
|
|
49
|
+
};
|
|
50
|
+
function Service(name, { enabled = true, serverMode } = {}) {
|
|
51
|
+
return function(target) {
|
|
52
|
+
const services = getServiceRefs(name);
|
|
53
|
+
const isEnabled = enabled && (!serverMode || process.env.SERVER_MODE === serverMode || process.env.SERVER_MODE === "all");
|
|
54
|
+
setServiceMeta(target, { name, enabled: isEnabled });
|
|
55
|
+
if (!isEnabled)
|
|
56
|
+
return target;
|
|
57
|
+
setServiceRefs(name, [...services, target]);
|
|
58
|
+
return target;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function Srv(name) {
|
|
62
|
+
return function(prototype, key) {
|
|
63
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
64
|
+
metadataMap.set(key, { type: "Srv", key, name: name ?? capitalize(key) });
|
|
65
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function Use(name) {
|
|
69
|
+
return function(prototype, key) {
|
|
70
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
71
|
+
metadataMap.set(key, { type: "Use", key, name: name ?? capitalize(key) });
|
|
72
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function Queue(name) {
|
|
76
|
+
return function(prototype, key) {
|
|
77
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
78
|
+
metadataMap.set(key, { type: "Queue", key, name: name ?? capitalize(key) });
|
|
79
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function Websocket(name) {
|
|
83
|
+
return function(prototype, key) {
|
|
84
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
85
|
+
metadataMap.set(key, { type: "Websocket", key, name: name ?? capitalize(key) });
|
|
86
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function Db(name) {
|
|
90
|
+
return function(prototype, key) {
|
|
91
|
+
const metadataMap = getServiceInjectMetaMapOnPrototype(prototype);
|
|
92
|
+
metadataMap.set(key, { type: "Db", key, name });
|
|
93
|
+
setServiceInjectMetaMapOnPrototype(prototype, metadataMap);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const serviceOf = (srvRef) => {
|
|
97
|
+
if (isServiceDefined(srvRef))
|
|
98
|
+
return srvRef;
|
|
99
|
+
const injectMetaMap = getServiceInjectMetaMapOnPrototype(srvRef.prototype);
|
|
100
|
+
for (const injectMeta of [...injectMetaMap.values()]) {
|
|
101
|
+
if (injectMeta.type === "Db")
|
|
102
|
+
InjectModel(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
103
|
+
else if (injectMeta.type === "Use")
|
|
104
|
+
Inject(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
105
|
+
else if (injectMeta.type === "Srv") {
|
|
106
|
+
const services = getServiceRefs(injectMeta.name);
|
|
107
|
+
if (!services.length)
|
|
108
|
+
throw new Error(`Service ${injectMeta.name} not found`);
|
|
109
|
+
Inject(services.at(-1))(srvRef.prototype, injectMeta.key);
|
|
110
|
+
} else if (injectMeta.type === "Queue")
|
|
111
|
+
Inject(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
112
|
+
else
|
|
113
|
+
Inject(injectMeta.name)(srvRef.prototype, injectMeta.key);
|
|
114
|
+
}
|
|
115
|
+
InjectConnection()(srvRef.prototype, "connection");
|
|
116
|
+
Injectable()(srvRef);
|
|
117
|
+
setServiceDefined(srvRef);
|
|
118
|
+
return srvRef;
|
|
119
|
+
};
|
|
120
|
+
const AVOID_MIX_SRV_KEY_SET = /* @__PURE__ */ new Set(["onModuleInit", "onModuleDestroy"]);
|
|
121
|
+
function MixSrvs(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20) {
|
|
122
|
+
class Mix extends t1 {
|
|
123
|
+
}
|
|
124
|
+
const injectMetadataMap = new Map(
|
|
125
|
+
[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) => {
|
|
126
|
+
const injectMetadataMap2 = getServiceInjectMetaMapOnPrototype(srvRef);
|
|
127
|
+
applyMixins(Mix, [srvRef], AVOID_MIX_SRV_KEY_SET);
|
|
128
|
+
return [...acc, ...injectMetadataMap2];
|
|
129
|
+
}, [])
|
|
130
|
+
);
|
|
131
|
+
Reflect.defineMetadata("service", false, Mix.prototype);
|
|
132
|
+
Reflect.defineMetadata("inject", injectMetadataMap, Mix.prototype);
|
|
133
|
+
return Mix;
|
|
134
|
+
}
|
|
135
|
+
const LogService = (name) => {
|
|
136
|
+
class LogService2 {
|
|
137
|
+
logger = new Logger(name);
|
|
138
|
+
}
|
|
139
|
+
return LogService2;
|
|
140
|
+
};
|
|
141
|
+
const DbService = (database, sigRef) => {
|
|
142
|
+
const [modelName, className] = [database.refName, capitalize(database.refName)];
|
|
143
|
+
class DbService2 {
|
|
144
|
+
logger = new Logger(`${modelName}Service`);
|
|
145
|
+
__databaseModel;
|
|
146
|
+
async __list(query, queryOption) {
|
|
147
|
+
return await this.__databaseModel.__list(query, queryOption);
|
|
148
|
+
}
|
|
149
|
+
async __listIds(query, queryOption) {
|
|
150
|
+
return await this.__databaseModel.__listIds(query, queryOption);
|
|
151
|
+
}
|
|
152
|
+
async __find(query, queryOption) {
|
|
153
|
+
return await this.__databaseModel.__find(query, queryOption);
|
|
154
|
+
}
|
|
155
|
+
async __findId(query, queryOption) {
|
|
156
|
+
return await this.__databaseModel.__findId(query, queryOption);
|
|
157
|
+
}
|
|
158
|
+
async __pick(query, queryOption) {
|
|
159
|
+
return await this.__databaseModel.__pick(query, queryOption);
|
|
160
|
+
}
|
|
161
|
+
async __pickId(query, queryOption) {
|
|
162
|
+
return await this.__databaseModel.__pickId(query, queryOption);
|
|
163
|
+
}
|
|
164
|
+
async __exists(query) {
|
|
165
|
+
return await this.__databaseModel.__exists(query);
|
|
166
|
+
}
|
|
167
|
+
async __count(query) {
|
|
168
|
+
return await this.__databaseModel.__count(query);
|
|
169
|
+
}
|
|
170
|
+
async __insight(query) {
|
|
171
|
+
return await this.__databaseModel.__insight(query);
|
|
172
|
+
}
|
|
173
|
+
async __search(searchText, queryOption) {
|
|
174
|
+
return await this.__databaseModel[`search${className}`](searchText, queryOption);
|
|
175
|
+
}
|
|
176
|
+
async __searchDocs(searchText, queryOption) {
|
|
177
|
+
return await this.__databaseModel[`searchDocs${className}`](searchText, queryOption);
|
|
178
|
+
}
|
|
179
|
+
async __searchCount(searchText) {
|
|
180
|
+
return await this.__databaseModel[`searchCount${className}`](searchText);
|
|
181
|
+
}
|
|
182
|
+
async _preCreate(data) {
|
|
183
|
+
return data;
|
|
184
|
+
}
|
|
185
|
+
async _postCreate(doc) {
|
|
186
|
+
return doc;
|
|
187
|
+
}
|
|
188
|
+
async _preUpdate(id, data) {
|
|
189
|
+
return data;
|
|
190
|
+
}
|
|
191
|
+
async _postUpdate(doc) {
|
|
192
|
+
return doc;
|
|
193
|
+
}
|
|
194
|
+
async _preRemove(id) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
async _postRemove(doc) {
|
|
198
|
+
return doc;
|
|
199
|
+
}
|
|
200
|
+
async [`get${className}`](id) {
|
|
201
|
+
return await this.__databaseModel[`get${className}`](id);
|
|
202
|
+
}
|
|
203
|
+
async [`load${className}`](id) {
|
|
204
|
+
return await this.__databaseModel[`load${className}`](id);
|
|
205
|
+
}
|
|
206
|
+
async [`load${className}Many`](ids) {
|
|
207
|
+
return await this.__databaseModel[`load${className}Many`](ids);
|
|
208
|
+
}
|
|
209
|
+
async [`create${className}`](data) {
|
|
210
|
+
const input = await this._preCreate(data);
|
|
211
|
+
const doc = await this.__databaseModel[`create${className}`](input);
|
|
212
|
+
return await this._postCreate(doc);
|
|
213
|
+
}
|
|
214
|
+
async [`update${className}`](id, data) {
|
|
215
|
+
const input = await this._preUpdate(id, data);
|
|
216
|
+
const doc = await this.__databaseModel[`update${className}`](id, input);
|
|
217
|
+
return await this._postUpdate(doc);
|
|
218
|
+
}
|
|
219
|
+
async [`remove${className}`](id) {
|
|
220
|
+
await this._preRemove(id);
|
|
221
|
+
const doc = await this.__databaseModel[`remove${className}`](id);
|
|
222
|
+
return await this._postRemove(doc);
|
|
223
|
+
}
|
|
224
|
+
async [`search${className}`](query, queryOption) {
|
|
225
|
+
return await this.__databaseModel[`search${className}`](query, queryOption);
|
|
226
|
+
}
|
|
227
|
+
async [`searchDocs${className}`](query, queryOption) {
|
|
228
|
+
return await this.__databaseModel[`searchDocs${className}`](query, queryOption);
|
|
229
|
+
}
|
|
230
|
+
async [`searchCount${className}`](query) {
|
|
231
|
+
return await this.__databaseModel[`searchCount${className}`](query);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
__decorateClass([
|
|
235
|
+
Use(`${modelName}Model`)
|
|
236
|
+
], DbService2.prototype, "__databaseModel", 2);
|
|
237
|
+
const getQueryDataFromKey = (queryKey, args) => {
|
|
238
|
+
const lastArg = args.at(-1);
|
|
239
|
+
const hasQueryOption = lastArg && typeof lastArg === "object" && (typeof lastArg.select === "object" || typeof lastArg.skip === "number" || typeof lastArg.limit === "number" || typeof lastArg.sort === "string");
|
|
240
|
+
const queryFn = getFilterQuery(database.Filter, queryKey);
|
|
241
|
+
const query = queryFn(...hasQueryOption ? args.slice(0, -1) : args);
|
|
242
|
+
const queryOption = hasQueryOption ? lastArg : {};
|
|
243
|
+
return { query, queryOption };
|
|
244
|
+
};
|
|
245
|
+
const filterKeyMetaMap = getFilterKeyMetaMapOnPrototype(database.Filter.prototype);
|
|
246
|
+
const queryKeys = [...filterKeyMetaMap.keys()];
|
|
247
|
+
queryKeys.forEach((queryKey) => {
|
|
248
|
+
const queryFn = getFilterQuery(database.Filter, queryKey);
|
|
249
|
+
DbService2.prototype[`list${capitalize(queryKey)}`] = async function(...args) {
|
|
250
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
251
|
+
return this.__list(query, queryOption);
|
|
252
|
+
};
|
|
253
|
+
DbService2.prototype[`listIds${capitalize(queryKey)}`] = async function(...args) {
|
|
254
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
255
|
+
return this.__listIds(query, queryOption);
|
|
256
|
+
};
|
|
257
|
+
DbService2.prototype[`find${capitalize(queryKey)}`] = async function(...args) {
|
|
258
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
259
|
+
return this.__find(query, queryOption);
|
|
260
|
+
};
|
|
261
|
+
DbService2.prototype[`findId${capitalize(queryKey)}`] = async function(...args) {
|
|
262
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
263
|
+
return this.__findId(query, queryOption);
|
|
264
|
+
};
|
|
265
|
+
DbService2.prototype[`pick${capitalize(queryKey)}`] = async function(...args) {
|
|
266
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
267
|
+
return this.__pick(query, queryOption);
|
|
268
|
+
};
|
|
269
|
+
DbService2.prototype[`pickId${capitalize(queryKey)}`] = async function(...args) {
|
|
270
|
+
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
271
|
+
return this.__pickId(query, queryOption);
|
|
272
|
+
};
|
|
273
|
+
DbService2.prototype[`exists${capitalize(queryKey)}`] = async function(...args) {
|
|
274
|
+
const query = queryFn(...args);
|
|
275
|
+
return this.__exists(query);
|
|
276
|
+
};
|
|
277
|
+
DbService2.prototype[`count${capitalize(queryKey)}`] = async function(...args) {
|
|
278
|
+
const query = queryFn(...args);
|
|
279
|
+
return this.__count(query);
|
|
280
|
+
};
|
|
281
|
+
DbService2.prototype[`insight${capitalize(queryKey)}`] = async function(...args) {
|
|
282
|
+
const query = queryFn(...args);
|
|
283
|
+
return this.__insight(query);
|
|
284
|
+
};
|
|
285
|
+
});
|
|
286
|
+
Use(`${modelName}Model`)(DbService2.prototype, `${modelName}Model`);
|
|
287
|
+
return DbService2;
|
|
288
|
+
};
|
|
289
|
+
const ExtendedUserService = (database, srvRef, sigRef) => {
|
|
290
|
+
const filterKeyMetaMap = getFilterKeyMetaMapOnPrototype(database.Filter.prototype);
|
|
291
|
+
const queryKeys = [...filterKeyMetaMap.keys()];
|
|
292
|
+
queryKeys.forEach((queryKey) => {
|
|
293
|
+
const queryFn = getFilterQuery(database.Filter, queryKey);
|
|
294
|
+
srvRef.prototype[`list${capitalize(queryKey)}`] = async function(...args) {
|
|
295
|
+
const queryOption = args.at(-1);
|
|
296
|
+
const hasQueryOption = typeof queryOption === "object" && (typeof queryOption.select === "object" || typeof queryOption.skip === "number" || typeof queryOption.limit === "number" || typeof queryOption.sort === "string");
|
|
297
|
+
const query = queryFn(...hasQueryOption ? args.slice(0, -1) : args);
|
|
298
|
+
return this.__list(query, queryOption);
|
|
299
|
+
};
|
|
300
|
+
srvRef.prototype[`insight${capitalize(queryKey)}`] = async function(...args) {
|
|
301
|
+
const query = queryFn(...args);
|
|
302
|
+
return this.__insight(query);
|
|
303
|
+
};
|
|
304
|
+
});
|
|
305
|
+
return srvRef;
|
|
306
|
+
};
|
|
307
|
+
const ExtendedSummaryService = (database, srvRef, sigRef) => {
|
|
308
|
+
return srvRef;
|
|
309
|
+
};
|
|
310
|
+
const ExtendedSettingService = (database, srvRef, sigRef) => {
|
|
311
|
+
return srvRef;
|
|
312
|
+
};
|
|
313
|
+
export {
|
|
314
|
+
Db,
|
|
315
|
+
DbService,
|
|
316
|
+
ExtendedSettingService,
|
|
317
|
+
ExtendedSummaryService,
|
|
318
|
+
ExtendedUserService,
|
|
319
|
+
LogService,
|
|
320
|
+
MixSrvs,
|
|
321
|
+
Queue,
|
|
322
|
+
Service,
|
|
323
|
+
ServiceStorage,
|
|
324
|
+
Srv,
|
|
325
|
+
Use,
|
|
326
|
+
Websocket,
|
|
327
|
+
isServiceEnabled,
|
|
328
|
+
serviceOf
|
|
329
|
+
};
|