@go-mailer/jarvis 4.0.4 → 4.2.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.
Files changed (2) hide show
  1. package/lib/query.js +40 -25
  2. package/package.json +1 -1
package/lib/query.js CHANGED
@@ -2,10 +2,13 @@ const buildQuery = (options) => {
2
2
  const sort_condition = options.sort_by ? buildSortOrderString(options.sort_by) : ''
3
3
  const fields_to_return = options.return_only ? buildReturnFieldsString(options.return_only) : ''
4
4
  const count = options.count || false
5
- const group_by = options.group_by || null
6
5
 
6
+ const group_by = options.group_by
7
+ const sum = options.sum
8
+ const is_pipeline = group_by || sum
9
+
7
10
  let seek_conditions = {}
8
- if (options.bool){
11
+ if (options.bool) {
9
12
  seek_conditions = {
10
13
  ...seek_conditions,
11
14
  ...buildBooleanQuery(options.bool || '')
@@ -13,7 +16,7 @@ const buildQuery = (options) => {
13
16
  }
14
17
 
15
18
  if (options.exists) {
16
- seek_conditions = {
19
+ seek_conditions = {
17
20
  ...seek_conditions,
18
21
  ...buildExistsQuery(options.exists || '')
19
22
  }
@@ -30,6 +33,7 @@ const buildQuery = (options) => {
30
33
  delete options.population
31
34
  delete options.return_only
32
35
  delete options.sort_by
36
+ delete options.sum
33
37
 
34
38
  Object.keys(options).forEach((field) => {
35
39
  const field_value = options[field] ? options[field].toString().toLowerCase() : ''
@@ -49,10 +53,11 @@ const buildQuery = (options) => {
49
53
  })
50
54
 
51
55
  let pipeline = []
52
- if (group_by) {
53
- pipeline = generateGroupPipeline({
56
+ if (is_pipeline) {
57
+ pipeline = generatePipeline({
54
58
  count,
55
59
  group_by,
60
+ sum,
56
61
  seek_conditions,
57
62
  fields_to_return,
58
63
  sort_condition
@@ -107,21 +112,21 @@ const buildExistsQuery = (value) => {
107
112
  const buildInQuery = (value) => {
108
113
  const values = value.split(':')
109
114
  return {
110
- $in: [...values]
115
+ $in: [...values].map(val => isNaN(val) ? val : Number(val))
111
116
  }
112
117
  }
113
118
 
114
119
  const buildNorQuery = (value) => {
115
120
  const values = value.split('!')
116
121
  return {
117
- $nin: [...values.slice(1)]
122
+ $nin: [...values.slice(1)].map(val => isNaN(val) ? val : Number(val))
118
123
  }
119
124
  }
120
125
 
121
126
  const buildOrQuery = (value) => {
122
127
  const values = value.split(',')
123
128
  return {
124
- $in: [...values]
129
+ $in: [...values].map(val => isNaN(val) ? val : Number(val))
125
130
  }
126
131
  }
127
132
 
@@ -166,17 +171,14 @@ const determinePagination = (page = 0, population) => {
166
171
  return pagination
167
172
  }
168
173
 
169
- const generateGroupPipeline = ({ group_by, seek_conditions, sort_condition, fields_to_return, count }) => {
170
- if (!group_by || typeof group_by !== 'string') return null
171
-
174
+ const generatePipeline = ({ group_by, seek_conditions, sort_condition, sum, fields_to_return, count }) => {
172
175
  const pipeline = []
173
- const group_field = group_by.split(',')[0]
174
176
 
175
177
  pipeline.push({
176
178
  $match: { ...seek_conditions }
177
179
  })
178
-
179
- if (sort_condition){
180
+
181
+ if (sort_condition) {
180
182
  const sort_options = sort_condition.split(' ').reduce((sac, condition) => {
181
183
  let key = condition
182
184
  let value = 1
@@ -184,7 +186,7 @@ const generateGroupPipeline = ({ group_by, seek_conditions, sort_condition, fiel
184
186
  key = key.split('-')[1]
185
187
  value = -1
186
188
  }
187
-
189
+
188
190
  return { ...sac, [key]: value }
189
191
  }, {})
190
192
 
@@ -192,27 +194,40 @@ const generateGroupPipeline = ({ group_by, seek_conditions, sort_condition, fiel
192
194
  $sort: { ...sort_options }
193
195
  })
194
196
  }
195
-
196
- if (fields_to_return){
197
+
198
+ if (fields_to_return) {
197
199
  const projection_options = fields_to_return.split(' ').reduce((sac, condition) => ({ ...sac, [condition]: 1 }), {})
198
200
 
199
201
  pipeline.push({
200
202
  $project: { ...projection_options }
201
203
  })
202
204
  }
203
-
204
- pipeline.push({
205
- $group: {
206
- _id: { group_field: `$${group_field}`}
207
- }
208
- })
205
+
206
+ if (group_by) {
207
+ const group_field = group_by.split(',')[0]
208
+ pipeline.push({
209
+ $group: {
210
+ _id: { group_field: `$${group_field}` }
211
+ }
212
+ })
213
+ }
214
+
215
+ if (sum) {
216
+ const sum_field = sum.split(',')[0]
217
+ pipeline.push({
218
+ $group: {
219
+ _id: { tenant_id: "$tenant_id" },
220
+ [sum_field]: { $sum: `$${sum_field}` }
221
+ }
222
+ })
223
+ }
209
224
 
210
225
  if (count) {
211
226
  pipeline.push({
212
227
  $count: "size"
213
228
  })
214
229
  }
215
-
230
+
216
231
  return pipeline
217
232
  }
218
233
 
@@ -226,5 +241,5 @@ module.exports = {
226
241
  buildSortOrderString,
227
242
  buildWildcardOptions,
228
243
  determinePagination,
229
- generateGroupPipeline
244
+ generatePipeline
230
245
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "4.0.4",
3
+ "version": "4.2.0",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:go-mailer-ltd/jarvis-node.git",
6
6
  "author": "Nathan Oguntuberu <nateoguns.work@gmail.com>",