speedly 1.2.48 → 2.0.5
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/dist/cjs/auth/auth2.d.ts +18 -0
- package/dist/cjs/auth/auth2.js +93 -0
- package/dist/cjs/config/init.d.ts +15 -0
- package/dist/cjs/config/init.js +58 -0
- package/dist/cjs/db/db.d.ts +65 -66
- package/dist/cjs/db/db.js +15 -22
- package/dist/cjs/document/index.d.ts +2 -0
- package/dist/cjs/document/index.js +7 -0
- package/dist/cjs/index.d.ts +3 -53
- package/dist/cjs/index.js +7 -22
- package/dist/cjs/kit/auth/auth.d.ts +3 -0
- package/dist/cjs/kit/auth/auth.js +38 -0
- package/dist/cjs/kit/auth/types.d.ts +19 -0
- package/dist/cjs/kit/auth/types.js +2 -0
- package/dist/cjs/kit/db/db.d.ts +182 -0
- package/dist/cjs/kit/db/db.js +594 -0
- package/dist/cjs/kit/db/utils.d.ts +3 -0
- package/dist/cjs/kit/db/utils.js +15 -0
- package/dist/cjs/kit/index.d.ts +5 -0
- package/dist/cjs/kit/index.js +14 -0
- package/dist/cjs/kit/uploader/uploader.d.ts +24 -0
- package/dist/cjs/kit/uploader/uploader.js +148 -0
- package/dist/cjs/kit/validator/validator.d.ts +9 -0
- package/dist/cjs/kit/validator/validator.js +36 -0
- package/dist/cjs/model/index.d.ts +2 -0
- package/dist/cjs/model/index.js +8 -0
- package/dist/cjs/model/translation.d.ts +24 -6
- package/dist/cjs/modules/index.d.ts +2 -0
- package/dist/cjs/modules/index.js +8 -0
- package/dist/cjs/modules/translation/translation.routes.js +12 -8
- package/dist/cjs/util/index.d.ts +2 -0
- package/dist/cjs/util/index.js +8 -0
- package/dist/cjs/util/makeOptional.js +17 -7
- package/dist/cjs/yup.config.d.ts +2 -0
- package/dist/cjs/yup.config.js +24 -0
- package/dist/esm/config/init.d.ts +15 -0
- package/dist/esm/config/init.js +58 -0
- package/dist/esm/db/db.d.ts +65 -66
- package/dist/esm/db/db.js +15 -22
- package/dist/esm/document/index.d.ts +2 -0
- package/dist/esm/document/index.js +7 -0
- package/dist/esm/index.d.ts +3 -53
- package/dist/esm/index.js +7 -22
- package/dist/esm/kit/auth/auth.d.ts +3 -0
- package/dist/esm/kit/auth/auth.js +38 -0
- package/dist/esm/kit/auth/types.d.ts +19 -0
- package/dist/esm/kit/auth/types.js +2 -0
- package/dist/esm/kit/db/db.d.ts +182 -0
- package/dist/esm/kit/db/db.js +594 -0
- package/dist/esm/kit/db/utils.d.ts +3 -0
- package/dist/esm/kit/db/utils.js +15 -0
- package/dist/esm/kit/index.d.ts +5 -0
- package/dist/esm/kit/index.js +14 -0
- package/dist/esm/kit/uploader/uploader.d.ts +24 -0
- package/dist/esm/kit/uploader/uploader.js +148 -0
- package/dist/esm/kit/validator/validator.d.ts +9 -0
- package/dist/esm/kit/validator/validator.js +36 -0
- package/dist/esm/model/index.d.ts +2 -0
- package/dist/esm/model/index.js +8 -0
- package/dist/esm/model/translation.d.ts +24 -6
- package/dist/esm/modules/index.d.ts +2 -0
- package/dist/esm/modules/index.js +8 -0
- package/dist/esm/modules/translation/translation.routes.js +12 -8
- package/dist/esm/util/index.d.ts +2 -0
- package/dist/esm/util/index.js +8 -0
- package/dist/esm/util/makeOptional.js +17 -7
- package/package.json +1 -1
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
let __path = "./models/";
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const strToObj_1 = __importDefault(require("../../util/strToObj"));
|
|
9
|
+
const getConfig_1 = __importDefault(require("../../util/getConfig"));
|
|
10
|
+
const translator_1 = __importDefault(require("../../util/translator"));
|
|
11
|
+
const utils_1 = require("./utils");
|
|
12
|
+
let configs = {
|
|
13
|
+
dbType: "mongodb",
|
|
14
|
+
path: "../models",
|
|
15
|
+
type: "external",
|
|
16
|
+
dbEnv: "DB_URL",
|
|
17
|
+
...(0, getConfig_1.default)("db"),
|
|
18
|
+
};
|
|
19
|
+
const usingMongoDb = (collectionName, config = { type: "external" }) => {
|
|
20
|
+
let model;
|
|
21
|
+
let queryState = {
|
|
22
|
+
queries: [],
|
|
23
|
+
};
|
|
24
|
+
if (config?.path)
|
|
25
|
+
__path = config.path;
|
|
26
|
+
model = require(path_1.default.join(...(config.type == "external"
|
|
27
|
+
? [require.main?.filename || "./", __path]
|
|
28
|
+
: ["../model"]), collectionName));
|
|
29
|
+
if (model.default)
|
|
30
|
+
model = model.default;
|
|
31
|
+
const actionHandler = {
|
|
32
|
+
find: (match = {}) => {
|
|
33
|
+
queryState.action = "find";
|
|
34
|
+
queryState.match = match;
|
|
35
|
+
Object.defineProperty(handler, "name", {
|
|
36
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
37
|
+
});
|
|
38
|
+
return handler;
|
|
39
|
+
},
|
|
40
|
+
create: (body = {}) => {
|
|
41
|
+
queryState.action = "create";
|
|
42
|
+
queryState.body = body;
|
|
43
|
+
Object.defineProperty(handler, "name", {
|
|
44
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
45
|
+
});
|
|
46
|
+
return handler;
|
|
47
|
+
},
|
|
48
|
+
updateOne: (match = {}, body = {}) => {
|
|
49
|
+
queryState.action = "updateOne";
|
|
50
|
+
queryState.match = match;
|
|
51
|
+
queryState.body = body;
|
|
52
|
+
Object.defineProperty(handler, "name", {
|
|
53
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
54
|
+
});
|
|
55
|
+
return handler;
|
|
56
|
+
},
|
|
57
|
+
updateMany: (match = {}, body = {}) => {
|
|
58
|
+
queryState.action = "updateMany";
|
|
59
|
+
queryState.match = match;
|
|
60
|
+
queryState.body = body;
|
|
61
|
+
Object.defineProperty(handler, "name", {
|
|
62
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
63
|
+
});
|
|
64
|
+
return handler;
|
|
65
|
+
},
|
|
66
|
+
deleteOne: (match = {}) => {
|
|
67
|
+
queryState.action = "deleteOne";
|
|
68
|
+
queryState.match = match;
|
|
69
|
+
Object.defineProperty(handler, "name", {
|
|
70
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
71
|
+
});
|
|
72
|
+
return handler;
|
|
73
|
+
},
|
|
74
|
+
deleteMany: (match = {}) => {
|
|
75
|
+
queryState.action = "deleteMany";
|
|
76
|
+
queryState.match = match;
|
|
77
|
+
Object.defineProperty(handler, "name", {
|
|
78
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
79
|
+
});
|
|
80
|
+
return handler;
|
|
81
|
+
},
|
|
82
|
+
findOne: (match = {}) => {
|
|
83
|
+
queryState.action = "findOne";
|
|
84
|
+
queryState.match = match;
|
|
85
|
+
Object.defineProperty(handler, "name", {
|
|
86
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
87
|
+
});
|
|
88
|
+
return handler;
|
|
89
|
+
},
|
|
90
|
+
findOneAndUpdate: (match = {}, body = {}) => {
|
|
91
|
+
queryState.action = "findOneAndUpdate";
|
|
92
|
+
queryState.match = match;
|
|
93
|
+
queryState.body = body;
|
|
94
|
+
Object.defineProperty(handler, "name", {
|
|
95
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
96
|
+
});
|
|
97
|
+
return handler;
|
|
98
|
+
},
|
|
99
|
+
aggregate: (pipeline = []) => {
|
|
100
|
+
queryState.action = "aggregate";
|
|
101
|
+
queryState.pipeline = pipeline;
|
|
102
|
+
Object.defineProperty(handler, "name", {
|
|
103
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
104
|
+
});
|
|
105
|
+
return handler;
|
|
106
|
+
},
|
|
107
|
+
findOneAndDelete: (match = {}) => {
|
|
108
|
+
queryState.action = "findOneAndDelete";
|
|
109
|
+
queryState.match = match;
|
|
110
|
+
Object.defineProperty(handler, "name", {
|
|
111
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
112
|
+
});
|
|
113
|
+
return handler;
|
|
114
|
+
},
|
|
115
|
+
findById: (id = "") => {
|
|
116
|
+
queryState.action = "findById";
|
|
117
|
+
queryState.id = id;
|
|
118
|
+
Object.defineProperty(handler, "name", {
|
|
119
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
120
|
+
});
|
|
121
|
+
return handler;
|
|
122
|
+
},
|
|
123
|
+
findByIdAndUpdate: (id = "", body = {}) => {
|
|
124
|
+
queryState.action = "findByIdAndUpdate";
|
|
125
|
+
queryState.id = id;
|
|
126
|
+
queryState.body = body;
|
|
127
|
+
Object.defineProperty(handler, "name", {
|
|
128
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
129
|
+
});
|
|
130
|
+
return handler;
|
|
131
|
+
},
|
|
132
|
+
findByIdAndDelete: (id = "") => {
|
|
133
|
+
queryState.action = "findByIdAndDelete";
|
|
134
|
+
queryState.id = id;
|
|
135
|
+
Object.defineProperty(handler, "name", {
|
|
136
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
137
|
+
});
|
|
138
|
+
return handler;
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Handles database operations for the specified model based on the action and query parameters.
|
|
143
|
+
* @param {object} req - Express request object containing query and body data.
|
|
144
|
+
* @param {object} res - Express response object used to send responses.
|
|
145
|
+
* @param {function} next - Express next middleware function for error handling.
|
|
146
|
+
*/
|
|
147
|
+
const handler = async (req, res, next) => {
|
|
148
|
+
let data = undefined, detail = {};
|
|
149
|
+
let match = {};
|
|
150
|
+
let realTimeQueries = [...queryState.queries];
|
|
151
|
+
const { sort, limit, page, select } = req.query;
|
|
152
|
+
if (req.query.search) {
|
|
153
|
+
const searchStr = Array.isArray(req.query.search)
|
|
154
|
+
? req.query.search[0]
|
|
155
|
+
: req.query.search;
|
|
156
|
+
if (typeof searchStr === "string") {
|
|
157
|
+
const splittedSearch = searchStr.trim().split(/\s|/g);
|
|
158
|
+
const searchValue = splittedSearch
|
|
159
|
+
.map((word) => `(?=.*${word.split("").join("\\s?")})`)
|
|
160
|
+
.join("");
|
|
161
|
+
match.$or = [
|
|
162
|
+
{ name: { $regex: searchValue } },
|
|
163
|
+
{ title: { $regex: searchValue } },
|
|
164
|
+
{ subTitle: { $regex: searchValue } },
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (req.query.filters) {
|
|
169
|
+
const filtersStr = Array.isArray(req.query.filters)
|
|
170
|
+
? req.query.filters[0]
|
|
171
|
+
: req.query.filters;
|
|
172
|
+
if (typeof filtersStr === "string") {
|
|
173
|
+
match = { ...match, ...JSON.parse(filtersStr) };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (typeof queryState.match == "function")
|
|
177
|
+
match = { ...match, ...queryState.match(req) };
|
|
178
|
+
if (typeof queryState.match == "object")
|
|
179
|
+
match = { ...match, ...queryState.match };
|
|
180
|
+
{
|
|
181
|
+
if (req.query.sort && queryState.action !== "aggregate") {
|
|
182
|
+
const sortQueryIndex = realTimeQueries.findIndex((q) => "type" in q && q.type == "sort");
|
|
183
|
+
const sortStr = Array.isArray(req.query.sort)
|
|
184
|
+
? req.query.sort[0]
|
|
185
|
+
: req.query.sort;
|
|
186
|
+
const sortObject = typeof sortStr === "string" ? (0, strToObj_1.default)(sortStr) : {};
|
|
187
|
+
if (sortQueryIndex == -1)
|
|
188
|
+
realTimeQueries.push({
|
|
189
|
+
type: "sort",
|
|
190
|
+
value: (typeof sortStr === "string" ? sortStr : "") + " _id",
|
|
191
|
+
});
|
|
192
|
+
else
|
|
193
|
+
realTimeQueries.splice(sortQueryIndex, 1, {
|
|
194
|
+
type: "sort",
|
|
195
|
+
value: {
|
|
196
|
+
...(realTimeQueries[sortQueryIndex] &&
|
|
197
|
+
"value" in realTimeQueries[sortQueryIndex] &&
|
|
198
|
+
typeof realTimeQueries[sortQueryIndex].value === "object" &&
|
|
199
|
+
realTimeQueries[sortQueryIndex].value !== null
|
|
200
|
+
? realTimeQueries[sortQueryIndex].value
|
|
201
|
+
: {}),
|
|
202
|
+
...sortObject,
|
|
203
|
+
_id: 1,
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
if (req.query.page && queryState.action !== "aggregate") {
|
|
208
|
+
const pageStr = Array.isArray(req.query.page)
|
|
209
|
+
? req.query.page[0]
|
|
210
|
+
: typeof req.query.page === "string"
|
|
211
|
+
? req.query.page
|
|
212
|
+
: "";
|
|
213
|
+
const limitStr = Array.isArray(req.query.limit)
|
|
214
|
+
? req.query.limit[0]
|
|
215
|
+
: typeof req.query.limit === "string"
|
|
216
|
+
? req.query.limit
|
|
217
|
+
: "";
|
|
218
|
+
const pageNum = parseInt(typeof pageStr === "string" ? pageStr : "") || 1;
|
|
219
|
+
const limitNum = parseInt(typeof limitStr === "string" ? limitStr : "") ||
|
|
220
|
+
configs?.pagination?.quantity ||
|
|
221
|
+
20;
|
|
222
|
+
if (realTimeQueries.findIndex((q) => "type" in q && q.type == "skip") ==
|
|
223
|
+
-1)
|
|
224
|
+
realTimeQueries.push({
|
|
225
|
+
type: "skip",
|
|
226
|
+
value: (pageNum - 1) * limitNum,
|
|
227
|
+
});
|
|
228
|
+
if (realTimeQueries.findIndex((q) => "type" in q && q.type == "limit") ==
|
|
229
|
+
-1)
|
|
230
|
+
realTimeQueries.push({
|
|
231
|
+
type: "limit",
|
|
232
|
+
value: limitNum,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
else if (req.query.limit && queryState.action !== "aggregate") {
|
|
236
|
+
const pageStrAgg = Array.isArray(req.query.page)
|
|
237
|
+
? req.query.page[0]
|
|
238
|
+
: typeof req.query.page === "string"
|
|
239
|
+
? req.query.page
|
|
240
|
+
: "";
|
|
241
|
+
const limitStrAgg = Array.isArray(req.query.limit)
|
|
242
|
+
? req.query.limit[0]
|
|
243
|
+
: typeof req.query.limit === "string"
|
|
244
|
+
? req.query.limit
|
|
245
|
+
: "";
|
|
246
|
+
const pageNumAgg = parseInt(typeof pageStrAgg === "string" ? pageStrAgg : "") || 1;
|
|
247
|
+
const limitNumAgg = parseInt(typeof limitStrAgg === "string" ? limitStrAgg : "") || 20;
|
|
248
|
+
realTimeQueries.push({ $skip: (pageNumAgg - 1) * limitNumAgg }, {
|
|
249
|
+
$limit: parseInt(Array.isArray(req.query.limit)
|
|
250
|
+
? String(req.query.limit[0])
|
|
251
|
+
: typeof req.query.limit === "string"
|
|
252
|
+
? req.query.limit
|
|
253
|
+
: req.query.limit !== undefined
|
|
254
|
+
? String(req.query.limit)
|
|
255
|
+
: ""),
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
else if (req.query.limit && queryState.action == "aggregate") {
|
|
259
|
+
realTimeQueries.push({ $limit: parseInt(String(req.query.limit)) });
|
|
260
|
+
}
|
|
261
|
+
if (req.query.select && queryState.action !== "aggregate") {
|
|
262
|
+
const selectQueryIndex = realTimeQueries.findIndex((q) => "type" in q && q.type == "select");
|
|
263
|
+
if (selectQueryIndex == -1)
|
|
264
|
+
realTimeQueries.push({ type: "select", value: req.query.select });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
const page = parseInt(Array.isArray(req.query.page)
|
|
269
|
+
? String(req.query.page[0])
|
|
270
|
+
: typeof req.query.page === "string"
|
|
271
|
+
? req.query.page
|
|
272
|
+
: "") || 1;
|
|
273
|
+
const limit = parseInt(Array.isArray(req.query.limit)
|
|
274
|
+
? String(req.query.limit[0])
|
|
275
|
+
: typeof req.query.limit === "string"
|
|
276
|
+
? req.query.limit
|
|
277
|
+
: req.query.limit !== undefined
|
|
278
|
+
? String(req.query.limit)
|
|
279
|
+
: "") ||
|
|
280
|
+
configs.pagination?.quantity ||
|
|
281
|
+
20;
|
|
282
|
+
switch (queryState.action) {
|
|
283
|
+
case "find":
|
|
284
|
+
if (configs.pagination?.detailed && req.query.page) {
|
|
285
|
+
const page = parseInt(Array.isArray(req.query.page)
|
|
286
|
+
? String(req.query.page[0])
|
|
287
|
+
: typeof req.query.page === "string"
|
|
288
|
+
? req.query.page
|
|
289
|
+
: "") || 1;
|
|
290
|
+
const limit = parseInt(Array.isArray(req.query.limit)
|
|
291
|
+
? String(req.query.limit[0])
|
|
292
|
+
: typeof req.query.limit === "string"
|
|
293
|
+
? req.query.limit
|
|
294
|
+
: req.query.limit !== undefined
|
|
295
|
+
? String(req.query.limit)
|
|
296
|
+
: "") ||
|
|
297
|
+
configs.pagination?.quantity ||
|
|
298
|
+
20;
|
|
299
|
+
console.log("db", 261, model, config.path);
|
|
300
|
+
const totalPages = Math.ceil((await model.countDocuments(match)) / limit);
|
|
301
|
+
detail = {
|
|
302
|
+
...detail,
|
|
303
|
+
page,
|
|
304
|
+
limit,
|
|
305
|
+
totalPages,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
data = model?.find?.call(model, match);
|
|
309
|
+
break;
|
|
310
|
+
case "create":
|
|
311
|
+
req.body = Array.isArray(req.body)
|
|
312
|
+
? req.body.map((item) => ({
|
|
313
|
+
...item,
|
|
314
|
+
...(typeof queryState.body == "function"
|
|
315
|
+
? queryState.body(req)
|
|
316
|
+
: queryState.body),
|
|
317
|
+
}))
|
|
318
|
+
: {
|
|
319
|
+
...req.body,
|
|
320
|
+
...(typeof queryState.body == "function"
|
|
321
|
+
? queryState.body(req)
|
|
322
|
+
: queryState.body),
|
|
323
|
+
};
|
|
324
|
+
data = model?.[queryState.action]?.call(model, req.body);
|
|
325
|
+
break;
|
|
326
|
+
case "updateOne":
|
|
327
|
+
data = model?.[queryState.action]?.call(model, match, {
|
|
328
|
+
...req.body,
|
|
329
|
+
...(typeof queryState.body == "function"
|
|
330
|
+
? queryState.body(req)
|
|
331
|
+
: queryState.body),
|
|
332
|
+
});
|
|
333
|
+
break;
|
|
334
|
+
case "updateMany":
|
|
335
|
+
data = model?.[queryState.action]?.call(model, match, {
|
|
336
|
+
...req.body,
|
|
337
|
+
...(typeof queryState.body == "function"
|
|
338
|
+
? queryState.body(req)
|
|
339
|
+
: queryState.body),
|
|
340
|
+
});
|
|
341
|
+
break;
|
|
342
|
+
case "deleteOne":
|
|
343
|
+
data = model?.[queryState.action]?.call(model, match);
|
|
344
|
+
break;
|
|
345
|
+
case "deleteMany":
|
|
346
|
+
data = model?.[queryState.action]?.call(model, match);
|
|
347
|
+
break;
|
|
348
|
+
case "findOne":
|
|
349
|
+
data = model?.[queryState.action]?.call(model, match);
|
|
350
|
+
break;
|
|
351
|
+
case "findOneAndUpdate":
|
|
352
|
+
req.document = await model.findOne(match);
|
|
353
|
+
data = model?.[queryState.action]?.call(model, match, (0, utils_1.createUpdateObject)(req, queryState.body || {}), { new: true });
|
|
354
|
+
break;
|
|
355
|
+
case "findOneAndDelete":
|
|
356
|
+
data = model?.[queryState.action]?.call(model, match);
|
|
357
|
+
break;
|
|
358
|
+
case "findById":
|
|
359
|
+
data = model?.[queryState.action]?.call(model, queryState.id || req.params.id);
|
|
360
|
+
break;
|
|
361
|
+
case "aggregate":
|
|
362
|
+
if (configs.pagination?.detailed && req.query.page) {
|
|
363
|
+
const page = parseInt(req.query.page) || 1;
|
|
364
|
+
const limit = parseInt(req.query.limit) ||
|
|
365
|
+
configs.pagination?.quantity ||
|
|
366
|
+
20;
|
|
367
|
+
realTimeQueries = [
|
|
368
|
+
{
|
|
369
|
+
$facet: {
|
|
370
|
+
content: [...realTimeQueries],
|
|
371
|
+
detail: [
|
|
372
|
+
{ $group: { _id: null, count: { $sum: 1 } } },
|
|
373
|
+
{
|
|
374
|
+
$addFields: {
|
|
375
|
+
totalPages: { $ceil: { $divide: ["$count", limit] } },
|
|
376
|
+
page,
|
|
377
|
+
limit,
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
$unset: ["_id", "count"],
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
$unwind: "$detail",
|
|
388
|
+
},
|
|
389
|
+
];
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
realTimeQueries = [
|
|
393
|
+
{
|
|
394
|
+
$facet: {
|
|
395
|
+
content: [...realTimeQueries],
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
];
|
|
399
|
+
}
|
|
400
|
+
// res.status(200).json([...queryState.pipeline , ...realTimeQueries])
|
|
401
|
+
if (typeof queryState.pipeline == "function") {
|
|
402
|
+
data = model?.[queryState.action]?.call(model, [
|
|
403
|
+
...queryState.pipeline(req),
|
|
404
|
+
...realTimeQueries,
|
|
405
|
+
]);
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
data = model?.[queryState.action]?.call(model, [
|
|
409
|
+
...(queryState.pipeline || []),
|
|
410
|
+
...realTimeQueries,
|
|
411
|
+
]);
|
|
412
|
+
}
|
|
413
|
+
break;
|
|
414
|
+
case "findByIdAndUpdate":
|
|
415
|
+
req.document = await model.findByIdAndUpdate(queryState.id || req.params.id);
|
|
416
|
+
data = model?.[queryState.action]?.call(model, queryState.id || req.params.id, Object.entries({
|
|
417
|
+
...req.body,
|
|
418
|
+
...(typeof queryState.body == "function"
|
|
419
|
+
? queryState.body(req)
|
|
420
|
+
: queryState.body),
|
|
421
|
+
}).reduce((acc, [key, value]) => {
|
|
422
|
+
if (value == "$$REMOVE") {
|
|
423
|
+
return { ...acc, $unset: { ...acc?.$unset, [key]: value } };
|
|
424
|
+
}
|
|
425
|
+
return { ...acc, $set: { ...acc?.$set, [key]: value } };
|
|
426
|
+
}, {}), { new: true });
|
|
427
|
+
break;
|
|
428
|
+
case "findByIdAndDelete":
|
|
429
|
+
data = model?.[queryState.action]?.call(model, queryState.id || req.params.id);
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
if (queryState.action != "aggregate")
|
|
433
|
+
realTimeQueries.forEach((q) => {
|
|
434
|
+
if ("type" in q && typeof data?.[q.type] === "function") {
|
|
435
|
+
data = data[q.type].call(data, q.value);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
// if(req.query.select) data = data.select(req.query.select)
|
|
439
|
+
data = await data;
|
|
440
|
+
if (!data) {
|
|
441
|
+
next({
|
|
442
|
+
status: 404,
|
|
443
|
+
json: {
|
|
444
|
+
message: `${collectionName} not found.`,
|
|
445
|
+
},
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
if (res.logger && req.user) {
|
|
450
|
+
if (queryState.action &&
|
|
451
|
+
queryState.action.match(/create|update|delete/i)?.[0]) {
|
|
452
|
+
res.logger(`${{ log: "لاگها" }[collectionName] ||
|
|
453
|
+
(await (0, translator_1.default)(`${collectionName}`))} ${data.name || data.title || data.id || data._id || ""} توسط ${(req.user?.firstName ? req.user?.firstName + " " : "") +
|
|
454
|
+
(req.user?.lastName || "") ||
|
|
455
|
+
`ادمین با شماره ${req.user.phoneNumber}`} ${{
|
|
456
|
+
create: "ایجاد",
|
|
457
|
+
update: "بروزرسانی",
|
|
458
|
+
delete: "حذف",
|
|
459
|
+
nothing: "",
|
|
460
|
+
}[queryState.action
|
|
461
|
+
.match(/create|update|delete/i)?.[0]
|
|
462
|
+
?.toLowerCase() || "nothing"] || queryState.action} شد`, queryState.action.match(/update/i)?.[0]
|
|
463
|
+
? (await Promise.all(Object.entries(data._doc)
|
|
464
|
+
.filter(([key, value]) => ["_id", "__v", "updatedAt"].includes(key)
|
|
465
|
+
? false
|
|
466
|
+
: req.document._doc[key] == undefined ||
|
|
467
|
+
req.document._doc[key]?.toString?.() !==
|
|
468
|
+
value?.toString?.())
|
|
469
|
+
.map(async (item) => {
|
|
470
|
+
const translatedField = await (0, translator_1.default)(item[0], "fa");
|
|
471
|
+
if (req.document[item[0]] == undefined) {
|
|
472
|
+
return `فیلد ${translatedField} اضافه شد`;
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
return `فیلد ${translatedField} از ${req.document[item[0]]} به ${item[1]} تغییر یافت`;
|
|
476
|
+
}
|
|
477
|
+
}))).concat(await Promise.all(Object.entries(req.document._doc)
|
|
478
|
+
.filter(([key, value]) => ["_id", "__v", "updatedAt"].includes(key)
|
|
479
|
+
? false
|
|
480
|
+
: data._doc[key] == undefined)
|
|
481
|
+
.map(async (item) => {
|
|
482
|
+
const translatedField = await (0, translator_1.default)(item[0], "fa");
|
|
483
|
+
return `فیلد ${translatedField} حذف شد`;
|
|
484
|
+
})))
|
|
485
|
+
: []);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const action = queryState.action?.match(/create|update|delet/i)?.[0] || "find";
|
|
489
|
+
const resBody = queryState.action == "aggregate"
|
|
490
|
+
? {
|
|
491
|
+
message: config?.message ||
|
|
492
|
+
`the ${collectionName} was found successfully`,
|
|
493
|
+
content: [],
|
|
494
|
+
...data[0],
|
|
495
|
+
}
|
|
496
|
+
: {
|
|
497
|
+
content: data,
|
|
498
|
+
...{ detail: Object.keys(detail).length ? detail : undefined },
|
|
499
|
+
message: config?.message ||
|
|
500
|
+
`the ${collectionName} was ${action == "find" ? "found" : action + "ed"}`,
|
|
501
|
+
};
|
|
502
|
+
res.success ? res.success(200, resBody) : res.status(200).json(resBody);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
catch (err) {
|
|
506
|
+
if (err &&
|
|
507
|
+
typeof err === "object" &&
|
|
508
|
+
"errorResponse" in err &&
|
|
509
|
+
err.errorResponse?.code == 11000)
|
|
510
|
+
return next({
|
|
511
|
+
status: 405,
|
|
512
|
+
json: {
|
|
513
|
+
message: `(${Object.entries(err.errorResponse.keyValue)[0][0]}) already exists; write a unique value`,
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
console.error("Error : model", err);
|
|
517
|
+
return next({
|
|
518
|
+
status: 500,
|
|
519
|
+
json: {
|
|
520
|
+
message: (err instanceof Error ? err.message : String(err)) ||
|
|
521
|
+
"internal server error",
|
|
522
|
+
},
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
handler.select = (value) => {
|
|
527
|
+
queryState.queries.push({
|
|
528
|
+
type: "select",
|
|
529
|
+
value: typeof value == "string" ? (0, strToObj_1.default)(value, 0) : value,
|
|
530
|
+
});
|
|
531
|
+
return handler;
|
|
532
|
+
};
|
|
533
|
+
handler.sort = (value) => {
|
|
534
|
+
queryState.queries.push({
|
|
535
|
+
type: "sort",
|
|
536
|
+
value: typeof value == "string" ? (0, strToObj_1.default)(value) : value,
|
|
537
|
+
});
|
|
538
|
+
return handler;
|
|
539
|
+
};
|
|
540
|
+
handler.skip = (value) => {
|
|
541
|
+
queryState.queries.push({ type: "skip", value });
|
|
542
|
+
return handler;
|
|
543
|
+
};
|
|
544
|
+
handler.limit = (value) => {
|
|
545
|
+
queryState.queries.push({ type: "limit", value });
|
|
546
|
+
return handler;
|
|
547
|
+
};
|
|
548
|
+
handler.populate = (value) => {
|
|
549
|
+
queryState.queries.push({ type: "populate", value });
|
|
550
|
+
return handler;
|
|
551
|
+
};
|
|
552
|
+
return actionHandler;
|
|
553
|
+
};
|
|
554
|
+
// const usingMySql = (tableName, config) => {
|
|
555
|
+
// const mysqlConnection = mysql.createConnection(process.env[configs.dbEnv]);
|
|
556
|
+
// mysqlConnection.connect((err) => {
|
|
557
|
+
// if (err) console.log("DB ERR: ", err.message);
|
|
558
|
+
// else console.log("database connected successfully ");
|
|
559
|
+
// });
|
|
560
|
+
// const db = mysqlConnection.promise()
|
|
561
|
+
// let queryState = {
|
|
562
|
+
// queryString: "select * from ?",
|
|
563
|
+
// variables: [{ tableName }],
|
|
564
|
+
// };
|
|
565
|
+
// const requestHandler = async (req, res, next) => {
|
|
566
|
+
// const [result] = await db.query(queryState.queryString, queryState.variables);
|
|
567
|
+
// };
|
|
568
|
+
// const chainActions = {
|
|
569
|
+
// query: (query = "select * from ?", ...variables) => {
|
|
570
|
+
// queryState = { queryString: query, variables };
|
|
571
|
+
// return requestHandler;
|
|
572
|
+
// },
|
|
573
|
+
// };
|
|
574
|
+
// return chainActions;
|
|
575
|
+
// };
|
|
576
|
+
const db = (collectionName, config = configs) => {
|
|
577
|
+
let generatedConfig = {
|
|
578
|
+
dbType: "mongodb",
|
|
579
|
+
path: "../models",
|
|
580
|
+
dbEnv: "DB_URL",
|
|
581
|
+
type: "external",
|
|
582
|
+
...(0, getConfig_1.default)("db"),
|
|
583
|
+
};
|
|
584
|
+
Object.entries(config).forEach(([key, value]) => {
|
|
585
|
+
generatedConfig[key] = value;
|
|
586
|
+
});
|
|
587
|
+
switch (generatedConfig.dbType) {
|
|
588
|
+
case "mongodb":
|
|
589
|
+
return usingMongoDb(collectionName, generatedConfig);
|
|
590
|
+
case "mysql":
|
|
591
|
+
// return usingMySql(collectionName, generatedConfig);
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
exports.default = db;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createUpdateObject = void 0;
|
|
4
|
+
const createUpdateObject = (req, additionalFields) => {
|
|
5
|
+
let updateObject = { $set: { ...req.body } };
|
|
6
|
+
const additionalObject = typeof additionalFields === "function"
|
|
7
|
+
? additionalFields(req)
|
|
8
|
+
: additionalFields;
|
|
9
|
+
if (Object.keys(additionalObject).find((key) => key.startsWith("$")))
|
|
10
|
+
updateObject = { ...updateObject, ...additionalObject };
|
|
11
|
+
else
|
|
12
|
+
updateObject.$set = { ...updateObject.$set, ...additionalObject };
|
|
13
|
+
return updateObject;
|
|
14
|
+
};
|
|
15
|
+
exports.createUpdateObject = createUpdateObject;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validator = exports.uploader = exports.db = exports.auth = void 0;
|
|
7
|
+
const auth_1 = __importDefault(require("./auth/auth"));
|
|
8
|
+
exports.auth = auth_1.default;
|
|
9
|
+
const db_1 = __importDefault(require("./db/db"));
|
|
10
|
+
exports.db = db_1.default;
|
|
11
|
+
const uploader_1 = __importDefault(require("./uploader/uploader"));
|
|
12
|
+
exports.uploader = uploader_1.default;
|
|
13
|
+
const validator_1 = __importDefault(require("./validator/validator"));
|
|
14
|
+
exports.validator = validator_1.default;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import multer from "multer";
|
|
2
|
+
import { NextFunction, Request, Response } from "express";
|
|
3
|
+
declare global {
|
|
4
|
+
namespace Express {
|
|
5
|
+
interface Request {
|
|
6
|
+
mediaId?: any;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
declare const _default: (destination?: string | ((req: Request, file: Express.Multer.File) => string), config?: {
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
saveInDb: boolean;
|
|
13
|
+
prefix: string;
|
|
14
|
+
limit: number;
|
|
15
|
+
format: RegExp;
|
|
16
|
+
path: string;
|
|
17
|
+
}) => {
|
|
18
|
+
single: (fieldName: string) => (req: Request, res: Response, next: NextFunction) => void;
|
|
19
|
+
array: (fieldName: string, maxCount?: number) => (req: Request, res: Response, next: NextFunction) => void;
|
|
20
|
+
fields: (fields: multer.Field[]) => (req: Request, res: Response, next: NextFunction) => void;
|
|
21
|
+
any: () => (req: Request, res: Response, next: NextFunction) => void;
|
|
22
|
+
none: () => (req: Request, res: Response, next: NextFunction) => void;
|
|
23
|
+
} | undefined;
|
|
24
|
+
export default _default;
|