speedly 1.2.39 → 1.2.42
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/auth.js +12 -8
- package/dist/cjs/db/db.d.ts +3 -3
- package/dist/cjs/db/db.js +158 -49
- package/dist/cjs/document/document.d.ts +2 -0
- package/dist/cjs/document/document.js +185 -0
- package/dist/cjs/index.d.ts +6 -5
- package/dist/cjs/index.js +4 -2
- package/dist/cjs/uploader/uploader.js +0 -5
- package/dist/cjs/validator/validator.js +29 -24
- package/dist/esm/auth/auth.js +12 -8
- package/dist/esm/db/db.d.ts +3 -3
- package/dist/esm/db/db.js +158 -49
- package/dist/esm/document/document.d.ts +2 -0
- package/dist/esm/document/document.js +185 -0
- package/dist/esm/index.d.ts +6 -5
- package/dist/esm/index.js +4 -2
- package/dist/esm/uploader/uploader.js +0 -5
- package/dist/esm/validator/validator.js +29 -24
- package/package.json +4 -1
package/dist/cjs/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.utils = exports.modules = exports.models = exports.validator = exports.uploader = exports.db = exports.auth = void 0;
|
|
6
|
+
exports.document = exports.utils = exports.modules = exports.models = exports.validator = exports.uploader = exports.db = exports.auth = void 0;
|
|
7
7
|
const auth_1 = __importDefault(require("./auth/auth"));
|
|
8
8
|
exports.auth = auth_1.default;
|
|
9
9
|
const db_1 = __importDefault(require("./db/db"));
|
|
@@ -14,9 +14,11 @@ const validator_1 = __importDefault(require("./validator/validator"));
|
|
|
14
14
|
exports.validator = validator_1.default;
|
|
15
15
|
const translation_1 = __importDefault(require("./model/translation"));
|
|
16
16
|
const translation_routes_1 = __importDefault(require("./modules/translation/translation.routes"));
|
|
17
|
+
const document_1 = __importDefault(require("./document/document"));
|
|
18
|
+
exports.document = document_1.default;
|
|
17
19
|
const translator_1 = __importDefault(require("./util/translator"));
|
|
18
20
|
const utils = {
|
|
19
|
-
translator: translator_1.default
|
|
21
|
+
translator: translator_1.default,
|
|
20
22
|
};
|
|
21
23
|
exports.utils = utils;
|
|
22
24
|
const models = { translation: translation_1.default };
|
|
@@ -16,7 +16,6 @@ let configs = {
|
|
|
16
16
|
path: '../../../public',
|
|
17
17
|
...(0, getConfig_1.default)("uploader")
|
|
18
18
|
};
|
|
19
|
-
console.log('uploader', 15, configs);
|
|
20
19
|
exports.default = (destination = "/image", config = configs) => {
|
|
21
20
|
let dest;
|
|
22
21
|
try {
|
|
@@ -33,7 +32,6 @@ exports.default = (destination = "/image", config = configs) => {
|
|
|
33
32
|
let currentPath = path_1.default.join(configs.path);
|
|
34
33
|
splitPath.forEach(folder => {
|
|
35
34
|
currentPath = path_1.default.join(currentPath, folder);
|
|
36
|
-
console.log('uploader', 39, currentPath, !fs_1.default.existsSync(currentPath));
|
|
37
35
|
if (!fs_1.default.existsSync(currentPath)) {
|
|
38
36
|
fs_1.default.mkdirSync(currentPath);
|
|
39
37
|
}
|
|
@@ -54,12 +52,10 @@ exports.default = (destination = "/image", config = configs) => {
|
|
|
54
52
|
const fileName = (configs.prefix ? configs.prefix + "-" : "") +
|
|
55
53
|
originalName.replace(/\.\w+$/, "") + ext;
|
|
56
54
|
const filePath = path_1.default.join(configs.path, dest, fileName);
|
|
57
|
-
console.log('uploader', 65, filePath);
|
|
58
55
|
try {
|
|
59
56
|
fs_1.default.existsSync(filePath);
|
|
60
57
|
}
|
|
61
58
|
catch (error) {
|
|
62
|
-
console.log('uploader', 70, error);
|
|
63
59
|
}
|
|
64
60
|
if (fs_1.default.existsSync(filePath)) {
|
|
65
61
|
return cb(new Error("File already exists in the destination folder"), '');
|
|
@@ -99,7 +95,6 @@ exports.default = (destination = "/image", config = configs) => {
|
|
|
99
95
|
desc: req.body.desc,
|
|
100
96
|
url: "/static/" + dest + "/" + req.file.filename,
|
|
101
97
|
});
|
|
102
|
-
console.log('uploader', 101, mediaDoc);
|
|
103
98
|
req.mediaId = mediaDoc.insertedId;
|
|
104
99
|
}
|
|
105
100
|
req.body[fieldName] = path_1.default
|
|
@@ -1,31 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
// این تابع validator generic هست
|
|
4
|
-
const validator = (schemas) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
const validator = (schemas) => {
|
|
5
|
+
const mw = async (req, res, next) => {
|
|
6
|
+
try {
|
|
7
|
+
if (schemas.body) {
|
|
8
|
+
req.body = (await schemas.body.validate(req.body, {
|
|
9
|
+
stripUnknown: true,
|
|
10
|
+
}));
|
|
11
|
+
}
|
|
12
|
+
if (schemas.params) {
|
|
13
|
+
req.params = (await schemas.params.validate(req.params, {
|
|
14
|
+
stripUnknown: true,
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
if (schemas.query) {
|
|
18
|
+
req.query = (await schemas.query.validate(req.query, {
|
|
19
|
+
stripUnknown: true,
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
next();
|
|
10
23
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
catch (err) {
|
|
25
|
+
return next({
|
|
26
|
+
status: 405,
|
|
27
|
+
json: { message: err.message },
|
|
28
|
+
section: "validation",
|
|
29
|
+
});
|
|
15
30
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
next();
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
return next({
|
|
25
|
-
status: 405,
|
|
26
|
-
json: { message: err.message },
|
|
27
|
-
section: "validation",
|
|
28
|
-
});
|
|
29
|
-
}
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(mw, "__validationSchema", { value: schemas });
|
|
33
|
+
Object.defineProperty(mw, "name", { value: `validator` });
|
|
34
|
+
return mw;
|
|
30
35
|
};
|
|
31
36
|
exports.default = validator;
|
package/dist/esm/auth/auth.js
CHANGED
|
@@ -12,20 +12,24 @@ const gConfig = {
|
|
|
12
12
|
},
|
|
13
13
|
...(0, getConfig_1.default)("auth"),
|
|
14
14
|
};
|
|
15
|
-
const executer = (authType) =>
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const executer = (authType) => {
|
|
16
|
+
const mw = async (req, res, next) => {
|
|
17
|
+
const accessResult = await gConfig?.customValidator?.(req, authType);
|
|
18
|
+
if (accessResult == null)
|
|
19
|
+
return next({ status: 401, json: { message: "Unauthorized" } });
|
|
20
|
+
if (!accessResult)
|
|
21
|
+
return next({ status: 403, json: { message: "Forbidden" } });
|
|
22
|
+
return next();
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(mw, "name", { value: `auth:${authType}` });
|
|
25
|
+
return mw;
|
|
22
26
|
};
|
|
23
27
|
const auth = {
|
|
24
28
|
user: () => {
|
|
25
29
|
return executer("user");
|
|
26
30
|
},
|
|
27
31
|
admin: (config) => {
|
|
28
|
-
return executer(`admin${config?.permission ? `:${config.permission}` :
|
|
32
|
+
return executer(`admin${config?.permission ? `:${config.permission}` : ""}`);
|
|
29
33
|
},
|
|
30
34
|
any: () => {
|
|
31
35
|
return executer("any");
|
package/dist/esm/db/db.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Request, Response, NextFunction } from
|
|
2
|
-
declare module
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
declare module "express-serve-static-core" {
|
|
3
3
|
interface Request {
|
|
4
4
|
document?: any;
|
|
5
5
|
user?: any;
|
|
@@ -13,7 +13,7 @@ type ConfigsType = {
|
|
|
13
13
|
dbType?: string;
|
|
14
14
|
path?: string;
|
|
15
15
|
dbEnv?: string;
|
|
16
|
-
type:
|
|
16
|
+
type: "internal" | "external";
|
|
17
17
|
pagination?: {
|
|
18
18
|
quantity?: number;
|
|
19
19
|
detailed?: boolean;
|
package/dist/esm/db/db.js
CHANGED
|
@@ -11,88 +11,129 @@ const translator_1 = __importDefault(require("../util/translator"));
|
|
|
11
11
|
let configs = {
|
|
12
12
|
dbType: "mongodb",
|
|
13
13
|
path: "../models",
|
|
14
|
-
type:
|
|
14
|
+
type: "external",
|
|
15
15
|
dbEnv: "DB_URL",
|
|
16
16
|
...(0, getConfig_1.default)("db"),
|
|
17
17
|
};
|
|
18
|
-
const usingMongoDb = (collectionName, config = { type:
|
|
18
|
+
const usingMongoDb = (collectionName, config = { type: "external" }) => {
|
|
19
19
|
let model;
|
|
20
20
|
let queryState = {
|
|
21
21
|
queries: [],
|
|
22
22
|
};
|
|
23
23
|
if (config?.path)
|
|
24
24
|
__path = config.path;
|
|
25
|
-
model = require(path_1.default.join(...(config.type ==
|
|
25
|
+
model = require(path_1.default.join(...(config.type == "external"
|
|
26
|
+
? [require.main?.filename || "./", __path]
|
|
27
|
+
: ["../model"]), collectionName));
|
|
26
28
|
if (model.default)
|
|
27
29
|
model = model.default;
|
|
28
30
|
const actionHandler = {
|
|
29
31
|
find: (match = {}) => {
|
|
30
32
|
queryState.action = "find";
|
|
31
33
|
queryState.match = match;
|
|
34
|
+
Object.defineProperty(handler, "name", {
|
|
35
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
36
|
+
});
|
|
32
37
|
return handler;
|
|
33
38
|
},
|
|
34
39
|
create: (body = {}) => {
|
|
35
40
|
queryState.action = "create";
|
|
36
41
|
queryState.body = body;
|
|
42
|
+
Object.defineProperty(handler, "name", {
|
|
43
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
44
|
+
});
|
|
37
45
|
return handler;
|
|
38
46
|
},
|
|
39
47
|
updateOne: (match = {}, body = {}) => {
|
|
40
48
|
queryState.action = "updateOne";
|
|
41
49
|
queryState.match = match;
|
|
42
50
|
queryState.body = body;
|
|
51
|
+
Object.defineProperty(handler, "name", {
|
|
52
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
53
|
+
});
|
|
43
54
|
return handler;
|
|
44
55
|
},
|
|
45
56
|
updateMany: (match = {}, body = {}) => {
|
|
46
57
|
queryState.action = "updateMany";
|
|
47
58
|
queryState.match = match;
|
|
48
59
|
queryState.body = body;
|
|
60
|
+
Object.defineProperty(handler, "name", {
|
|
61
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
62
|
+
});
|
|
49
63
|
return handler;
|
|
50
64
|
},
|
|
51
65
|
deleteOne: (match = {}) => {
|
|
52
66
|
queryState.action = "deleteOne";
|
|
53
67
|
queryState.match = match;
|
|
68
|
+
Object.defineProperty(handler, "name", {
|
|
69
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
70
|
+
});
|
|
54
71
|
return handler;
|
|
55
72
|
},
|
|
56
73
|
deleteMany: (match = {}) => {
|
|
57
74
|
queryState.action = "deleteMany";
|
|
58
75
|
queryState.match = match;
|
|
76
|
+
Object.defineProperty(handler, "name", {
|
|
77
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
78
|
+
});
|
|
59
79
|
return handler;
|
|
60
80
|
},
|
|
61
81
|
findOne: (match = {}) => {
|
|
62
82
|
queryState.action = "findOne";
|
|
63
83
|
queryState.match = match;
|
|
84
|
+
Object.defineProperty(handler, "name", {
|
|
85
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
86
|
+
});
|
|
64
87
|
return handler;
|
|
65
88
|
},
|
|
66
89
|
findOneAndUpdate: (match = {}, body = {}) => {
|
|
67
90
|
queryState.action = "findOneAndUpdate";
|
|
68
91
|
queryState.match = match;
|
|
69
92
|
queryState.body = body;
|
|
93
|
+
Object.defineProperty(handler, "name", {
|
|
94
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
95
|
+
});
|
|
70
96
|
return handler;
|
|
71
97
|
},
|
|
72
98
|
aggregate: (pipeline = []) => {
|
|
73
99
|
queryState.action = "aggregate";
|
|
74
100
|
queryState.pipeline = pipeline;
|
|
101
|
+
Object.defineProperty(handler, "name", {
|
|
102
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
103
|
+
});
|
|
75
104
|
return handler;
|
|
76
105
|
},
|
|
77
106
|
findOneAndDelete: (match = {}) => {
|
|
78
107
|
queryState.action = "findOneAndDelete";
|
|
79
108
|
queryState.match = match;
|
|
109
|
+
Object.defineProperty(handler, "name", {
|
|
110
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
111
|
+
});
|
|
80
112
|
return handler;
|
|
81
113
|
},
|
|
82
114
|
findById: (id = "") => {
|
|
83
115
|
queryState.action = "findById";
|
|
84
116
|
queryState.id = id;
|
|
117
|
+
Object.defineProperty(handler, "name", {
|
|
118
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
119
|
+
});
|
|
85
120
|
return handler;
|
|
86
121
|
},
|
|
87
122
|
findByIdAndUpdate: (id = "", body = {}) => {
|
|
88
123
|
queryState.action = "findByIdAndUpdate";
|
|
89
124
|
queryState.id = id;
|
|
90
125
|
queryState.body = body;
|
|
126
|
+
Object.defineProperty(handler, "name", {
|
|
127
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
128
|
+
});
|
|
91
129
|
return handler;
|
|
92
130
|
},
|
|
93
131
|
findByIdAndDelete: (id = "") => {
|
|
94
132
|
queryState.action = "findByIdAndDelete";
|
|
95
133
|
queryState.id = id;
|
|
134
|
+
Object.defineProperty(handler, "name", {
|
|
135
|
+
value: `model:${queryState.action}:${collectionName}`,
|
|
136
|
+
});
|
|
96
137
|
return handler;
|
|
97
138
|
},
|
|
98
139
|
};
|
|
@@ -137,7 +178,7 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
137
178
|
match = { ...match, ...queryState.match };
|
|
138
179
|
{
|
|
139
180
|
if (req.query.sort && queryState.action !== "aggregate") {
|
|
140
|
-
const sortQueryIndex = realTimeQueries.findIndex((q) =>
|
|
181
|
+
const sortQueryIndex = realTimeQueries.findIndex((q) => "type" in q && q.type == "sort");
|
|
141
182
|
const sortStr = Array.isArray(req.query.sort)
|
|
142
183
|
? req.query.sort[0]
|
|
143
184
|
: req.query.sort;
|
|
@@ -151,63 +192,110 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
151
192
|
realTimeQueries.splice(sortQueryIndex, 1, {
|
|
152
193
|
type: "sort",
|
|
153
194
|
value: {
|
|
154
|
-
...(
|
|
195
|
+
...(realTimeQueries[sortQueryIndex] &&
|
|
196
|
+
"value" in realTimeQueries[sortQueryIndex] &&
|
|
197
|
+
typeof realTimeQueries[sortQueryIndex].value === "object" &&
|
|
198
|
+
realTimeQueries[sortQueryIndex].value !== null
|
|
199
|
+
? realTimeQueries[sortQueryIndex].value
|
|
200
|
+
: {}),
|
|
155
201
|
...sortObject,
|
|
156
202
|
_id: 1,
|
|
157
203
|
},
|
|
158
204
|
});
|
|
159
205
|
}
|
|
160
206
|
if (req.query.page && queryState.action !== "aggregate") {
|
|
161
|
-
const pageStr = Array.isArray(req.query.page)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
207
|
+
const pageStr = Array.isArray(req.query.page)
|
|
208
|
+
? req.query.page[0]
|
|
209
|
+
: typeof req.query.page === "string"
|
|
210
|
+
? req.query.page
|
|
211
|
+
: "";
|
|
212
|
+
const limitStr = Array.isArray(req.query.limit)
|
|
213
|
+
? req.query.limit[0]
|
|
214
|
+
: typeof req.query.limit === "string"
|
|
215
|
+
? req.query.limit
|
|
216
|
+
: "";
|
|
217
|
+
const pageNum = parseInt(typeof pageStr === "string" ? pageStr : "") || 1;
|
|
218
|
+
const limitNum = parseInt(typeof limitStr === "string" ? limitStr : "") ||
|
|
219
|
+
configs?.pagination?.quantity ||
|
|
220
|
+
20;
|
|
221
|
+
if (realTimeQueries.findIndex((q) => "type" in q && q.type == "skip") ==
|
|
222
|
+
-1)
|
|
166
223
|
realTimeQueries.push({
|
|
167
224
|
type: "skip",
|
|
168
225
|
value: (pageNum - 1) * limitNum,
|
|
169
226
|
});
|
|
170
|
-
if (realTimeQueries.findIndex((q) =>
|
|
227
|
+
if (realTimeQueries.findIndex((q) => "type" in q && q.type == "limit") ==
|
|
228
|
+
-1)
|
|
171
229
|
realTimeQueries.push({
|
|
172
230
|
type: "limit",
|
|
173
231
|
value: limitNum,
|
|
174
232
|
});
|
|
175
233
|
}
|
|
176
234
|
else if (req.query.limit && queryState.action !== "aggregate") {
|
|
177
|
-
const pageStrAgg = Array.isArray(req.query.page)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
235
|
+
const pageStrAgg = Array.isArray(req.query.page)
|
|
236
|
+
? req.query.page[0]
|
|
237
|
+
: typeof req.query.page === "string"
|
|
238
|
+
? req.query.page
|
|
239
|
+
: "";
|
|
240
|
+
const limitStrAgg = Array.isArray(req.query.limit)
|
|
241
|
+
? req.query.limit[0]
|
|
242
|
+
: typeof req.query.limit === "string"
|
|
243
|
+
? req.query.limit
|
|
244
|
+
: "";
|
|
245
|
+
const pageNumAgg = parseInt(typeof pageStrAgg === "string" ? pageStrAgg : "") || 1;
|
|
246
|
+
const limitNumAgg = parseInt(typeof limitStrAgg === "string" ? limitStrAgg : "") || 20;
|
|
247
|
+
realTimeQueries.push({ $skip: (pageNumAgg - 1) * limitNumAgg }, {
|
|
248
|
+
$limit: parseInt(Array.isArray(req.query.limit)
|
|
182
249
|
? String(req.query.limit[0])
|
|
183
|
-
: typeof req.query.limit ===
|
|
250
|
+
: typeof req.query.limit === "string"
|
|
184
251
|
? req.query.limit
|
|
185
252
|
: req.query.limit !== undefined
|
|
186
253
|
? String(req.query.limit)
|
|
187
|
-
:
|
|
254
|
+
: ""),
|
|
255
|
+
});
|
|
188
256
|
}
|
|
189
257
|
else if (req.query.limit && queryState.action == "aggregate") {
|
|
190
258
|
realTimeQueries.push({ $limit: parseInt(String(req.query.limit)) });
|
|
191
259
|
}
|
|
192
260
|
if (req.query.select && queryState.action !== "aggregate") {
|
|
193
|
-
const selectQueryIndex = realTimeQueries.findIndex((q) =>
|
|
261
|
+
const selectQueryIndex = realTimeQueries.findIndex((q) => "type" in q && q.type == "select");
|
|
194
262
|
if (selectQueryIndex == -1)
|
|
195
263
|
realTimeQueries.push({ type: "select", value: req.query.select });
|
|
196
264
|
}
|
|
197
265
|
}
|
|
198
266
|
try {
|
|
199
|
-
const page = parseInt(Array.isArray(req.query.page)
|
|
200
|
-
|
|
267
|
+
const page = parseInt(Array.isArray(req.query.page)
|
|
268
|
+
? String(req.query.page[0])
|
|
269
|
+
: typeof req.query.page === "string"
|
|
270
|
+
? req.query.page
|
|
271
|
+
: "") || 1;
|
|
272
|
+
const limit = parseInt(Array.isArray(req.query.limit)
|
|
273
|
+
? String(req.query.limit[0])
|
|
274
|
+
: typeof req.query.limit === "string"
|
|
275
|
+
? req.query.limit
|
|
276
|
+
: req.query.limit !== undefined
|
|
277
|
+
? String(req.query.limit)
|
|
278
|
+
: "") ||
|
|
201
279
|
configs.pagination?.quantity ||
|
|
202
280
|
20;
|
|
203
281
|
switch (queryState.action) {
|
|
204
282
|
case "find":
|
|
205
283
|
if (configs.pagination?.detailed && req.query.page) {
|
|
206
|
-
const page = parseInt(Array.isArray(req.query.page)
|
|
207
|
-
|
|
284
|
+
const page = parseInt(Array.isArray(req.query.page)
|
|
285
|
+
? String(req.query.page[0])
|
|
286
|
+
: typeof req.query.page === "string"
|
|
287
|
+
? req.query.page
|
|
288
|
+
: "") || 1;
|
|
289
|
+
const limit = parseInt(Array.isArray(req.query.limit)
|
|
290
|
+
? String(req.query.limit[0])
|
|
291
|
+
: typeof req.query.limit === "string"
|
|
292
|
+
? req.query.limit
|
|
293
|
+
: req.query.limit !== undefined
|
|
294
|
+
? String(req.query.limit)
|
|
295
|
+
: "") ||
|
|
208
296
|
configs.pagination?.quantity ||
|
|
209
297
|
20;
|
|
210
|
-
console.log(
|
|
298
|
+
console.log("db", 261, model, config.path);
|
|
211
299
|
const totalPages = Math.ceil((await model.countDocuments(match)) / limit);
|
|
212
300
|
detail = {
|
|
213
301
|
...detail,
|
|
@@ -291,17 +379,19 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
291
379
|
{
|
|
292
380
|
$addFields: {
|
|
293
381
|
totalPages: { $ceil: { $divide: ["$count", limit] } },
|
|
294
|
-
page,
|
|
382
|
+
page,
|
|
383
|
+
limit,
|
|
295
384
|
},
|
|
296
385
|
},
|
|
297
386
|
{
|
|
298
|
-
$unset: [
|
|
299
|
-
}
|
|
387
|
+
$unset: ["_id", "count"],
|
|
388
|
+
},
|
|
300
389
|
],
|
|
301
390
|
},
|
|
302
|
-
},
|
|
303
|
-
|
|
304
|
-
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
$unwind: "$detail",
|
|
394
|
+
},
|
|
305
395
|
];
|
|
306
396
|
}
|
|
307
397
|
else {
|
|
@@ -309,16 +399,22 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
309
399
|
{
|
|
310
400
|
$facet: {
|
|
311
401
|
content: [...realTimeQueries],
|
|
312
|
-
}
|
|
313
|
-
}
|
|
402
|
+
},
|
|
403
|
+
},
|
|
314
404
|
];
|
|
315
405
|
}
|
|
316
406
|
// res.status(200).json([...queryState.pipeline , ...realTimeQueries])
|
|
317
407
|
if (typeof queryState.pipeline == "function") {
|
|
318
|
-
data = model?.[queryState.action]?.call(model, [
|
|
408
|
+
data = model?.[queryState.action]?.call(model, [
|
|
409
|
+
...queryState.pipeline(req),
|
|
410
|
+
...realTimeQueries,
|
|
411
|
+
]);
|
|
319
412
|
}
|
|
320
413
|
else {
|
|
321
|
-
data = model?.[queryState.action]?.call(model, [
|
|
414
|
+
data = model?.[queryState.action]?.call(model, [
|
|
415
|
+
...(queryState.pipeline || []),
|
|
416
|
+
...realTimeQueries,
|
|
417
|
+
]);
|
|
322
418
|
}
|
|
323
419
|
break;
|
|
324
420
|
case "findByIdAndUpdate":
|
|
@@ -341,7 +437,7 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
341
437
|
}
|
|
342
438
|
if (queryState.action != "aggregate")
|
|
343
439
|
realTimeQueries.forEach((q) => {
|
|
344
|
-
if (
|
|
440
|
+
if ("type" in q && typeof data?.[q.type] === "function") {
|
|
345
441
|
data = data[q.type].call(data, q.value);
|
|
346
442
|
}
|
|
347
443
|
});
|
|
@@ -357,13 +453,19 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
357
453
|
}
|
|
358
454
|
else {
|
|
359
455
|
if (res.logger && req.user) {
|
|
360
|
-
if (queryState.action &&
|
|
456
|
+
if (queryState.action &&
|
|
457
|
+
queryState.action.match(/create|update|delete/i)?.[0]) {
|
|
361
458
|
res.logger(`${{ log: "لاگها" }[collectionName] ||
|
|
362
459
|
(await (0, translator_1.default)(`${collectionName}`))} ${data.name || data.title || data.id || data._id || ""} توسط ${(req.user?.firstName ? req.user?.firstName + " " : "") +
|
|
363
460
|
(req.user?.lastName || "") ||
|
|
364
|
-
`ادمین با شماره ${req.user.phoneNumber}`} ${{
|
|
461
|
+
`ادمین با شماره ${req.user.phoneNumber}`} ${{
|
|
462
|
+
create: "ایجاد",
|
|
463
|
+
update: "بروزرسانی",
|
|
464
|
+
delete: "حذف",
|
|
465
|
+
nothing: "",
|
|
466
|
+
}[queryState.action
|
|
365
467
|
.match(/create|update|delete/i)?.[0]
|
|
366
|
-
?.toLowerCase() ||
|
|
468
|
+
?.toLowerCase() || "nothing"] || queryState.action} شد`, queryState.action.match(/update/i)?.[0]
|
|
367
469
|
? (await Promise.all(Object.entries(data._doc)
|
|
368
470
|
.filter(([key, value]) => ["_id", "__v", "updatedAt"].includes(key)
|
|
369
471
|
? false
|
|
@@ -390,20 +492,27 @@ const usingMongoDb = (collectionName, config = { type: 'external' }) => {
|
|
|
390
492
|
}
|
|
391
493
|
}
|
|
392
494
|
const action = queryState.action?.match(/create|update|delet/i)?.[0] || "find";
|
|
393
|
-
const resBody = queryState.action ==
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
495
|
+
const resBody = queryState.action == "aggregate"
|
|
496
|
+
? {
|
|
497
|
+
message: config?.message ||
|
|
498
|
+
`the ${collectionName} was found successfully`,
|
|
499
|
+
content: [],
|
|
500
|
+
...data[0],
|
|
501
|
+
}
|
|
502
|
+
: {
|
|
503
|
+
content: data,
|
|
504
|
+
...{ detail: Object.keys(detail).length ? detail : undefined },
|
|
505
|
+
message: config?.message ||
|
|
506
|
+
`the ${collectionName} was ${action == "find" ? "found" : action + "ed"}`,
|
|
507
|
+
};
|
|
402
508
|
res.success ? res.success(200, resBody) : res.status(200).json(resBody);
|
|
403
509
|
}
|
|
404
510
|
}
|
|
405
511
|
catch (err) {
|
|
406
|
-
if (err &&
|
|
512
|
+
if (err &&
|
|
513
|
+
typeof err === "object" &&
|
|
514
|
+
"errorResponse" in err &&
|
|
515
|
+
err.errorResponse?.code == 11000)
|
|
407
516
|
return next({
|
|
408
517
|
status: 405,
|
|
409
518
|
json: {
|
|
@@ -474,7 +583,7 @@ const db = (collectionName, config = configs) => {
|
|
|
474
583
|
dbType: "mongodb",
|
|
475
584
|
path: "../models",
|
|
476
585
|
dbEnv: "DB_URL",
|
|
477
|
-
type:
|
|
586
|
+
type: "external",
|
|
478
587
|
...(0, getConfig_1.default)("db"),
|
|
479
588
|
};
|
|
480
589
|
Object.entries(config).forEach(([key, value]) => {
|