vanta-api 1.0.4 → 1.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/package.json +1 -1
- package/src/api-features.js +18 -8
package/package.json
CHANGED
package/src/api-features.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// api-features.js
|
|
2
1
|
import mongoose from "mongoose";
|
|
3
2
|
import winston from "winston";
|
|
4
3
|
import { securityConfig } from "./config.js";
|
|
@@ -170,8 +169,6 @@ export class ApiFeatures {
|
|
|
170
169
|
});
|
|
171
170
|
});
|
|
172
171
|
|
|
173
|
-
// پشتیبانی از nested populate: در صورت نیاز، منطق تو در تو را میتوانید اینجا اضافه کنید.
|
|
174
|
-
|
|
175
172
|
return this;
|
|
176
173
|
}
|
|
177
174
|
|
|
@@ -184,11 +181,9 @@ export class ApiFeatures {
|
|
|
184
181
|
|
|
185
182
|
async execute(options = {}) {
|
|
186
183
|
try {
|
|
187
|
-
// انتخاب حالت cursor در مواقع پردازش دادههای حجیم
|
|
188
184
|
if (options.useCursor === true) {
|
|
189
185
|
this.useCursor = true;
|
|
190
186
|
}
|
|
191
|
-
// اجرای موازی pipelineهای شمارش و داده
|
|
192
187
|
const [countResult, dataResult] = await Promise.all([
|
|
193
188
|
this.Model.aggregate([...this.countPipeline, { $count: "total" }]),
|
|
194
189
|
(this.useCursor
|
|
@@ -258,7 +253,22 @@ export class ApiFeatures {
|
|
|
258
253
|
|
|
259
254
|
#sanitizeNestedObjects(obj) {
|
|
260
255
|
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
261
|
-
|
|
256
|
+
// Handle ObjectId fields with nested operators
|
|
257
|
+
if (key.endsWith("Id") && typeof value === "object" && !Array.isArray(value)) {
|
|
258
|
+
const sanitizedObj = {};
|
|
259
|
+
for (const [op, val] of Object.entries(value)) {
|
|
260
|
+
if (["$eq", "$ne", "$gt", "$gte", "$lt", "$lte"].includes(op) && mongoose.isValidObjectId(val)) {
|
|
261
|
+
sanitizedObj[op] = new mongoose.Types.ObjectId(val);
|
|
262
|
+
} else if (["$in", "$nin"].includes(op) && Array.isArray(val)) {
|
|
263
|
+
sanitizedObj[op] = val
|
|
264
|
+
.filter(v => mongoose.isValidObjectId(v))
|
|
265
|
+
.map(v => new mongoose.Types.ObjectId(v));
|
|
266
|
+
} else {
|
|
267
|
+
sanitizedObj[op] = val;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
acc[key] = sanitizedObj;
|
|
271
|
+
} else if (typeof value === "object" && !Array.isArray(value)) {
|
|
262
272
|
acc[key] = this.#sanitizeNestedObjects(value);
|
|
263
273
|
} else {
|
|
264
274
|
acc[key] = this.#sanitizeValue(key, value);
|
|
@@ -274,7 +284,7 @@ export class ApiFeatures {
|
|
|
274
284
|
if (typeof value === "string") {
|
|
275
285
|
if (value === "true") return true;
|
|
276
286
|
if (value === "false") return false;
|
|
277
|
-
if (/^\d+$/.test(value)) return parseInt(value);
|
|
287
|
+
if (/^\d+$/.test(value)) return parseInt(value, 10);
|
|
278
288
|
}
|
|
279
289
|
return value;
|
|
280
290
|
}
|
|
@@ -303,4 +313,4 @@ export class ApiFeatures {
|
|
|
303
313
|
}
|
|
304
314
|
}
|
|
305
315
|
|
|
306
|
-
export default ApiFeatures;
|
|
316
|
+
export default ApiFeatures;
|