@go-mailer/jarvis 3.2.10 → 3.3.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 (3) hide show
  1. package/README.md +16 -0
  2. package/lib/query.js +35 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -224,6 +224,22 @@ Keywords in request queries are used to prepare the following queries. Given the
224
224
  ```
225
225
  GET https://go-mailer.com?group_by=age
226
226
  ```
227
+ 7. **boolean**: To return items that match the query for boolean fields, the `bool` keyword is used. pre-pending the minus (-) signto the name of the field would evaluate that field to `false` and vice-versa.
228
+
229
+ ```
230
+ GET https://go-mailer.com?bool=age,-name
231
+
232
+ // evaluates to:
233
+ { age: true, name: false }
234
+ ```
235
+ 8. **exists**: To return items based on the existence of certain fields, the `exists` keyword is used. Prepend the minus (-) sign to achieve the opposite.
236
+
237
+ ```
238
+ GET https://go-mailer.com?exists=age,-state
239
+
240
+ // evaluates to
241
+ { age: { $exists: true }, state: { $exists: false } }
242
+ ```
227
243
 
228
244
  ### **Special Characters**
229
245
  The use of special characters in query values indicate the need to prepare the following queries:
package/lib/query.js CHANGED
@@ -2,14 +2,29 @@ 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 seek_conditions = options.bool ? { ...buildBooleanQuery(options.bool || '') } : {}
6
5
  const group_by = options.group_by || null
6
+
7
+ let seek_conditions = {}
8
+ if (options.bool){
9
+ seek_conditions = {
10
+ ...seek_conditions,
11
+ ...buildBooleanQuery(options.bool || '')
12
+ }
13
+ }
14
+
15
+ if (options.exists) {
16
+ seek_conditions = {
17
+ ...seek_conditions,
18
+ ...buildExistsQuery(options.exists || '')
19
+ }
20
+ }
7
21
 
8
22
  const { skip, limit } = determinePagination(options.page, options.population)
9
23
 
10
24
  /** Delete sort and return fields */
11
25
  delete options.bool
12
26
  delete options.count
27
+ delete options.exists
13
28
  delete options.group_by
14
29
  delete options.page
15
30
  delete options.population
@@ -36,12 +51,11 @@ const buildQuery = (options) => {
36
51
  let pipeline = []
37
52
  if (group_by) {
38
53
  pipeline = generateGroupPipeline({
54
+ count,
39
55
  group_by,
40
56
  seek_conditions,
41
57
  fields_to_return,
42
- sort_condition,
43
- skip,
44
- limit
58
+ sort_condition
45
59
  })
46
60
  }
47
61
 
@@ -73,6 +87,23 @@ const buildBooleanQuery = (value) => {
73
87
  }, {})
74
88
  }
75
89
 
90
+ const buildExistsQuery = (value) => {
91
+ const values = value.split(',')
92
+ return values.reduce((sac, val) => {
93
+ let truthiness = true
94
+ let key = val
95
+ if (val[0] === '-') {
96
+ truthiness = false
97
+ key = val.substr(1)
98
+ }
99
+
100
+ return {
101
+ ...sac,
102
+ [key]: { $exists: truthiness }
103
+ }
104
+ }, {})
105
+ }
106
+
76
107
  const buildInQuery = (value) => {
77
108
  const values = value.split(':')
78
109
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "3.2.10",
3
+ "version": "3.3.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>",