@livequery/mongoose 2.0.21 → 2.0.22

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.
@@ -94,47 +94,67 @@ export class MongoQuery {
94
94
  static #parse_summary(req) {
95
95
  const mappers = Object
96
96
  .entries(req.options)
97
- .map(([k, v]) => {
97
+ .map(([k, v], index) => {
98
98
  if (!k.startsWith('::'))
99
99
  return [];
100
- const [a, ...groups] = `${v}`.split('|');
101
- const [expr, infix] = a.split(')')[0].split('(');
102
- if (!['sum', 'avg', 'max', 'min', 'count'].includes(expr))
103
- return [];
104
- const query = this.#postfix_to_mongodb(this.#infix_to_postfix(infix));
105
- const group_by = groups.length == 0 ? 'NO_GROUP' : groups.join('|');
100
+ const exprs = `${v}`.split('|').filter(Boolean);
101
+ const fns = exprs.map(l => {
102
+ const exp = l.split('(')[0];
103
+ if (!['sum', 'avg', 'max', 'min', 'count'].includes(exp))
104
+ return [];
105
+ const infix = l.split('(')?.[1]?.split(')')?.[0];
106
+ if (!infix)
107
+ return [];
108
+ if (exp == 'count')
109
+ return [{ exp, infix, query: { $sum: 1 } }];
110
+ const query = this.#postfix_to_mongodb(this.#infix_to_postfix(infix));
111
+ return [{ exp, infix, query: { [`$${exp}`]: query } }];
112
+ }).flat(2);
113
+ const groups = exprs.filter(g => g.match(/^[a-zA-Z_]+$/));
114
+ const $match = exprs.map(exp => {
115
+ for (const { c, f } of [
116
+ { c: '==', f: 'eq' },
117
+ { c: '<>', f: 'ne' },
118
+ { c: '>=', f: 'gte' },
119
+ { c: '<=', f: 'lte' },
120
+ { c: '>', f: 'gt' },
121
+ { c: '<', f: 'lt' },
122
+ { c: '=', f: 'eq' },
123
+ ]) {
124
+ if (exp.includes(c)) {
125
+ const [a, b] = exp.split(c);
126
+ return { [a]: { [f]: isNaN(Number(b)) ? (c == '==' ? (b == 'true') : b) : Number(b) } };
127
+ }
128
+ }
129
+ }).filter(Boolean);
106
130
  return [{
107
- id: Buffer.from(k, 'utf-8').toString('hex'),
108
- group_id: Buffer.from(group_by, 'utf-8').toString('hex'),
109
- name: k.split('::')[1] || '#',
110
- expr: expr.toLowerCase(),
111
- query,
131
+ id: k.split('::')[1] || `summary_${index}`,
132
+ fns,
112
133
  groups,
113
- group_by
134
+ $match
114
135
  }];
115
- })
116
- .flat(2);
117
- const expresisons = mappers.reduce((p, { expr, query, groups, group_id, id }) => {
118
- p.set(group_id, {
119
- ...p.has(group_id) ? p.get(group_id) : {
120
- _id: groups.length == 0 ? null : groups.reduce((p, by) => {
121
- return {
122
- ...p,
123
- [by]: `$${by}`
124
- };
125
- }, {}),
126
- },
127
- ...expr == 'count' ? { [id]: { $sum: 1 } } : {},
128
- ...expr == 'max' ? { [id]: { $max: query } } : {},
129
- ...expr == 'min' ? { [id]: { $min: query } } : {},
130
- ...expr == 'sum' ? { [id]: { $sum: query } } : {}
136
+ }).flat(1);
137
+ const expresisons = mappers.reduce((p, { fns, groups, id, $match }) => {
138
+ p.set(id, {
139
+ $match,
140
+ _id: groups.length == 0 ? null : groups.reduce((p, by) => {
141
+ return {
142
+ ...p,
143
+ [by]: `$${by}`
144
+ };
145
+ }, {}),
146
+ ...fns.reduce((p, { infix, exp, query }) => ({
147
+ ...p,
148
+ [`${exp}_${infix}`]: query
149
+ }), {})
131
150
  });
132
151
  return p;
133
152
  }, new Map);
134
- const pipelines = [...expresisons].reduce((p, [group_by, $group]) => {
153
+ const pipelines = [...expresisons].reduce((p, [group_by, { $match, ...$group }]) => {
135
154
  return {
136
155
  ...p,
137
156
  [group_by]: [
157
+ ...$match.length > 0 ? [{ $match }] : [],
138
158
  { $group },
139
159
  {
140
160
  $project: {
@@ -294,7 +314,7 @@ export class MongoQuery {
294
314
  ];
295
315
  }
296
316
  static #build_cursor_paging($sort, req) {
297
- const { pipelines, summary, mappers } = this.#parse_summary(req);
317
+ const { pipelines, mappers } = this.#parse_summary(req);
298
318
  const limit = this.#get_limit(req);
299
319
  return [
300
320
  {
@@ -306,7 +326,12 @@ export class MongoQuery {
306
326
  },
307
327
  {
308
328
  $project: {
309
- ...summary,
329
+ summary: mappers.length == 0 ? 1 : mappers.reduce((p, c) => {
330
+ return {
331
+ ...p,
332
+ [c.id]: c.groups.length == 0 ? { $arrayElemAt: [`$${c.id}.value`, 0] } : `$${c.id}`
333
+ };
334
+ }, {}),
310
335
  prev: {
311
336
  $ifNull: [
312
337
  { $arrayElemAt: ["$prev", 0] },
@@ -323,12 +348,7 @@ export class MongoQuery {
323
348
  },
324
349
  {
325
350
  $project: {
326
- summary: mappers.length == 0 ? 1 : mappers.reduce((p, c) => {
327
- return {
328
- ...p,
329
- [c.name]: { $arrayElemAt: [`$summary.${c.group_id}.${c.id}`, 0] }
330
- };
331
- }, {}),
351
+ summary: 1,
332
352
  items: {
333
353
  $concatArrays: ["$prev.items", "$next.items"]
334
354
  },
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "repository": {
7
7
  "url": "git@github.com:livequery/mongoose.git"
8
8
  },
9
- "version": "2.0.21",
9
+ "version": "2.0.22",
10
10
  "description": "Mongoose datasource mapping for @livequery ecosystem",
11
11
  "main": "./build/src/index.js",
12
12
  "types": "./build/src/index.d.ts",