@livequery/mongoose 2.0.22 → 2.0.23
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/build/src/MongoQuery.js +51 -55
- package/package.json +1 -1
package/build/src/MongoQuery.js
CHANGED
|
@@ -92,23 +92,24 @@ export class MongoQuery {
|
|
|
92
92
|
return stack.pop();
|
|
93
93
|
}
|
|
94
94
|
static #parse_summary(req) {
|
|
95
|
-
const
|
|
95
|
+
const parsed = Object
|
|
96
96
|
.entries(req.options)
|
|
97
|
-
.map(([
|
|
98
|
-
if (!
|
|
97
|
+
.map(([key, v], index) => {
|
|
98
|
+
if (!key.startsWith('::'))
|
|
99
99
|
return [];
|
|
100
100
|
const exprs = `${v}`.split('|').filter(Boolean);
|
|
101
101
|
const fns = exprs.map(l => {
|
|
102
102
|
const exp = l.split('(')[0];
|
|
103
|
-
if (!['sum', 'avg', 'max', 'min', 'count'].includes(exp))
|
|
103
|
+
if (!['sum', 'avg', 'max', 'min', 'count', 'distinct'].includes(exp))
|
|
104
104
|
return [];
|
|
105
|
+
if (exp == 'count' || exp == 'distinct')
|
|
106
|
+
return [{ key: exp, query: { $sum: 1 } }];
|
|
105
107
|
const infix = l.split('(')?.[1]?.split(')')?.[0];
|
|
106
108
|
if (!infix)
|
|
107
109
|
return [];
|
|
108
|
-
|
|
109
|
-
return [{ exp, infix, query: { $sum: 1 } }];
|
|
110
|
+
const key = `${exp}_${infix}`;
|
|
110
111
|
const query = this.#postfix_to_mongodb(this.#infix_to_postfix(infix));
|
|
111
|
-
return [{
|
|
112
|
+
return [{ key, query: { [`$${exp}`]: query } }];
|
|
112
113
|
}).flat(2);
|
|
113
114
|
const groups = exprs.filter(g => g.match(/^[a-zA-Z_]+$/));
|
|
114
115
|
const $match = exprs.map(exp => {
|
|
@@ -127,42 +128,39 @@ export class MongoQuery {
|
|
|
127
128
|
}
|
|
128
129
|
}
|
|
129
130
|
}).filter(Boolean);
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
...p,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const pipelines = [...expresisons].reduce((p, [group_by, { $match, ...$group }]) => {
|
|
154
|
-
return {
|
|
155
|
-
...p,
|
|
156
|
-
[group_by]: [
|
|
157
|
-
...$match.length > 0 ? [{ $match }] : [],
|
|
158
|
-
{ $group },
|
|
131
|
+
const is_distinc_count = `${v}`.includes('distinc');
|
|
132
|
+
const simple = is_distinc_count ? key : (exprs.length == 1 ? fns[0].key : false);
|
|
133
|
+
const pipelines = [
|
|
134
|
+
...$match.length > 0 ? [{ $match }] : [],
|
|
135
|
+
{
|
|
136
|
+
$group: {
|
|
137
|
+
_id: groups.length == 0 ? null : groups.reduce((p, by) => {
|
|
138
|
+
return {
|
|
139
|
+
...p,
|
|
140
|
+
[by]: `$${by}`
|
|
141
|
+
};
|
|
142
|
+
}, {}),
|
|
143
|
+
...fns.reduce((p, { query, key }) => ({
|
|
144
|
+
...p,
|
|
145
|
+
[key]: query
|
|
146
|
+
}), {})
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
...is_distinc_count ? [
|
|
150
|
+
{
|
|
151
|
+
$count: key
|
|
152
|
+
}
|
|
153
|
+
] : [
|
|
159
154
|
{
|
|
160
155
|
$project: {
|
|
161
|
-
...
|
|
156
|
+
...groups.reduce((p, c) => ({
|
|
162
157
|
...p,
|
|
163
158
|
[c]: `$_id.${c}`
|
|
164
159
|
}), {}),
|
|
165
|
-
...
|
|
160
|
+
...fns.reduce((p, { key }) => ({
|
|
161
|
+
...p,
|
|
162
|
+
[key]: 1
|
|
163
|
+
}), {}),
|
|
166
164
|
_id: 0
|
|
167
165
|
}
|
|
168
166
|
},
|
|
@@ -170,20 +168,23 @@ export class MongoQuery {
|
|
|
170
168
|
$limit: 50
|
|
171
169
|
}
|
|
172
170
|
]
|
|
171
|
+
];
|
|
172
|
+
return [{ key, pipelines, simple }];
|
|
173
|
+
})
|
|
174
|
+
.flat(1);
|
|
175
|
+
const pipelines = parsed.reduce((p, { key, pipelines }) => ({
|
|
176
|
+
...p,
|
|
177
|
+
[key]: pipelines
|
|
178
|
+
}), {});
|
|
179
|
+
const summary = parsed.length == 0 ? undefined : parsed.reduce((p, { key, simple }) => {
|
|
180
|
+
return {
|
|
181
|
+
...p,
|
|
182
|
+
[key]: simple ? { $arrayElemAt: [`$${key}.${simple}`, 0] } : `$${key}`
|
|
173
183
|
};
|
|
174
184
|
}, {});
|
|
175
|
-
const summary = expresisons.size == 0 ? {} : {
|
|
176
|
-
summary: [...expresisons].reduce((p, [group_by, $group]) => {
|
|
177
|
-
return {
|
|
178
|
-
...p,
|
|
179
|
-
[group_by]: `$${group_by}`
|
|
180
|
-
};
|
|
181
|
-
}, {})
|
|
182
|
-
};
|
|
183
185
|
return {
|
|
184
186
|
pipelines,
|
|
185
|
-
summary
|
|
186
|
-
mappers
|
|
187
|
+
summary
|
|
187
188
|
};
|
|
188
189
|
}
|
|
189
190
|
static #parse_conditions(filters) {
|
|
@@ -314,7 +315,7 @@ export class MongoQuery {
|
|
|
314
315
|
];
|
|
315
316
|
}
|
|
316
317
|
static #build_cursor_paging($sort, req) {
|
|
317
|
-
const { pipelines,
|
|
318
|
+
const { pipelines, summary } = this.#parse_summary(req);
|
|
318
319
|
const limit = this.#get_limit(req);
|
|
319
320
|
return [
|
|
320
321
|
{
|
|
@@ -326,12 +327,7 @@ export class MongoQuery {
|
|
|
326
327
|
},
|
|
327
328
|
{
|
|
328
329
|
$project: {
|
|
329
|
-
summary
|
|
330
|
-
return {
|
|
331
|
-
...p,
|
|
332
|
-
[c.id]: c.groups.length == 0 ? { $arrayElemAt: [`$${c.id}.value`, 0] } : `$${c.id}`
|
|
333
|
-
};
|
|
334
|
-
}, {}),
|
|
330
|
+
summary,
|
|
335
331
|
prev: {
|
|
336
332
|
$ifNull: [
|
|
337
333
|
{ $arrayElemAt: ["$prev", 0] },
|
package/package.json
CHANGED