-
Notifications
You must be signed in to change notification settings - Fork 14
/
where.js
57 lines (50 loc) · 3.12 KB
/
where.js
1
2
3
4
5
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
/*
Example of where() API
---------------------
where() method can take three parameters: 'key', 'op', 'val'
* `key` -- the property name of the data. Or you can pass a Function here to group multiple query inside it. See details in [example]('./examples/where.js')
* `val` -- value to be matched with. It can be a _int_, _string_, _bool_ or even _Function_ - depending on the `op`.
* `op` -- operand to be used for matching. The following operands are available to use:
* `=` : For weak equality matching
* `eq` : Same as `=`
* `!=` : For weak not equality matching
* `neq` : Same as `!=`
* `==` : For strict equality matching
* `seq` : Same as `==`
* `!==` : For strict not equality matching
* `sneq` : Same as `!==`
* `>` : Check if value of given **key** in data is Greater than **val**
* `gt` : Same as `>`
* `<` : Check if value of given **key** in data is Less than **val**
* `lt` : Same as `<`
* `>=` : Check if value of given **key** in data is Greater than or Equal of **val**
* `gte` : Same as `>=`
* `<=` : Check if value of given **key** in data is Less than or Equal of **val**
* `lte` : Same as `<=`
* `null` : Check if the value of given **key** in data is **null** (`val` parameter in `where()` can be omitted for this `op`)
* `notnull` : Check if the value of given **key** in data is **not null** (`val` parameter in `where()` can be omitted for this `op`)
* `in` : Check if the value of given **key** in data is exists in given **val**. **val** should be a plain _Array_.
* `notin` : Check if the value of given **key** in data is not exists in given **val**. **val** should be a plain _Array_.
* `startswith` : Check if the value of given **key** in data starts with (has a prefix of) the given **val**. This would only works for _String_ type data.
* `endswith` : Check if the value of given **key** in data ends with (has a suffix of) the given **val**. This would only works for _String_ type data.
* `contains` : Check if the value of given **key** in data has a substring of given **val**. This would only works for _String_ type data.
* `match` : Check if the value of given **key** in data has a Regular Expression match with the given **val**. The `val` parameter should be a **RegExp** for this `op`.
* `macro` : It would try to match the value of given **key** in data executing the given `val`. The `val` parameter should be a **Function** for this `op`. This function should have a matching logic inside it and return **true** or **false** based on that.
*/
const jsonQ = require('../index.js');
let Q = new jsonQ(__dirname + '/data.json');
//To get a direct property 'users' from Json
const users = Q.from('users')
.where('id', '=', 1)
.fetch();
console.log('-------- Printing Users ---------');
console.log(users);
// Resetting the instance to initial state
// because the previous where() query changed it internal state
Q = Q.reset();
const multipleWhere = Q.from('users')
.where('id', '=', 1)
.where('location', '=', 'Sylhet')
.fetch();
console.log('-------- Printing Result of multiple where ---------');
console.log(multipleWhere);