feathers-utils 1.6.0 → 1.7.2
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.
- package/dist/esm/hooks/checkMulti.d.ts +2 -0
- package/dist/esm/hooks/checkMulti.js +18 -0
- package/dist/esm/hooks/runPerItem.d.ts +4 -0
- package/dist/esm/hooks/runPerItem.js +25 -0
- package/dist/esm/hooks/setData.d.ts +4 -0
- package/dist/esm/hooks/setData.js +33 -0
- package/dist/esm/index.d.ts +30 -0
- package/dist/esm/index.js +31 -0
- package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +15 -0
- package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +46 -0
- package/dist/esm/mixins/debounce-mixin/index.d.ts +5 -0
- package/dist/esm/mixins/debounce-mixin/index.js +20 -0
- package/dist/esm/types.d.ts +59 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/utils/addHook.d.ts +3 -0
- package/dist/esm/utils/addHook.js +36 -0
- package/dist/esm/utils/filterQuery.d.ts +3 -0
- package/dist/esm/utils/filterQuery.js +28 -0
- package/dist/esm/utils/getPaginate.d.ts +2 -0
- package/dist/esm/utils/getPaginate.js +14 -0
- package/dist/esm/utils/isMulti.d.ts +2 -0
- package/dist/esm/utils/isMulti.js +17 -0
- package/dist/esm/utils/isPaginated.d.ts +2 -0
- package/dist/esm/utils/isPaginated.js +8 -0
- package/dist/esm/utils/markHookForSkip.d.ts +3 -0
- package/dist/esm/utils/markHookForSkip.js +14 -0
- package/dist/esm/utils/mergeQuery/index.d.ts +3 -0
- package/dist/esm/utils/mergeQuery/index.js +327 -0
- package/dist/esm/utils/mergeQuery/mergeArrays.d.ts +2 -0
- package/dist/esm/utils/mergeQuery/mergeArrays.js +37 -0
- package/dist/esm/utils/pushSet.d.ts +2 -0
- package/dist/esm/utils/pushSet.js +19 -0
- package/dist/esm/utils/setResultEmpty.d.ts +2 -0
- package/dist/esm/utils/setResultEmpty.js +25 -0
- package/dist/esm/utils/shouldSkip.d.ts +2 -0
- package/dist/esm/utils/shouldSkip.js +29 -0
- package/dist/hooks/runPerItem.d.ts +2 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +7 -1
- package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +2 -2
- package/dist/utils/getPaginate.d.ts +2 -0
- package/dist/utils/getPaginate.js +15 -0
- package/dist/utils/isMulti.js +2 -3
- package/dist/utils/isPaginated.d.ts +2 -0
- package/dist/utils/isPaginated.js +12 -0
- package/dist/utils/setResultEmpty.d.ts +2 -0
- package/dist/utils/setResultEmpty.js +29 -0
- package/package.json +15 -9
- package/src/hooks/runPerItem.ts +2 -1
- package/src/index.ts +3 -0
- package/src/mixins/debounce-mixin/DebouncedStore.ts +2 -2
- package/src/utils/getPaginate.ts +18 -0
- package/src/utils/isMulti.ts +2 -3
- package/src/utils/isPaginated.ts +12 -0
- package/src/utils/setResultEmpty.ts +28 -0
- package/tsconfig-esm.json +9 -0
- package/.github/workflows/node.js.yml +0 -45
- package/.vscode/launch.json +0 -29
- package/.vscode/settings.json +0 -14
- package/test/hooks/checkMulti.test.ts +0 -121
- package/test/hooks/setData.test.ts +0 -333
- package/test/mixins/debounce-mixin.test.ts +0 -174
- package/test/utils/addHook.test.ts +0 -307
- package/test/utils/isMulti.test.ts +0 -53
- package/test/utils/markHookForSkip.test.ts +0 -292
- package/test/utils/mergeQuery.test.ts +0 -407
- package/test/utils/pushSet.test.ts +0 -66
- package/test/utils/shouldSkip.test.ts +0 -67
|
@@ -1,407 +0,0 @@
|
|
|
1
|
-
import assert from"assert";
|
|
2
|
-
import { mergeQuery } from "../../src";
|
|
3
|
-
import feathers from "@feathersjs/feathers";
|
|
4
|
-
import { Service } from "feathers-memory";
|
|
5
|
-
|
|
6
|
-
describe("util - mergeQuery", function() {
|
|
7
|
-
describe("general", function() {
|
|
8
|
-
it("$limit: -1", function() {
|
|
9
|
-
const query = mergeQuery({ $limit: -1 }, { id: 1 });
|
|
10
|
-
assert.deepStrictEqual(query, { $limit: -1, id: 1 });
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
describe("simple objects passing", function() {
|
|
15
|
-
const app = feathers();
|
|
16
|
-
app.use(
|
|
17
|
-
"/service",
|
|
18
|
-
new Service(
|
|
19
|
-
{
|
|
20
|
-
paginate: { default: 10, max: 100 },
|
|
21
|
-
whitelist: ["$and"]
|
|
22
|
-
}
|
|
23
|
-
)
|
|
24
|
-
);
|
|
25
|
-
const service = app.service("/service");
|
|
26
|
-
const passingPairs = {
|
|
27
|
-
"empty": {
|
|
28
|
-
target: {},
|
|
29
|
-
source: {},
|
|
30
|
-
options: { useLogicalConjunction: false },
|
|
31
|
-
expected: {}
|
|
32
|
-
},
|
|
33
|
-
"target": {
|
|
34
|
-
target: { id : 1, test1: true },
|
|
35
|
-
source: { id : 2, test2: false },
|
|
36
|
-
options: { defaultHandle: "target", useLogicalConjunction: false },
|
|
37
|
-
expected: { id: 1, test1: true, test2: false }
|
|
38
|
-
},
|
|
39
|
-
"source": {
|
|
40
|
-
target: { id : 1, test1: true },
|
|
41
|
-
source: { id : 2, test2: false },
|
|
42
|
-
options: { defaultHandle: "source", useLogicalConjunction: false },
|
|
43
|
-
expected: { id: 2, test1: true, test2: false }
|
|
44
|
-
},
|
|
45
|
-
"native booleans": {
|
|
46
|
-
target: { test: true },
|
|
47
|
-
source: { test: false },
|
|
48
|
-
options: { useLogicalConjunction: false },
|
|
49
|
-
expected: { test: false }
|
|
50
|
-
},
|
|
51
|
-
"native nested to boolean": {
|
|
52
|
-
target: { test: { nested: [{ deep: true } ] } },
|
|
53
|
-
source: { test: false },
|
|
54
|
-
options: { useLogicalConjunction: false },
|
|
55
|
-
expected: { test: false }
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
for (const key in passingPairs) {
|
|
59
|
-
const { target, source, options, expected } = passingPairs[key];
|
|
60
|
-
it(`'${key}'`, function() {
|
|
61
|
-
const query = mergeQuery(target, source, Object.assign({ service }, options));
|
|
62
|
-
assert.deepStrictEqual(query, expected, "works as expected");
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe("simple objects passing with handle combine", function() {
|
|
68
|
-
const app = feathers();
|
|
69
|
-
app.use(
|
|
70
|
-
"/service",
|
|
71
|
-
new Service(
|
|
72
|
-
{
|
|
73
|
-
paginate: { default: 10, max: 100 },
|
|
74
|
-
whitelist: ["$and"]
|
|
75
|
-
}
|
|
76
|
-
)
|
|
77
|
-
);
|
|
78
|
-
const service = app.service("/service");
|
|
79
|
-
const passingPairs = {
|
|
80
|
-
"combine two numbers": {
|
|
81
|
-
target: { id : 1 },
|
|
82
|
-
source: { id : 2 },
|
|
83
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
84
|
-
expected: { id: { $in: [1, 2] } }
|
|
85
|
-
},
|
|
86
|
-
"combine $in different types": {
|
|
87
|
-
target: { id : "1" },
|
|
88
|
-
source: { id : 2 },
|
|
89
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
90
|
-
expected: { id: { $in: ["1", 2] } }
|
|
91
|
-
},
|
|
92
|
-
"combine number to $in with overlapping": {
|
|
93
|
-
target: { id: 1 },
|
|
94
|
-
source: { id: { $in: [1, 2] } },
|
|
95
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
96
|
-
expected: { id: { $in: [1, 2] } }
|
|
97
|
-
},
|
|
98
|
-
"combine numbers in $in": {
|
|
99
|
-
target: { id: 1 },
|
|
100
|
-
source: { id: { $in: [2, 3] } },
|
|
101
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
102
|
-
expected: { id: { $in: [2, 3, 1] } }
|
|
103
|
-
},
|
|
104
|
-
"combine two $in": {
|
|
105
|
-
target: { id: { $in: [2] } },
|
|
106
|
-
source: { id: { $in: [3, 4] } },
|
|
107
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
108
|
-
expected: { id: { $in: [2, 3, 4] } }
|
|
109
|
-
},
|
|
110
|
-
"combine two $or queries": {
|
|
111
|
-
target: { $or: [ { id: 1 }, { id: 2 } ] },
|
|
112
|
-
source: { $or: [ { id: 3 } ] },
|
|
113
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
114
|
-
expected: { $or: [ { id: 1 }, { id: 2 }, { id: 3 } ] }
|
|
115
|
-
},
|
|
116
|
-
"combine two $and queries": {
|
|
117
|
-
target: { $and: [ { id: 1 }, { id: 2 } ] },
|
|
118
|
-
source: { $and: [ { id: 3 } ] },
|
|
119
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
120
|
-
expected: { $or: [{ $and: [{ id: 1 }, { id: 2 }] }, { $and: [{ id: 3 }] }] }
|
|
121
|
-
},
|
|
122
|
-
"combine $or and $and queries": {
|
|
123
|
-
target: { $or: [ { id: 1 }, { id: 2 } ], $and: [{ id: 4 }] },
|
|
124
|
-
source: { $or: [ { id: 3 } ], $and: [{ id: 5 }] },
|
|
125
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
126
|
-
expected: { $or: [ { id: 1 }, { id: 2 }, { id: 3 }, { $and: [{ id: 4 }] }, { $and: [{ id: 5 }] } ] }
|
|
127
|
-
},
|
|
128
|
-
"removes empty $or for both": {
|
|
129
|
-
target: { $or: [{}] },
|
|
130
|
-
source: { $or: [{}] },
|
|
131
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
132
|
-
expected: {}
|
|
133
|
-
},
|
|
134
|
-
"removes empty $and for both": {
|
|
135
|
-
target: { $and: [{}] },
|
|
136
|
-
source: { $and: [{}] },
|
|
137
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
138
|
-
expected: {}
|
|
139
|
-
},
|
|
140
|
-
"removes duplicate entries in $or": {
|
|
141
|
-
target: { $or: [{ id: 1 }, { id: 1 }, { id: 2 }] },
|
|
142
|
-
source: { $or: [{ id: 2 }] },
|
|
143
|
-
options: { defaultHandle: "combine", useLogicalConjunction: false },
|
|
144
|
-
expected: { $or: [{ id: 1 }, { id: 2 }] }
|
|
145
|
-
},
|
|
146
|
-
"$in and other props": {
|
|
147
|
-
target: { id: { $in: [1, 2, 3] }, status: "complete" },
|
|
148
|
-
source: { id: { $in: [2, 3, 4] }, status: { $in: ["complete", "pending", "draft"] } },
|
|
149
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
150
|
-
expected: { id: { $in: [ 2, 3 ] }, status: "complete" }
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
for (const key in passingPairs) {
|
|
154
|
-
const { target, source, options, expected } = passingPairs[key];
|
|
155
|
-
it(`'${key}'`, function() {
|
|
156
|
-
const query = mergeQuery(target, source, Object.assign({ service }, options));
|
|
157
|
-
assert.deepStrictEqual(query, expected, "works as expected");
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
describe("with useLogicalConjunction: true", function() {
|
|
162
|
-
const app = feathers();
|
|
163
|
-
app.use(
|
|
164
|
-
"/service",
|
|
165
|
-
new Service(
|
|
166
|
-
{
|
|
167
|
-
paginate: { default: 10, max: 100 },
|
|
168
|
-
whitelist: ["$and"]
|
|
169
|
-
}
|
|
170
|
-
)
|
|
171
|
-
);
|
|
172
|
-
const service = app.service("/service");
|
|
173
|
-
const passingPairs = {
|
|
174
|
-
"merge empty source": {
|
|
175
|
-
target: { id: 1 },
|
|
176
|
-
source: {},
|
|
177
|
-
options: { defaultHandle: "combine", useLogicalConjunction: true },
|
|
178
|
-
expected: { id: 1 }
|
|
179
|
-
},
|
|
180
|
-
"merge empty target": {
|
|
181
|
-
target: { },
|
|
182
|
-
source: { id: 1 },
|
|
183
|
-
options: { defaultHandle: "combine", useLogicalConjunction: true },
|
|
184
|
-
expected: { id: 1 }
|
|
185
|
-
},
|
|
186
|
-
"merge queries with $or": {
|
|
187
|
-
target: { id: 1 },
|
|
188
|
-
source: { $or: [{ id: { $in: [1, 3] } }], id: 2 },
|
|
189
|
-
options: { defaultHandle: "combine", useLogicalConjunction: true },
|
|
190
|
-
expected: { id: 1, $or: [{ id: { $in: [1, 3] } }, { id: 2 }] }
|
|
191
|
-
},
|
|
192
|
-
"merge queries with existing $or": {
|
|
193
|
-
target: { id: 1 },
|
|
194
|
-
source: { $or: [{ id: { $in: [1, 3] } }], id: 2 },
|
|
195
|
-
options: { defaultHandle: "combine", useLogicalConjunction: true },
|
|
196
|
-
expected: { id: 1, $or: [{ id: { $in: [1, 3] } }, { id : 2 }] }
|
|
197
|
-
},
|
|
198
|
-
"merge queries with existing $and": {
|
|
199
|
-
target: { id: 1 },
|
|
200
|
-
source: { $and: [{ id: 2 }] },
|
|
201
|
-
options: { defaultHandle: "combine", useLogicalConjunction: true },
|
|
202
|
-
expected: { id: 1, $or: [{ $and: [{ id: 2 }] }] }
|
|
203
|
-
},
|
|
204
|
-
"merge queries with existing $or in target": {
|
|
205
|
-
target: { $or: [{ id: 1 }], id: 3 },
|
|
206
|
-
source: { id: 2 },
|
|
207
|
-
options: { defaultHandle: "combine", useLogicalConjunction: true },
|
|
208
|
-
expected: { $or: [{ id: 1 }, { id: 2 }], id: 3 }
|
|
209
|
-
},
|
|
210
|
-
};
|
|
211
|
-
for (const key in passingPairs) {
|
|
212
|
-
const { target, source, options, expected } = passingPairs[key];
|
|
213
|
-
it(`'${key}'`, function() {
|
|
214
|
-
const query = mergeQuery(target, source, Object.assign({ service }, options));
|
|
215
|
-
assert.deepStrictEqual(query, expected, "works as expected");
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
describe("simple objects passing with handle intersect", function() {
|
|
222
|
-
const app = feathers();
|
|
223
|
-
app.use(
|
|
224
|
-
"/service",
|
|
225
|
-
new Service(
|
|
226
|
-
{
|
|
227
|
-
paginate: { default: 10, max: 100 },
|
|
228
|
-
whitelist: ["$and"]
|
|
229
|
-
}
|
|
230
|
-
)
|
|
231
|
-
);
|
|
232
|
-
const service = app.service("/service");
|
|
233
|
-
const passingPairs = {
|
|
234
|
-
"intersect number and overlapping $in": {
|
|
235
|
-
target: { id: 1 },
|
|
236
|
-
source: { id: { $in: [1, 3] } },
|
|
237
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
238
|
-
expected: { id: 1 }
|
|
239
|
-
},
|
|
240
|
-
"intersect $in and $in with overlapping": {
|
|
241
|
-
target: { id: { $in: [1, 2] } },
|
|
242
|
-
source: { id: { $in: [1, 3] } },
|
|
243
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
244
|
-
expected: { id: 1 }
|
|
245
|
-
},
|
|
246
|
-
"$limit for target stays the same": {
|
|
247
|
-
target: { $limit: 50, $skip: 10, $sort: { id: 1 } },
|
|
248
|
-
source: { id: 1 },
|
|
249
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
250
|
-
expected: { id: 1, $limit: 50, $skip: 10, $sort: { id: 1 } }
|
|
251
|
-
},
|
|
252
|
-
"$limit gets overridden": {
|
|
253
|
-
target: { $limit: 50, $skip: 10, $sort: { id: 1 } },
|
|
254
|
-
source: { $limit: 10, id: 1 },
|
|
255
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
256
|
-
expected: { id: 1, $limit: 10, $skip: 10, $sort: { id: 1 } }
|
|
257
|
-
},
|
|
258
|
-
"intersects two $or queries": {
|
|
259
|
-
target: { $or: [ { id: 1 }, { id: 2 } ] },
|
|
260
|
-
source: { $or: [ { id: 3 } ] },
|
|
261
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
262
|
-
expected: { $and: [{ $or: [{ id: 1 }, { id: 2 }] }, { $or: [{ id: 3 }] }] }
|
|
263
|
-
},
|
|
264
|
-
"intersects two $and queries": {
|
|
265
|
-
target: { $and: [ { id: 1 }, { id: 2 } ] },
|
|
266
|
-
source: { $and: [ { id: 3 } ] },
|
|
267
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
268
|
-
expected: { $and: [ { id: 1 }, { id: 2 }, { id: 3 } ] }
|
|
269
|
-
},
|
|
270
|
-
"intersect $or and $and queries": {
|
|
271
|
-
target: { $or: [ { id: 1 }, { id: 2 } ], $and: [{ id: 4 }] },
|
|
272
|
-
source: { $or: [ { id: 3 } ], $and: [{ id: 5 }] },
|
|
273
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
274
|
-
expected: { $and: [{ id: 4 }, { $or: [ { id: 1 }, { id: 2 } ] }, { $or: [{ id: 3 }] }, { id: 5 }] }
|
|
275
|
-
},
|
|
276
|
-
"cleans up $and with empty entries": {
|
|
277
|
-
target: { $and: [{}, { id: 1 }, { id: 1 }, { id: 2 }] },
|
|
278
|
-
source: { $and: [{}, { id: 2 }] },
|
|
279
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
280
|
-
expected: { $and: [{ id: 1 }, { id: 2 }] }
|
|
281
|
-
},
|
|
282
|
-
"removes duplicate entries in $and": {
|
|
283
|
-
target: { $and: [{ id: 1 }, { id: 1 }, { id: 2 }] },
|
|
284
|
-
source: { $and: [{ id: 2 }] },
|
|
285
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
286
|
-
expected: { $and: [{ id: 1 }, { id: 2 }] }
|
|
287
|
-
},
|
|
288
|
-
"removes unnecessary $or in target with intersect": {
|
|
289
|
-
target: { $or: [{ id: 1 }, {}] },
|
|
290
|
-
source: { hi: "test" },
|
|
291
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
292
|
-
expected: { hi: "test" }
|
|
293
|
-
},
|
|
294
|
-
"removes unnecessary $or in source with intersect": {
|
|
295
|
-
target: { hi: "test" },
|
|
296
|
-
source: { $or: [{ id: 1 }, {}] },
|
|
297
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
298
|
-
expected: { hi: "test" }
|
|
299
|
-
},
|
|
300
|
-
"$in and other props": {
|
|
301
|
-
target: { id: { $in: [1, 2, 3] }, status: "complete" },
|
|
302
|
-
source: { id: { $in: [2, 3, 4] }, status: { $in: ["complete", "pending", "draft"] } },
|
|
303
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false },
|
|
304
|
-
expected: { id: { $in: [ 2, 3 ] }, status: "complete" }
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
for (const key in passingPairs) {
|
|
308
|
-
const { target, source, options, expected } = passingPairs[key];
|
|
309
|
-
it(`'${key}'`, function() {
|
|
310
|
-
const query = mergeQuery(target, source, Object.assign({ service }, options));
|
|
311
|
-
assert.deepStrictEqual(query, expected, "works as expected");
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
describe("with useLogicalConjunction: true", function() {
|
|
316
|
-
const app = feathers();
|
|
317
|
-
app.use(
|
|
318
|
-
"/service",
|
|
319
|
-
new Service(
|
|
320
|
-
{
|
|
321
|
-
paginate: { default: 10, max: 100 },
|
|
322
|
-
whitelist: ["$and"]
|
|
323
|
-
}
|
|
324
|
-
)
|
|
325
|
-
);
|
|
326
|
-
const service = app.service("/service");
|
|
327
|
-
const passingPairs = {
|
|
328
|
-
"merge empty source": {
|
|
329
|
-
target: { id: 1 },
|
|
330
|
-
source: {},
|
|
331
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: true },
|
|
332
|
-
expected: { id: 1 }
|
|
333
|
-
},
|
|
334
|
-
"merge empty target": {
|
|
335
|
-
target: {},
|
|
336
|
-
source: { id: 1 },
|
|
337
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: true },
|
|
338
|
-
expected: { id: 1 }
|
|
339
|
-
},
|
|
340
|
-
"merge queries with $and": {
|
|
341
|
-
target: { id: 1 },
|
|
342
|
-
source: { id: { $in: [1, 3] } },
|
|
343
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: true },
|
|
344
|
-
expected: { id: 1, $and: [{ id: { $in: [1, 3] } }] }
|
|
345
|
-
},
|
|
346
|
-
"merge queries with existing $and": {
|
|
347
|
-
target: { id: 1 },
|
|
348
|
-
source: { $and: [{ id: { $in: [1, 3] } }], id: 2 },
|
|
349
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: true },
|
|
350
|
-
expected: { id: 1, $and: [{ id: { $in: [1, 3] } }, { id : 2 }] }
|
|
351
|
-
},
|
|
352
|
-
"merge queries with existing $or": {
|
|
353
|
-
target: { id: 1 },
|
|
354
|
-
source: { $or: [{ id: 2 }] },
|
|
355
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: true },
|
|
356
|
-
expected: { id: 1, $and: [{ $or: [{ id: 2 }] }] }
|
|
357
|
-
},
|
|
358
|
-
"merge queries with existing $and in target": {
|
|
359
|
-
target: { $and: [{ id: 1 }], id: 3 },
|
|
360
|
-
source: { id: 2 },
|
|
361
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: true },
|
|
362
|
-
expected: { $and: [{ id: 1 }, { id: 2 }], id: 3 }
|
|
363
|
-
},
|
|
364
|
-
};
|
|
365
|
-
for (const key in passingPairs) {
|
|
366
|
-
const { target, source, options, expected } = passingPairs[key];
|
|
367
|
-
it(`'${key}'`, function() {
|
|
368
|
-
const query = mergeQuery(target, source, Object.assign({ service }, options));
|
|
369
|
-
assert.deepStrictEqual(query, expected, "works as expected");
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
describe("simple objects failing", function() {
|
|
376
|
-
const failingPairs = {
|
|
377
|
-
"intersect two numbers": {
|
|
378
|
-
target: { id: 1 },
|
|
379
|
-
source: { id: 2 },
|
|
380
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false }
|
|
381
|
-
},
|
|
382
|
-
"intersect booleans": {
|
|
383
|
-
target: { test: true },
|
|
384
|
-
source: { test: false },
|
|
385
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false }
|
|
386
|
-
},
|
|
387
|
-
"intersect number and $in": {
|
|
388
|
-
target: { id: 1 },
|
|
389
|
-
source: { id: { $in: [2, 3] } },
|
|
390
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false }
|
|
391
|
-
},
|
|
392
|
-
"intersect two $in": {
|
|
393
|
-
target: { id: { $in: [1, 3 ] } },
|
|
394
|
-
source: { id: { $in: [2, 4 ] } },
|
|
395
|
-
options: { defaultHandle: "intersect", useLogicalConjunction: false }
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
for (const key in failingPairs) {
|
|
399
|
-
const { target, source, options } = failingPairs[key];
|
|
400
|
-
it(`'${key}'`, function() {
|
|
401
|
-
assert.throws(() => {
|
|
402
|
-
mergeQuery(target, source, options);
|
|
403
|
-
}, err => err.name === "Forbidden", "throws as expected");
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
});
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import assert from "assert";
|
|
2
|
-
import { pushSet } from "../../src";
|
|
3
|
-
|
|
4
|
-
describe("util - pushSet", function() {
|
|
5
|
-
it("pushes to existing array", function() {
|
|
6
|
-
const obj = {
|
|
7
|
-
arr: [1]
|
|
8
|
-
};
|
|
9
|
-
pushSet(obj, ["arr"], 2);
|
|
10
|
-
assert.deepStrictEqual(obj, { arr: [1, 2] });
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("sets array for not existing", function() {
|
|
14
|
-
const obj = {};
|
|
15
|
-
pushSet(obj, ["arr"], 2);
|
|
16
|
-
assert.deepStrictEqual(obj, { arr: [2] });
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("adds existing element", function() {
|
|
20
|
-
const obj = {
|
|
21
|
-
arr: [1]
|
|
22
|
-
};
|
|
23
|
-
pushSet(obj, ["arr"], 1);
|
|
24
|
-
assert.deepStrictEqual(obj, { arr: [1, 1] });
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("skips existing element", function() {
|
|
28
|
-
const obj = {
|
|
29
|
-
arr: [1]
|
|
30
|
-
};
|
|
31
|
-
pushSet(obj, ["arr"], 1, { unique: true });
|
|
32
|
-
assert.deepStrictEqual(obj, { arr: [1] });
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe("deep dot notation", function() {
|
|
36
|
-
it("deep: pushes to existing array", function() {
|
|
37
|
-
const obj = {
|
|
38
|
-
nested: { deep:[{ arr: [1] }] }
|
|
39
|
-
};
|
|
40
|
-
pushSet(obj, ["nested", "deep", 0, "arr"], 2);
|
|
41
|
-
assert.deepStrictEqual(obj, { nested: { deep:[{ arr: [1, 2] }] } });
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("deep: sets array for not existing", function() {
|
|
45
|
-
const obj = {};
|
|
46
|
-
pushSet(obj, ["nested", "deep", 0, "arr"], 2);
|
|
47
|
-
assert.deepStrictEqual(obj, { nested: { deep:[{ arr: [2] }] } });
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("deep: adds existing element", function() {
|
|
51
|
-
const obj = {
|
|
52
|
-
nested: { deep:[{ arr: [1] }] }
|
|
53
|
-
};
|
|
54
|
-
pushSet(obj, ["nested", "deep", 0, "arr"], 1);
|
|
55
|
-
assert.deepStrictEqual(obj, { nested: { deep:[{ arr: [1, 1] }] } });
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("deep: skips existing element", function() {
|
|
59
|
-
const obj = {
|
|
60
|
-
nested: { deep:[{ arr: [1] }] }
|
|
61
|
-
};
|
|
62
|
-
pushSet(obj, ["nested", "deep", 0, "arr"], 1, { unique: true });
|
|
63
|
-
assert.deepStrictEqual(obj, { nested: { deep:[{ arr: [1] }] } });
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import assert from "assert";
|
|
2
|
-
import { shouldSkip } from "../../src";
|
|
3
|
-
|
|
4
|
-
describe("util - shouldSkip", function() {
|
|
5
|
-
it("returns false if skipHooks not defined", function() {
|
|
6
|
-
["before", "after"].forEach(type => {
|
|
7
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
8
|
-
const context = { type, method };
|
|
9
|
-
//@ts-ignore
|
|
10
|
-
assert.strictEqual(shouldSkip("all", context), false, `'${type}:${method}': returns false`);
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("returns true for skipHooks: ['all']", function() {
|
|
16
|
-
["before", "after", "error"].forEach(type => {
|
|
17
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
18
|
-
const context = { type, method, params: { skipHooks: ["all"] } };
|
|
19
|
-
//@ts-ignore
|
|
20
|
-
assert.strictEqual(shouldSkip("test", context), true, `'${type}:${method}': returns true`);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("returns true for skipHooks: [`type`]", function() {
|
|
26
|
-
["before", "after", "error"].forEach(type => {
|
|
27
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
28
|
-
const context = { type, method, params: { skipHooks: [type] } };
|
|
29
|
-
//@ts-ignore
|
|
30
|
-
assert.strictEqual(shouldSkip("test", context), true, `'${type}:${method}': returns true`);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("returns true for skipHooks: ['`type`:test']", function() {
|
|
36
|
-
["before", "after", "error"].forEach(type => {
|
|
37
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
38
|
-
const context = { type, method, params: { skipHooks: [`${type}:test`] } };
|
|
39
|
-
//@ts-ignore
|
|
40
|
-
assert.strictEqual(shouldSkip("test", context), true, `'${type}:${method}': returns true`);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("returns true for skipHooks: ['test']", function() {
|
|
46
|
-
["before", "after", "error"].forEach(type => {
|
|
47
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
48
|
-
const context = { type, method, params: { skipHooks: ["test"] } };
|
|
49
|
-
//@ts-ignore
|
|
50
|
-
assert.strictEqual(shouldSkip("test", context), true, `'${type}:${method}': returns true`);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("returns false for skipHooks other than test", function() {
|
|
56
|
-
const types = ["before", "after", "error"];
|
|
57
|
-
types.forEach(type => {
|
|
58
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
59
|
-
const remainingTypes = types.filter(x => x !== type);
|
|
60
|
-
const skipHooks = ["test2", `${type}:test2`, ...remainingTypes.map(t => `${t}:test1`)];
|
|
61
|
-
const context = { type, method, params: { skipHooks } };
|
|
62
|
-
//@ts-ignore
|
|
63
|
-
assert.strictEqual(shouldSkip("test1", context), false, `'${type}:${method}': returns true`);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
});
|