This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mongo-utils.js
246 lines (217 loc) · 6.18 KB
/
mongo-utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const _ = require('lodash');
const GROUPS = ['$and', '$or'];
/**
* Maps over a mongo query, calling `fn` on each non-operator object
* using the return value as the new query object at that level/layer
*/
const mapQuery = (query, fn) => {
if (Array.isArray(query)) {
return query.map(obj => mapQuery(obj, fn))
// Allow removal of empty children from lists
.filter(obj => !_.isEmpty(obj));
}
return _.reduce(query, (modifiedNQLMongoJSON, value, key) => {
let mappedObject;
if (GROUPS.includes(key)) {
const mappedValue = mapQuery(value, fn);
// Allow removal of parent with empty children
mappedObject = _.isEmpty(mappedValue) ? null : {
[key]: mappedValue
};
} else {
mappedObject = fn(value, key);
}
return _.assign({}, modifiedNQLMongoJSON, mappedObject);
}, {});
};
/**
* Combines two filters with $and conjunction
*/
const combineFilters = (primary, secondary) => {
if (_.isEmpty(primary)) {
return secondary;
}
if (_.isEmpty(secondary)) {
return primary;
}
return {
$and: [primary, secondary]
};
};
const findStatement = (statements, match) => {
return _.some(statements, (value, key, obj) => {
if (key === '$and') {
return findStatement(obj.$and, match);
} else if (key === '$or') {
return findStatement(obj.$or, match);
} else {
if ((key !== match) && _.isObject(value)) {
return findStatement(value, match);
} else {
return (key === match);
}
}
});
};
/**
* ## Reject statements
*
* Removes statements keys when matching `func` returns true
* in the primary filter, e.g.:
*
* In NQL results equivalent to:
* ('featured:true', 'featured:false') => ''
* ('featured:true', 'featured:false,status:published') => 'status:published'
*/
const rejectStatements = (statements, func) => {
if (!statements) {
return statements;
}
return mapQuery(statements, function (value, key) {
if (func(key)) {
return;
}
return {
[key]: value
};
});
};
/**
* ## Merge Filters
* Util to combine multiple filters based on the priority how
* they are passed into the method. For example:
* mergeFilter(overrides, custom, defaults);
* would merge these three filters having overrides on highers priority
* and defaults on the lowest priority
*/
const mergeFilters = (...filters) => {
let merged = {};
filters
.filter(filter => (!!filter)) // CASE: remove empty arguments if any
.forEach((filter) => {
if (filter && Object.keys(filter).length > 0) {
filter = rejectStatements(filter, (statement) => {
return findStatement(merged, statement);
});
if (filter) {
merged = merged ? combineFilters(merged, filter) : filter;
}
}
});
return merged;
};
/**
* ## Expand Filters
* Util that expands Mongo JSON statements with custom statements
*/
const expandFilters = (statements, expansions) => {
const expand = (primary, secondary) => {
// CASE: we don't want to have separate $and groups when expanding
// all statements should be withing the same group
if (secondary.$and) {
return {$and: [
primary,
...secondary.$and
]};
}
return {$and: [
primary,
secondary
]};
};
return mapQuery(statements, function (value, key) {
const expansion = _.find(expansions, {key});
if (!expansion) {
return {
[key]: value
};
}
let replaced = {
[expansion.replacement]: value
};
if (expansion.expansion) {
return expand(replaced, expansion.expansion);
}
return replaced;
});
};
/**
* @typedef {object} Mapping
*
* @prop {any} from
* @prop {any} to
*/
/**
* @typedef {object} KeyValueMapping
*
* @prop {Mapping} key
* @prop {Mapping[]} values
*/
/**
* Returns the replace value for `input`, or just `input` if there is no replacement
*
* @param {any} input
* @param {Mapping[]} valueMappings
*
* @returns {any}
*/
function replaceValue(input, valueMappings) {
const replacer = valueMappings.find(({from}) => from === input);
return replacer ? replacer.to : input;
}
/**
* Returns the result of calling fn on an item or each item in an array
*/
function fmap(item, fn) {
return Array.isArray(item) ? item.map(fn) : fn(item);
}
/**
* @typedef {Object} Query
*/
/**
* Returns a transformer which can be passed into nql
*
* @param {KeyValueMapping} mapping
*
* @returns {(input: Query) => Query}
*/
function mapKeyValues(mapping) {
/**
* @param {Query} input
*/
return function transformer(input) {
return mapQuery(input, function (value, key) {
// Passthrough on anything that doesn't match our mapping
if (key !== mapping.key.from) {
return {
[key]: value
};
}
// Primitive query of the form "key: value"
if (typeof value !== 'object') {
return {
[mapping.key.to]: replaceValue(value, mapping.values)
};
}
// Complex query of the form "key: { $in: [value, value] }" or "key: { $ne: value }"
return {
[mapping.key.to]: _.reduce(value, (updatedQuery, objValue, objKey) => {
// objKey = $in | $ne | etc...
// objValue = vallue | [value, value] | etc...
return Object.assign(updatedQuery, {
[objKey]: fmap(objValue, item => replaceValue(item, mapping.values))
});
}, {})
};
});
};
}
module.exports = {
mapKeyValues,
combineFilters,
findStatement,
rejectStatements,
mergeFilters,
expandFilters,
mapQuery
};