@ewb/mongoose-query 1.0.0 → 1.0.2
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 +24 -35
- package/lib/Query.d.ts +60 -77
- package/lib/Query.js +227 -307
- package/lib/QueryError.d.ts +16 -50
- package/lib/QueryError.js +52 -62
- package/lib/QueryHandler.d.ts +47 -59
- package/lib/QueryHandler.js +250 -297
- package/lib/QueryHistory.d.ts +75 -76
- package/lib/QueryHistory.js +137 -154
- package/lib/QueryModel.d.ts +65 -80
- package/lib/QueryModel.js +214 -201
- package/lib/index.d.ts +11 -10
- package/lib/index.js +8 -32
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/Query.utils.d.ts +49 -0
- package/lib/utils/Query.utils.js +143 -0
- package/lib/{constants.d.ts → utils/constants.d.ts} +1 -2
- package/lib/utils/constants.js +1 -0
- package/lib/utils/types.d.ts +11 -0
- package/lib/utils/types.js +1 -0
- package/lib/utils/utils.d.ts +8 -0
- package/lib/{utils.js → utils/utils.js} +38 -44
- package/package.json +4 -4
- package/lib/ExpressHandler.d.ts +0 -30
- package/lib/ExpressHandler.js +0 -215
- package/lib/ExpressHandler.js.map +0 -1
- package/lib/ModelQuery.d.ts +0 -24
- package/lib/ModelQuery.js +0 -80
- package/lib/ModelQuery.js.map +0 -1
- package/lib/Query.js.map +0 -1
- package/lib/QueryError.js.map +0 -1
- package/lib/QueryExpressHandler.d.ts +0 -50
- package/lib/QueryExpressHandler.js +0 -383
- package/lib/QueryExpressHandler.js.map +0 -1
- package/lib/QueryExpressSubHandler.d.ts +0 -20
- package/lib/QueryExpressSubHandler.js +0 -137
- package/lib/QueryExpressSubHandler.js.map +0 -1
- package/lib/QueryHandler.js.map +0 -1
- package/lib/QueryHistory.js.map +0 -1
- package/lib/QueryModel.js.map +0 -1
- package/lib/constants.js +0 -6
- package/lib/constants.js.map +0 -1
- package/lib/declarations.d.ts +0 -16
- package/lib/declarations.js +0 -3
- package/lib/declarations.js.map +0 -1
- package/lib/error/ExpressError.d.ts +0 -44
- package/lib/error/ExpressError.js +0 -39
- package/lib/error/ExpressError.js.map +0 -1
- package/lib/error/QueryError.d.ts +0 -47
- package/lib/error/QueryError.js +0 -50
- package/lib/error/QueryError.js.map +0 -1
- package/lib/error/QueryExpressError.d.ts +0 -46
- package/lib/error/QueryExpressError.js +0 -39
- package/lib/error/QueryExpressError.js.map +0 -1
- package/lib/error/V1Error.d.ts +0 -12
- package/lib/error/V1Error.js +0 -27
- package/lib/error/V1Error.js.map +0 -1
- package/lib/error/error.handler.d.ts +0 -3
- package/lib/error/error.handler.js +0 -30
- package/lib/error/error.handler.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/statics.d.ts +0 -17
- package/lib/statics.js +0 -35
- package/lib/statics.js.map +0 -1
- package/lib/utils.d.ts +0 -3
- package/lib/utils.js.map +0 -1
package/lib/QueryModel.js
CHANGED
|
@@ -1,201 +1,214 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { DEFAULT_LIMIT } from './utils/constants';
|
|
3
|
+
export const toValidSelectRegexp = (select) => new RegExp(`^${String(select)}(\..|$)`);
|
|
4
|
+
export const toValidSortRegexp = (sort) => new RegExp(`^(\\+|-)?(${String(sort)}|text)$`);
|
|
5
|
+
export const toValidTextRegexp = (str) => typeof str === 'string'
|
|
6
|
+
? new RegExp(str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i')
|
|
7
|
+
: '';
|
|
8
|
+
export class QueryModel {
|
|
9
|
+
constructor(opts = {}) {
|
|
10
|
+
this.opts = opts;
|
|
11
|
+
}
|
|
12
|
+
get publicFields() {
|
|
13
|
+
return this.opts.editFields;
|
|
14
|
+
}
|
|
15
|
+
get privateFields() {
|
|
16
|
+
return this.opts.privateFields;
|
|
17
|
+
}
|
|
18
|
+
get populate() {
|
|
19
|
+
return this.opts.populate;
|
|
20
|
+
}
|
|
21
|
+
get subs() {
|
|
22
|
+
return this.opts.subs;
|
|
23
|
+
}
|
|
24
|
+
get updateDateKey() {
|
|
25
|
+
return this.opts.updateDateKey;
|
|
26
|
+
}
|
|
27
|
+
get query() {
|
|
28
|
+
return this.opts.query;
|
|
29
|
+
}
|
|
30
|
+
createSearchQuery(query) {
|
|
31
|
+
return query.createQuery({ noRoot: false, query: this.opts.query });
|
|
32
|
+
}
|
|
33
|
+
setPopulate(modelQuery, query) {
|
|
34
|
+
let populate = [];
|
|
35
|
+
if (query.populate.length > 0 && this.opts.populate) {
|
|
36
|
+
populate = this.getValidPopulate(query.populate).map((x) => ({
|
|
37
|
+
...x,
|
|
38
|
+
select: x.select.join(' '),
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
if (query.rootPopulate.length > 0) {
|
|
42
|
+
populate.push(...query.rootPopulate);
|
|
43
|
+
}
|
|
44
|
+
if (populate.length > 0) {
|
|
45
|
+
modelQuery.populate(populate);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
getValidPopulate(queryPopulate, sub) {
|
|
49
|
+
let checkPop = this.opts.populate;
|
|
50
|
+
if (this.opts.subs && sub) {
|
|
51
|
+
const found = this.opts.subs.find((x) => x.sub === sub);
|
|
52
|
+
if (!found || !found.populate || found.populate.length === 0)
|
|
53
|
+
return [];
|
|
54
|
+
checkPop = found.populate;
|
|
55
|
+
}
|
|
56
|
+
if (!checkPop)
|
|
57
|
+
return [];
|
|
58
|
+
return queryPopulate
|
|
59
|
+
.map((pop) => {
|
|
60
|
+
const found = checkPop.find((x) => x.path === pop.path);
|
|
61
|
+
if (!found)
|
|
62
|
+
return null;
|
|
63
|
+
const select = pop.select?.filter((x) => found.select.includes(x)) || [];
|
|
64
|
+
return {
|
|
65
|
+
path: pop.path,
|
|
66
|
+
select: select.length > 0 ? select : found.select,
|
|
67
|
+
...(found.populate ? { populate: found.populate } : {}),
|
|
68
|
+
};
|
|
69
|
+
})
|
|
70
|
+
.filter(Boolean);
|
|
71
|
+
}
|
|
72
|
+
setLimitAndSkip(modelQuery, query) {
|
|
73
|
+
if (query.export)
|
|
74
|
+
return;
|
|
75
|
+
modelQuery.skip(query.skip ?? 0);
|
|
76
|
+
modelQuery.limit(query.limit ?? this.opts.limit ?? DEFAULT_LIMIT);
|
|
77
|
+
}
|
|
78
|
+
setSelect(modelQuery, query, overrideValid) {
|
|
79
|
+
const valids = overrideValid || this.opts.select;
|
|
80
|
+
let selects = valids || [];
|
|
81
|
+
if (valids && valids.length > 0 && query.select.length > 0) {
|
|
82
|
+
selects = query.select.filter((select) => valids.some((valid) => String(select).match(toValidSelectRegexp(valid))));
|
|
83
|
+
}
|
|
84
|
+
else if (this.opts.defaultSelect) {
|
|
85
|
+
selects = this.opts.defaultSelect;
|
|
86
|
+
}
|
|
87
|
+
if (selects.length > 0) {
|
|
88
|
+
modelQuery.select(selects.join(' '));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
setSort(modelQuery, query) {
|
|
92
|
+
if (query.sort.length > 0) {
|
|
93
|
+
if (this.opts.sort) {
|
|
94
|
+
const validSorts = query.sort.filter((x) => this.opts.sort.some((y) => String(x).match(toValidSortRegexp(y))));
|
|
95
|
+
modelQuery.sort(sortArrayToSortObject(validSorts, query.hasTextSearch));
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
modelQuery.sort(sortArrayToSortObject(query.sort, query.hasTextSearch));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
setProjection(modelQuery, query) {
|
|
103
|
+
if (query.projection) {
|
|
104
|
+
modelQuery.projection(query.projection);
|
|
105
|
+
query.projection = undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
getSubPopulateLookup(query, sub) {
|
|
109
|
+
if (!this.opts.subs)
|
|
110
|
+
return [];
|
|
111
|
+
const validPopulate = this.opts.subs.find((x) => x.sub === sub);
|
|
112
|
+
if (!validPopulate)
|
|
113
|
+
return [];
|
|
114
|
+
const populate = query.populate;
|
|
115
|
+
const $lookups = [];
|
|
116
|
+
if (populate.length > 0) {
|
|
117
|
+
for (const pop of populate) {
|
|
118
|
+
const found = validPopulate.populate.find((x) => x.path === pop.path);
|
|
119
|
+
if (!found)
|
|
120
|
+
continue;
|
|
121
|
+
const select = pop.select
|
|
122
|
+
? found.select.filter((x) => pop.select.includes(String(x)))
|
|
123
|
+
: found.select;
|
|
124
|
+
$lookups.push(...[
|
|
125
|
+
{
|
|
126
|
+
$lookup: {
|
|
127
|
+
from: found.lookupRef || `${pop.path}s`,
|
|
128
|
+
localField: `${String(sub)}.${pop.path}`,
|
|
129
|
+
foreignField: '_id',
|
|
130
|
+
as: pop.path,
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
...(pop.select
|
|
134
|
+
? [
|
|
135
|
+
{
|
|
136
|
+
$project: {
|
|
137
|
+
_id: 1,
|
|
138
|
+
[sub]: 1,
|
|
139
|
+
count: 1,
|
|
140
|
+
[pop.path]: select.reduce((obj, item) => ({ ...obj, [item]: 1 }), {}),
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
]
|
|
144
|
+
: []),
|
|
145
|
+
]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return $lookups;
|
|
149
|
+
}
|
|
150
|
+
getSubFilterConditionAndMatch(subQuery, sub) {
|
|
151
|
+
const cond = {};
|
|
152
|
+
let $match = {};
|
|
153
|
+
for (const key in subQuery) {
|
|
154
|
+
const v = subQuery[key];
|
|
155
|
+
if (key !== '$and' && !Array.isArray(v)) {
|
|
156
|
+
const $and = [];
|
|
157
|
+
const [newItem, toMatch] = queryToSubFilter(key, subQuery[key]);
|
|
158
|
+
if (newItem.length > 0) {
|
|
159
|
+
$and.push(...newItem);
|
|
160
|
+
}
|
|
161
|
+
$match = { ...$match, ...toMatch };
|
|
162
|
+
cond['$and'] = $and;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const $filter = { input: `$${String(sub)}`, as: 'subdoc', cond };
|
|
166
|
+
return { $filter, $match };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function queryToSubFilter(key, value) {
|
|
170
|
+
if (!value)
|
|
171
|
+
return [[], {}];
|
|
172
|
+
if (typeof value === 'object') {
|
|
173
|
+
if ('$in' in value && value.$in) {
|
|
174
|
+
return [
|
|
175
|
+
[{ $in: [`$$subdoc.${String(key)}`, value.$in.map(toCorrectIdValue)] }],
|
|
176
|
+
{},
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
if ('$nin' in value && value.$nin) {
|
|
180
|
+
return [
|
|
181
|
+
value.$nin.map((x) => ({
|
|
182
|
+
$ne: [`$$subdoc.${String(key)}`, toCorrectIdValue(x)],
|
|
183
|
+
})),
|
|
184
|
+
{},
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return [[], { [key]: toCorrectIdValue(value) }];
|
|
189
|
+
}
|
|
190
|
+
function toCorrectIdValue(v) {
|
|
191
|
+
if (!v)
|
|
192
|
+
return v;
|
|
193
|
+
if (mongoose.isValidObjectId(v))
|
|
194
|
+
return new mongoose.Types.ObjectId(v);
|
|
195
|
+
return v;
|
|
196
|
+
}
|
|
197
|
+
function sortArrayToSortObject(arr, hasText) {
|
|
198
|
+
const sortObj = {};
|
|
199
|
+
for (const item of arr) {
|
|
200
|
+
if (item === 'text' && hasText) {
|
|
201
|
+
sortObj['score'] = { $meta: 'textScore' };
|
|
202
|
+
}
|
|
203
|
+
else if (item[0] === '-') {
|
|
204
|
+
sortObj[item.substr(1)] = -1;
|
|
205
|
+
}
|
|
206
|
+
else if (item[0] === '+') {
|
|
207
|
+
sortObj[item.substr(1)] = 1;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
sortObj[item] = 1;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return sortObj;
|
|
214
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
1
|
+
export * from './QueryError';
|
|
2
|
+
export * from './Query';
|
|
3
|
+
export * from './QueryHandler';
|
|
4
|
+
export * from './QueryHistory';
|
|
5
|
+
export * from './QueryModel';
|
|
6
|
+
export * from './utils/Query.utils';
|
|
7
|
+
export * from './utils/types';
|
|
8
|
+
export { toValidNumber } from './utils/Query.utils';
|
|
9
|
+
export { ISearchPaginate } from './utils/utils';
|
|
10
|
+
export { IQueryAddToModelQuery } from './utils/types';
|
|
11
|
+
export { IQueryOptions } from './utils/types';
|
package/lib/index.js
CHANGED
|
@@ -1,32 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
require("./declarations");
|
|
14
|
-
const Query_1 = require("./Query");
|
|
15
|
-
__exportStar(require("./QueryError"), exports);
|
|
16
|
-
__exportStar(require("./QueryExpressHandler"), exports);
|
|
17
|
-
__exportStar(require("./Query"), exports);
|
|
18
|
-
__exportStar(require("./QueryHandler"), exports);
|
|
19
|
-
__exportStar(require("./QueryHistory"), exports);
|
|
20
|
-
__exportStar(require("./QueryModel"), exports);
|
|
21
|
-
__exportStar(require("./statics"), exports);
|
|
22
|
-
function mongooseQuery(req, _res, next) {
|
|
23
|
-
try {
|
|
24
|
-
req.mongooseQuery = new Query_1.Query(req.query);
|
|
25
|
-
next();
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
next(e);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
exports.default = mongooseQuery;
|
|
32
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export * from './QueryError';
|
|
2
|
+
export * from './Query';
|
|
3
|
+
export * from './QueryHandler';
|
|
4
|
+
export * from './QueryHistory';
|
|
5
|
+
export * from './QueryModel';
|
|
6
|
+
export * from './utils/Query.utils';
|
|
7
|
+
export * from './utils/types';
|
|
8
|
+
export { toValidNumber } from './utils/Query.utils';
|