@r5v/mongoose-paginate 1.0.0
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 +126 -0
- package/dist/aggregationPagingQuery.d.ts +25 -0
- package/dist/aggregationPagingQuery.d.ts.map +1 -0
- package/dist/aggregationPagingQuery.js +194 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/pagingQuery.d.ts +16 -0
- package/dist/pagingQuery.d.ts.map +1 -0
- package/dist/pagingQuery.js +104 -0
- package/dist/tests/dotNotation.spec.d.ts +2 -0
- package/dist/tests/dotNotation.spec.d.ts.map +1 -0
- package/dist/tests/dotNotation.spec.js +249 -0
- package/dist/tests/findProtectedPaths.spec.d.ts +2 -0
- package/dist/tests/findProtectedPaths.spec.d.ts.map +1 -0
- package/dist/tests/findProtectedPaths.spec.js +11 -0
- package/dist/tests/getPathsWithRef.spec.d.ts +2 -0
- package/dist/tests/getPathsWithRef.spec.d.ts.map +1 -0
- package/dist/tests/getPathsWithRef.spec.js +28 -0
- package/dist/tests/getPropertyFromDotNotation.spec.d.ts +2 -0
- package/dist/tests/getPropertyFromDotNotation.spec.d.ts.map +1 -0
- package/dist/tests/getPropertyFromDotNotation.spec.js +213 -0
- package/dist/tests/insertPopulate.spec.d.ts +2 -0
- package/dist/tests/insertPopulate.spec.d.ts.map +1 -0
- package/dist/tests/insertPopulate.spec.js +64 -0
- package/dist/tests/pagingQuery.spec.d.ts +2 -0
- package/dist/tests/pagingQuery.spec.d.ts.map +1 -0
- package/dist/tests/pagingQuery.spec.js +21 -0
- package/dist/tests/parseSortString.spec.d.ts +2 -0
- package/dist/tests/parseSortString.spec.d.ts.map +1 -0
- package/dist/tests/parseSortString.spec.js +59 -0
- package/dist/utils/dotNotation.d.ts +10 -0
- package/dist/utils/dotNotation.d.ts.map +1 -0
- package/dist/utils/dotNotation.js +129 -0
- package/dist/utils/findKeyWithValue.d.ts +2 -0
- package/dist/utils/findKeyWithValue.d.ts.map +1 -0
- package/dist/utils/findKeyWithValue.js +19 -0
- package/dist/utils/findProtectedPaths.d.ts +3 -0
- package/dist/utils/findProtectedPaths.d.ts.map +1 -0
- package/dist/utils/findProtectedPaths.js +66 -0
- package/dist/utils/getPathsWithRef.d.ts +5 -0
- package/dist/utils/getPathsWithRef.d.ts.map +1 -0
- package/dist/utils/getPathsWithRef.js +111 -0
- package/dist/utils/isJsonString.d.ts +2 -0
- package/dist/utils/isJsonString.d.ts.map +1 -0
- package/dist/utils/isJsonString.js +13 -0
- package/dist/utils/isValidDateString.d.ts +2 -0
- package/dist/utils/isValidDateString.d.ts.map +1 -0
- package/dist/utils/isValidDateString.js +8 -0
- package/dist/utils/parseParams.d.ts +4 -0
- package/dist/utils/parseParams.d.ts.map +1 -0
- package/dist/utils/parseParams.js +49 -0
- package/dist/utils/parsePopulateQuery.d.ts +5 -0
- package/dist/utils/parsePopulateQuery.d.ts.map +1 -0
- package/dist/utils/parsePopulateQuery.js +29 -0
- package/dist/utils/parseSortString.d.ts +6 -0
- package/dist/utils/parseSortString.d.ts.map +1 -0
- package/dist/utils/parseSortString.js +39 -0
- package/package.json +60 -0
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
7
|
+
const parseSortString_1 = require("../utils/parseSortString");
|
|
8
|
+
describe('sortParser', () => {
|
|
9
|
+
const testInputs = [
|
|
10
|
+
"",
|
|
11
|
+
"name",
|
|
12
|
+
"name 1",
|
|
13
|
+
"name -1",
|
|
14
|
+
"name asc",
|
|
15
|
+
"name desc",
|
|
16
|
+
"name, address",
|
|
17
|
+
"name, address 1",
|
|
18
|
+
"name -1, address 1",
|
|
19
|
+
"name -1, address desc",
|
|
20
|
+
"name asc, address desc",
|
|
21
|
+
];
|
|
22
|
+
it('should return empty array', () => {
|
|
23
|
+
const fun = (0, parseSortString_1.parseSortString)("");
|
|
24
|
+
(0, strict_1.default)(Array.isArray(fun));
|
|
25
|
+
(0, strict_1.default)(fun.length === 0);
|
|
26
|
+
});
|
|
27
|
+
it('should return sort value of -1', () => {
|
|
28
|
+
const fun = (0, parseSortString_1.parseSortString)("name -1");
|
|
29
|
+
(0, strict_1.default)(Array.isArray(fun));
|
|
30
|
+
(0, strict_1.default)(Array.length > 0);
|
|
31
|
+
const [r1] = fun;
|
|
32
|
+
strict_1.default.strictEqual(r1[1], -1, 'sortstring is not returning a negative value');
|
|
33
|
+
});
|
|
34
|
+
testInputs.forEach((value) => {
|
|
35
|
+
it('should return a 2d matrix', () => {
|
|
36
|
+
const fun = (0, parseSortString_1.parseSortString)(value);
|
|
37
|
+
(0, strict_1.default)(Array.isArray(fun));
|
|
38
|
+
(0, strict_1.default)(Array.length > 0);
|
|
39
|
+
(0, strict_1.default)(fun.every(item => Array.isArray(item)));
|
|
40
|
+
});
|
|
41
|
+
it('should return sub arrays with length of 2', () => {
|
|
42
|
+
const fun = (0, parseSortString_1.parseSortString)(value);
|
|
43
|
+
(0, strict_1.default)(Array.isArray(fun));
|
|
44
|
+
(0, strict_1.default)(Array.length > 0);
|
|
45
|
+
(0, strict_1.default)(fun.every(item => Array.isArray(item) && item.length === 2), 'Every Item in sub arrays must have a length of 2');
|
|
46
|
+
});
|
|
47
|
+
it('should return sub arrays that have name:sortOrder', () => {
|
|
48
|
+
const fun = (0, parseSortString_1.parseSortString)(value);
|
|
49
|
+
(0, strict_1.default)(Array.isArray(fun));
|
|
50
|
+
(0, strict_1.default)(Array.length > 0);
|
|
51
|
+
(0, strict_1.default)(fun.every(item => {
|
|
52
|
+
return typeof item[0] === 'string';
|
|
53
|
+
}), 'Every Item in sub arrays must be a string');
|
|
54
|
+
(0, strict_1.default)(fun.every(item => {
|
|
55
|
+
return [-1, 1, 'asc', 'desc'].includes(item[1]);
|
|
56
|
+
}), 'Every Item in sub arrays must be one of `-1, 1, asc, desc`');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const getPropertyFromDotNotation: (obj: any, path: string) => any;
|
|
2
|
+
export declare const dotNotationToObject: (dotString: any, value?: any) => {};
|
|
3
|
+
export declare const createObjectFromDotNotation: (dotNotationMap: {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}) => {};
|
|
6
|
+
export declare const setPropertyFromDotNotation: (obj: any, path: any, value: any) => any;
|
|
7
|
+
export declare const setPropertiesFromDotNotation: (obj: any, dotNotationMap: any) => any;
|
|
8
|
+
export declare const setPropertyFromDotNotationImmutable: (obj: any, path: any, value: any) => any;
|
|
9
|
+
export declare const setPropertiesFromDotNotationImmutable: (obj: any, dotNotationMap: any) => any;
|
|
10
|
+
//# sourceMappingURL=dotNotation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dotNotation.d.ts","sourceRoot":"","sources":["../../src/utils/dotNotation.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,0BAA0B,GAAI,KAAI,GAAG,EAAE,MAAK,MAAM,QAI9D,CAAA;AACD,eAAO,MAAM,mBAAmB,GAAI,cAAS,EAAE,QAAM,GAAU,OAoB9D,CAAA;AAGD,eAAO,MAAO,2BAA2B,GAAI,gBAAe;IAAC,CAAC,GAAG,EAAC,MAAM,GAAE,GAAG,CAAA;CAAC,OAwB7E,CAAA;AAED,eAAO,MAAM,0BAA0B,GAAI,QAAG,EAAE,SAAI,EAAE,UAAK,QA4B1D,CAAC;AAGF,eAAO,MAAM,4BAA4B,GAAI,QAAG,EAAE,mBAAc,QAc/D,CAAC;AAGF,eAAO,MAAM,mCAAmC,GAAI,QAAG,EAAE,SAAI,EAAE,UAAK,QA6BnE,CAAC;AAGF,eAAO,MAAM,qCAAqC,GAAI,QAAG,EAAE,mBAAc,QAgBxE,CAAC"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setPropertiesFromDotNotationImmutable = exports.setPropertyFromDotNotationImmutable = exports.setPropertiesFromDotNotation = exports.setPropertyFromDotNotation = exports.createObjectFromDotNotation = exports.dotNotationToObject = exports.getPropertyFromDotNotation = void 0;
|
|
4
|
+
const getPropertyFromDotNotation = (obj, path) => {
|
|
5
|
+
return path.split('.').reduce((currentObject, key) => {
|
|
6
|
+
return currentObject && currentObject[key];
|
|
7
|
+
}, obj);
|
|
8
|
+
};
|
|
9
|
+
exports.getPropertyFromDotNotation = getPropertyFromDotNotation;
|
|
10
|
+
const dotNotationToObject = (dotString, value = null) => {
|
|
11
|
+
const keys = dotString.split('.');
|
|
12
|
+
const result = {};
|
|
13
|
+
let current = result;
|
|
14
|
+
for (let i = 0; i < keys.length; i++) {
|
|
15
|
+
const key = keys[i];
|
|
16
|
+
if (i === keys.length - 1) {
|
|
17
|
+
// Last key, assign the value
|
|
18
|
+
current[key] = value;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
// Not the last key, create nested object
|
|
22
|
+
current[key] = {};
|
|
23
|
+
current = current[key];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
exports.dotNotationToObject = dotNotationToObject;
|
|
29
|
+
const createObjectFromDotNotation = (dotNotationMap) => {
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const [dotString, value] of Object.entries(dotNotationMap)) {
|
|
32
|
+
const keys = dotString.split('.');
|
|
33
|
+
let current = result;
|
|
34
|
+
for (let i = 0; i < keys.length; i++) {
|
|
35
|
+
const key = keys[i];
|
|
36
|
+
if (i === keys.length - 1) {
|
|
37
|
+
// Last key, assign the value
|
|
38
|
+
current[key] = value;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// Not the last key, create nested object if it doesn't exist
|
|
42
|
+
if (!current[key] || typeof current[key] !== 'object') {
|
|
43
|
+
current[key] = {};
|
|
44
|
+
}
|
|
45
|
+
current = current[key];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
exports.createObjectFromDotNotation = createObjectFromDotNotation;
|
|
52
|
+
const setPropertyFromDotNotation = (obj, path, value) => {
|
|
53
|
+
if (!obj || typeof obj !== 'object') {
|
|
54
|
+
throw new Error('Target object must be a valid object');
|
|
55
|
+
}
|
|
56
|
+
if (!path || typeof path !== 'string') {
|
|
57
|
+
throw new Error('Path must be a non-empty string');
|
|
58
|
+
}
|
|
59
|
+
const keys = path.split('.');
|
|
60
|
+
let current = obj;
|
|
61
|
+
// Navigate to the parent of the target property
|
|
62
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
63
|
+
const key = keys[i];
|
|
64
|
+
// If the property doesn't exist or isn't an object, create it
|
|
65
|
+
if (!current[key] || typeof current[key] !== 'object' || Array.isArray(current[key])) {
|
|
66
|
+
current[key] = {};
|
|
67
|
+
}
|
|
68
|
+
current = current[key];
|
|
69
|
+
}
|
|
70
|
+
// Set the final property
|
|
71
|
+
const finalKey = keys[keys.length - 1];
|
|
72
|
+
current[finalKey] = value;
|
|
73
|
+
return obj;
|
|
74
|
+
};
|
|
75
|
+
exports.setPropertyFromDotNotation = setPropertyFromDotNotation;
|
|
76
|
+
// Set multiple properties using dot notation map
|
|
77
|
+
const setPropertiesFromDotNotation = (obj, dotNotationMap) => {
|
|
78
|
+
if (!obj || typeof obj !== 'object') {
|
|
79
|
+
throw new Error('Target object must be a valid object');
|
|
80
|
+
}
|
|
81
|
+
if (!dotNotationMap || typeof dotNotationMap !== 'object') {
|
|
82
|
+
throw new Error('Dot notation map must be a valid object');
|
|
83
|
+
}
|
|
84
|
+
for (const [path, value] of Object.entries(dotNotationMap)) {
|
|
85
|
+
(0, exports.setPropertyFromDotNotation)(obj, path, value);
|
|
86
|
+
}
|
|
87
|
+
return obj;
|
|
88
|
+
};
|
|
89
|
+
exports.setPropertiesFromDotNotation = setPropertiesFromDotNotation;
|
|
90
|
+
const setPropertyFromDotNotationImmutable = (obj, path, value) => {
|
|
91
|
+
if (!obj || typeof obj !== 'object') {
|
|
92
|
+
throw new Error('Target object must be a valid object');
|
|
93
|
+
}
|
|
94
|
+
if (!path || typeof path !== 'string') {
|
|
95
|
+
throw new Error('Path must be a non-empty string');
|
|
96
|
+
}
|
|
97
|
+
const keys = path.split('.');
|
|
98
|
+
const result = JSON.parse(JSON.stringify(obj)); // Deep clone
|
|
99
|
+
let current = result;
|
|
100
|
+
// Navigate to the parent of the target property
|
|
101
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
102
|
+
const key = keys[i];
|
|
103
|
+
// If the property doesn't exist or isn't an object, create it
|
|
104
|
+
if (!current[key] || typeof current[key] !== 'object' || Array.isArray(current[key])) {
|
|
105
|
+
current[key] = {};
|
|
106
|
+
}
|
|
107
|
+
current = current[key];
|
|
108
|
+
}
|
|
109
|
+
// Set the final property
|
|
110
|
+
const finalKey = keys[keys.length - 1];
|
|
111
|
+
current[finalKey] = value;
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
exports.setPropertyFromDotNotationImmutable = setPropertyFromDotNotationImmutable;
|
|
115
|
+
// Set multiple properties using dot notation map (immutable version)
|
|
116
|
+
const setPropertiesFromDotNotationImmutable = (obj, dotNotationMap) => {
|
|
117
|
+
if (!obj || typeof obj !== 'object') {
|
|
118
|
+
throw new Error('Target object must be a valid object');
|
|
119
|
+
}
|
|
120
|
+
if (!dotNotationMap || typeof dotNotationMap !== 'object') {
|
|
121
|
+
throw new Error('Dot notation map must be a valid object');
|
|
122
|
+
}
|
|
123
|
+
const result = JSON.parse(JSON.stringify(obj)); // Deep clone
|
|
124
|
+
for (const [path, value] of Object.entries(dotNotationMap)) {
|
|
125
|
+
(0, exports.setPropertyFromDotNotation)(result, path, value);
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
};
|
|
129
|
+
exports.setPropertiesFromDotNotationImmutable = setPropertiesFromDotNotationImmutable;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findKeyWithValue.d.ts","sourceRoot":"","sources":["../../src/utils/findKeyWithValue.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,GAAI,QAAG,EAAE,cAAS,EAAE,gBAAW,YAe3D,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findKeyWithValue = void 0;
|
|
4
|
+
const findKeyWithValue = (obj, targetKey, targetValue) => {
|
|
5
|
+
for (const key in obj) {
|
|
6
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
7
|
+
if (key === targetKey && obj[key] === targetValue) {
|
|
8
|
+
return true; // Key with matching value found
|
|
9
|
+
}
|
|
10
|
+
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
11
|
+
if ((0, exports.findKeyWithValue)(obj[key], targetKey, targetValue)) {
|
|
12
|
+
return true; // Found in a nested object
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return false; // Not found
|
|
18
|
+
};
|
|
19
|
+
exports.findKeyWithValue = findKeyWithValue;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findProtectedPaths.d.ts","sourceRoot":"","sources":["../../src/utils/findProtectedPaths.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAC,MAAM,UAAU,CAAC;AAE/B,eAAO,MAAM,kBAAkB,GAAI,OAAO,KAAK,CAAC,GAAG,CAAC,KAAG,MAAM,EAsE5D,CAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findProtectedPaths = void 0;
|
|
4
|
+
const findProtectedPaths = (model) => {
|
|
5
|
+
const schema = model.schema;
|
|
6
|
+
const data = [];
|
|
7
|
+
schema.eachPath((p, i) => {
|
|
8
|
+
const str = JSON.stringify(i);
|
|
9
|
+
data.push(str);
|
|
10
|
+
});
|
|
11
|
+
const pathsSet = new Set();
|
|
12
|
+
function buildFullPath(parentPath, childPath) {
|
|
13
|
+
if (!parentPath)
|
|
14
|
+
return childPath;
|
|
15
|
+
if (!childPath)
|
|
16
|
+
return parentPath;
|
|
17
|
+
return `${parentPath}.${childPath}`;
|
|
18
|
+
}
|
|
19
|
+
// Recursive function to traverse nested objects and find selected: false
|
|
20
|
+
function traverseObject(obj, currentPath = '') {
|
|
21
|
+
// Check if current object has selected === false
|
|
22
|
+
if (obj.selected === false && obj.path) {
|
|
23
|
+
const fullPath = buildFullPath(currentPath, obj.path);
|
|
24
|
+
pathsSet.add(fullPath);
|
|
25
|
+
}
|
|
26
|
+
// Check options.selected
|
|
27
|
+
if (obj.options && obj.options.selected === false && obj.path) {
|
|
28
|
+
const fullPath = buildFullPath(currentPath, obj.path);
|
|
29
|
+
pathsSet.add(fullPath);
|
|
30
|
+
}
|
|
31
|
+
// Check for nested paths in complex schema objects
|
|
32
|
+
if (obj.options && obj.options.type && obj.options.type.paths) {
|
|
33
|
+
const nestedPaths = obj.options.type.paths;
|
|
34
|
+
const parentPath = obj.path || currentPath;
|
|
35
|
+
for (const [key, nestedObj] of Object.entries(nestedPaths)) {
|
|
36
|
+
if (nestedObj.selected === false) {
|
|
37
|
+
const fullPath = buildFullPath(parentPath, nestedObj.path);
|
|
38
|
+
pathsSet.add(fullPath);
|
|
39
|
+
}
|
|
40
|
+
// Recursively traverse nested objects
|
|
41
|
+
traverseObject(nestedObj, parentPath);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Check other nested structures that might contain paths
|
|
45
|
+
if (obj.options && obj.options.type && typeof obj.options.type === 'object') {
|
|
46
|
+
const parentPath = obj.path || currentPath;
|
|
47
|
+
traverseObject(obj.options.type, parentPath);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Iterate through each JSON string in the array
|
|
51
|
+
for (const jsonString of data) {
|
|
52
|
+
try {
|
|
53
|
+
// Parse the JSON string
|
|
54
|
+
const obj = JSON.parse(jsonString);
|
|
55
|
+
// Start traversal from root
|
|
56
|
+
traverseObject(obj);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error('Error parsing JSON:', error);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Convert Set back to Array and return
|
|
64
|
+
return Array.from(pathsSet);
|
|
65
|
+
};
|
|
66
|
+
exports.findProtectedPaths = findProtectedPaths;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPathsWithRef.d.ts","sourceRoot":"","sources":["../../src/utils/getPathsWithRef.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,GAAE,SAAI;UACT,MAAM;aAAW,GAAG;GAoH5C,CAAA"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPathsWithRef = void 0;
|
|
4
|
+
const getPathsWithRef = (data) => {
|
|
5
|
+
const results = [];
|
|
6
|
+
const processedPaths = new Set(); // To avoid duplicates
|
|
7
|
+
// Helper function to build full dot notation path
|
|
8
|
+
function buildFullPath(parentPath, childPath) {
|
|
9
|
+
if (!parentPath)
|
|
10
|
+
return childPath;
|
|
11
|
+
if (!childPath)
|
|
12
|
+
return parentPath;
|
|
13
|
+
return `${parentPath}.${childPath}`;
|
|
14
|
+
}
|
|
15
|
+
// Function to check if options object contains ref
|
|
16
|
+
function hasRefInOptions(options) {
|
|
17
|
+
if (!options || typeof options !== 'object')
|
|
18
|
+
return false;
|
|
19
|
+
// Direct ref property
|
|
20
|
+
if (options.ref)
|
|
21
|
+
return true;
|
|
22
|
+
// Check in nested type array
|
|
23
|
+
if (Array.isArray(options.type)) {
|
|
24
|
+
return options.type.some(item => item && typeof item === 'object' && item.ref);
|
|
25
|
+
}
|
|
26
|
+
// Check in nested type object
|
|
27
|
+
if (options.type && typeof options.type === 'object' && options.type.ref) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
// Recursive function to traverse nested objects and find ref options
|
|
33
|
+
function traverseObject(obj, currentPath = '') {
|
|
34
|
+
// Check if current object has options with ref
|
|
35
|
+
if (obj.path && obj.options && hasRefInOptions(obj.options)) {
|
|
36
|
+
const fullPath = buildFullPath(currentPath, obj.path);
|
|
37
|
+
if (!processedPaths.has(fullPath)) {
|
|
38
|
+
processedPaths.add(fullPath);
|
|
39
|
+
results.push({
|
|
40
|
+
path: fullPath,
|
|
41
|
+
options: obj.options
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Check caster for array types (like books array)
|
|
46
|
+
if (obj.caster && obj.caster.options && hasRefInOptions(obj.caster.options)) {
|
|
47
|
+
const fullPath = buildFullPath(currentPath, obj.path || obj.caster.path);
|
|
48
|
+
if (!processedPaths.has(fullPath)) {
|
|
49
|
+
processedPaths.add(fullPath);
|
|
50
|
+
results.push({
|
|
51
|
+
path: fullPath,
|
|
52
|
+
options: obj.caster.options
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Check $embeddedSchemaType for array types
|
|
57
|
+
if (obj.$embeddedSchemaType && obj.$embeddedSchemaType.options && hasRefInOptions(obj.$embeddedSchemaType.options)) {
|
|
58
|
+
const fullPath = buildFullPath(currentPath, obj.path || obj.$embeddedSchemaType.path);
|
|
59
|
+
if (!processedPaths.has(fullPath)) {
|
|
60
|
+
processedPaths.add(fullPath);
|
|
61
|
+
results.push({
|
|
62
|
+
path: fullPath,
|
|
63
|
+
options: obj.$embeddedSchemaType.options
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Check for nested paths in complex schema objects
|
|
68
|
+
if (obj.options && obj.options.type && obj.options.type.paths) {
|
|
69
|
+
const nestedPaths = obj.options.type.paths;
|
|
70
|
+
const parentPath = obj.path || currentPath;
|
|
71
|
+
for (const [key, nestedObj] of Object.entries(nestedPaths)) {
|
|
72
|
+
traverseObject(nestedObj, parentPath);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Check other nested structures
|
|
76
|
+
if (obj.options && obj.options.type && typeof obj.options.type === 'object') {
|
|
77
|
+
const parentPath = obj.path || currentPath;
|
|
78
|
+
traverseObject(obj.options.type, parentPath);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// Handle the comma-separated format in the provided data
|
|
82
|
+
let jsonObjects = [];
|
|
83
|
+
if (typeof data === 'string') {
|
|
84
|
+
// Split by comma and clean up each JSON string
|
|
85
|
+
const jsonStrings = data.split(',{').map((str, index) => {
|
|
86
|
+
if (index === 0)
|
|
87
|
+
return str.startsWith(',') ? str.slice(1) : str;
|
|
88
|
+
return '{' + str;
|
|
89
|
+
}).filter(str => str.trim());
|
|
90
|
+
jsonObjects = jsonStrings;
|
|
91
|
+
}
|
|
92
|
+
else if (Array.isArray(data)) {
|
|
93
|
+
jsonObjects = data.map(item => typeof item === 'string' ? item : JSON.stringify(item));
|
|
94
|
+
}
|
|
95
|
+
// Process each JSON object
|
|
96
|
+
for (const jsonString of jsonObjects) {
|
|
97
|
+
try {
|
|
98
|
+
const cleanJsonString = jsonString.trim();
|
|
99
|
+
if (!cleanJsonString)
|
|
100
|
+
continue;
|
|
101
|
+
const obj = JSON.parse(cleanJsonString);
|
|
102
|
+
traverseObject(obj);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error('Error parsing JSON:', error);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return results;
|
|
110
|
+
};
|
|
111
|
+
exports.getPathsWithRef = getPathsWithRef;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isJsonString.d.ts","sourceRoot":"","sources":["../../src/utils/isJsonString.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,YAOvC,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isJsonString = void 0;
|
|
4
|
+
const isJsonString = (str) => {
|
|
5
|
+
try {
|
|
6
|
+
JSON.parse(str);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch (err) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
exports.isJsonString = isJsonString;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isValidDateString.d.ts","sourceRoot":"","sources":["../../src/utils/isValidDateString.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,GAAI,OAAO,MAAM,KAAG,OAKjD,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidDateString = void 0;
|
|
4
|
+
const isValidDateString = (value) => {
|
|
5
|
+
return (!isNaN(Date.parse(value)) &&
|
|
6
|
+
/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|([+-]\d{2}:\d{2})))?$/.test(value));
|
|
7
|
+
};
|
|
8
|
+
exports.isValidDateString = isValidDateString;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ParsedQs } from "qs";
|
|
2
|
+
import type { PagingQueryParsedRequestParams, AggregateQueryParsedRequestParams } from '../index.d';
|
|
3
|
+
export declare const parseParams: (defaultParams: PagingQueryParsedRequestParams | AggregateQueryParsedRequestParams, params: ParsedQs, isAggregate?: boolean) => PagingQueryParsedRequestParams | AggregateQueryParsedRequestParams;
|
|
4
|
+
//# sourceMappingURL=parseParams.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseParams.d.ts","sourceRoot":"","sources":["../../src/utils/parseParams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,IAAI,CAAC;AAC5B,OAAO,KAAK,EAAC,8BAA8B,EAAE,iCAAiC,EAAC,MAAM,YAAY,CAAA;AAGjG,eAAO,MAAM,WAAW,GAAI,eAAe,8BAA8B,GAAG,iCAAiC,EAAE,QAAQ,QAAQ,EAAE,qBAAmB,KAAG,8BAA8B,GAAG,iCA4CvL,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseParams = void 0;
|
|
4
|
+
const parseSortString_1 = require("./parseSortString");
|
|
5
|
+
const parseParams = (defaultParams, params, isAggregate = false) => {
|
|
6
|
+
if (!params) {
|
|
7
|
+
return {};
|
|
8
|
+
}
|
|
9
|
+
const keys = Object.keys(params);
|
|
10
|
+
const __params = structuredClone(defaultParams);
|
|
11
|
+
const parsedParams = Object.assign({}, defaultParams);
|
|
12
|
+
keys.forEach((k) => {
|
|
13
|
+
if (__params.hasOwnProperty(k)) {
|
|
14
|
+
if (k === "$filter" || k === "$postFilter") {
|
|
15
|
+
parsedParams[k] =
|
|
16
|
+
typeof params[k] === "string"
|
|
17
|
+
? JSON.parse(params[k])
|
|
18
|
+
: params[k];
|
|
19
|
+
}
|
|
20
|
+
if (k === "$limit" || k === "$skip") {
|
|
21
|
+
parsedParams[k] = parseInt(params[k].toString());
|
|
22
|
+
}
|
|
23
|
+
if (k === "$paging") {
|
|
24
|
+
const str = params[k].toLowerCase();
|
|
25
|
+
parsedParams[k] = str === 'false' || str === 'no' || str == '0';
|
|
26
|
+
}
|
|
27
|
+
if (k === "$sort" || k === "$preSort") {
|
|
28
|
+
parsedParams[k] = isAggregate ? (0, parseSortString_1.parseAggregateSortString)(params[k]) : (0, parseSortString_1.parseSortString)(params[k]);
|
|
29
|
+
}
|
|
30
|
+
if (k === "$count" || k === "$includes" || k === "$populate") {
|
|
31
|
+
const v = params[k].split(",").map((v) => v.trim());
|
|
32
|
+
if (k === "$includes") {
|
|
33
|
+
parsedParams["$populate"] = v;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
parsedParams[k] = v;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (k === "$select") {
|
|
40
|
+
parsedParams[k] = params[k];
|
|
41
|
+
}
|
|
42
|
+
if (k === "$lean") {
|
|
43
|
+
parsedParams[k] = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return parsedParams;
|
|
48
|
+
};
|
|
49
|
+
exports.parseParams = parseParams;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parsePopulateQuery.d.ts","sourceRoot":"","sources":["../../src/utils/parsePopulateQuery.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,GAAI,aAAa,MAAM,EAAE,EAAE,WAAW,MAAM,EAAE;UAmB1D,MAAM;aAAW,GAAG;GAGtC,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parsePopulateArray = void 0;
|
|
4
|
+
const parsePopulateArray = (populateArr, selectArr) => {
|
|
5
|
+
return populateArr.reduce((acc, curr) => {
|
|
6
|
+
const matched = selectArr.filter(item => {
|
|
7
|
+
const rgx = new RegExp(`^${curr}($|\.?)`, "g");
|
|
8
|
+
return rgx.test(item);
|
|
9
|
+
});
|
|
10
|
+
const pop = { path: curr };
|
|
11
|
+
matched.forEach(item => {
|
|
12
|
+
const str = item.slice(item.indexOf(".") + 1);
|
|
13
|
+
if (str === curr) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const currSelect = {};
|
|
17
|
+
if (str.startsWith("-")) {
|
|
18
|
+
currSelect[str.slice(1).trim()] = 0;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
currSelect[str.trim()] = 1;
|
|
22
|
+
}
|
|
23
|
+
pop["select"] = Object.assign(Object.assign({}, pop["select"]), currSelect);
|
|
24
|
+
});
|
|
25
|
+
acc.push(pop);
|
|
26
|
+
return acc;
|
|
27
|
+
}, []);
|
|
28
|
+
};
|
|
29
|
+
exports.parsePopulateArray = parsePopulateArray;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SortOrder } from "mongoose";
|
|
2
|
+
export declare const parseSortString: (sortString: string) => [string, SortOrder][];
|
|
3
|
+
export declare const parseAggregateSortString: (sortString: any) => {
|
|
4
|
+
[key: string]: SortOrder;
|
|
5
|
+
};
|
|
6
|
+
//# sourceMappingURL=parseSortString.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseSortString.d.ts","sourceRoot":"","sources":["../../src/utils/parseSortString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,eAAO,MAAM,eAAe,GAAI,YAAY,MAAM,0BAyBjD,CAAA;AAED,eAAO,MAAM,wBAAwB,GAAI,eAAU,KAAE;IAAC,CAAC,GAAG,EAAC,MAAM,GAAE,SAAS,CAAA;CAO3E,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseAggregateSortString = exports.parseSortString = void 0;
|
|
4
|
+
const parseSortString = (sortString) => {
|
|
5
|
+
return sortString
|
|
6
|
+
.split(",") //["_id 1","abc -1"]
|
|
7
|
+
.map((item) => {
|
|
8
|
+
const [k, v] = item.trim().replace(/ +/g, " ").split(" "); //["_id","1"]
|
|
9
|
+
if (!k && !v) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
if (k && !v) {
|
|
13
|
+
return [k.trim(), 1];
|
|
14
|
+
}
|
|
15
|
+
let _order;
|
|
16
|
+
if (isNaN(Number(v))) {
|
|
17
|
+
_order = v.trim();
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
_order = Number(v);
|
|
21
|
+
}
|
|
22
|
+
const order = [-1, 1, 'asc', 'desc'].includes(_order) ? _order : 1;
|
|
23
|
+
return [
|
|
24
|
+
k.trim(),
|
|
25
|
+
order
|
|
26
|
+
];
|
|
27
|
+
})
|
|
28
|
+
.filter((v) => v !== null);
|
|
29
|
+
};
|
|
30
|
+
exports.parseSortString = parseSortString;
|
|
31
|
+
const parseAggregateSortString = (sortString) => {
|
|
32
|
+
const sortObj = (0, exports.parseSortString)(sortString);
|
|
33
|
+
return sortObj.reduce((a, c) => {
|
|
34
|
+
const [k, v] = c;
|
|
35
|
+
a[k] = v;
|
|
36
|
+
return a;
|
|
37
|
+
}, {});
|
|
38
|
+
};
|
|
39
|
+
exports.parseAggregateSortString = parseAggregateSortString;
|