feathers-utils 1.6.1 → 2.0.0-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 (84) hide show
  1. package/dist/esm/hooks/checkMulti.d.ts +2 -0
  2. package/dist/esm/hooks/checkMulti.js +18 -0
  3. package/dist/esm/hooks/runPerItem.d.ts +4 -0
  4. package/dist/esm/hooks/runPerItem.js +26 -0
  5. package/dist/esm/hooks/setData.d.ts +4 -0
  6. package/dist/esm/hooks/setData.js +34 -0
  7. package/dist/esm/index.d.ts +30 -0
  8. package/dist/esm/index.js +30 -0
  9. package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +15 -0
  10. package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +46 -0
  11. package/dist/esm/mixins/debounce-mixin/index.d.ts +8 -0
  12. package/dist/esm/mixins/debounce-mixin/index.js +20 -0
  13. package/dist/esm/types.d.ts +60 -0
  14. package/dist/esm/types.js +2 -0
  15. package/dist/esm/utils/filterQuery.d.ts +3 -0
  16. package/dist/esm/utils/filterQuery.js +28 -0
  17. package/dist/esm/utils/getPaginate.d.ts +7 -0
  18. package/dist/esm/utils/getPaginate.js +14 -0
  19. package/dist/esm/utils/isMulti.d.ts +2 -0
  20. package/dist/esm/utils/isMulti.js +17 -0
  21. package/dist/esm/utils/isPaginated.d.ts +2 -0
  22. package/dist/esm/utils/isPaginated.js +8 -0
  23. package/dist/esm/utils/markHookForSkip.d.ts +3 -0
  24. package/dist/esm/utils/markHookForSkip.js +14 -0
  25. package/dist/esm/utils/mergeQuery/index.d.ts +3 -0
  26. package/dist/esm/utils/mergeQuery/index.js +327 -0
  27. package/dist/esm/utils/mergeQuery/mergeArrays.d.ts +2 -0
  28. package/dist/esm/utils/mergeQuery/mergeArrays.js +37 -0
  29. package/dist/esm/utils/pushSet.d.ts +2 -0
  30. package/dist/esm/utils/pushSet.js +19 -0
  31. package/dist/esm/utils/setResultEmpty.d.ts +2 -0
  32. package/dist/esm/utils/setResultEmpty.js +25 -0
  33. package/dist/esm/utils/shouldSkip.d.ts +2 -0
  34. package/dist/esm/utils/shouldSkip.js +29 -0
  35. package/dist/hooks/checkMulti.js +2 -2
  36. package/dist/hooks/runPerItem.d.ts +2 -1
  37. package/dist/hooks/runPerItem.js +3 -2
  38. package/dist/hooks/setData.js +7 -6
  39. package/dist/index.d.ts +6 -3
  40. package/dist/index.js +7 -3
  41. package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +2 -2
  42. package/dist/mixins/debounce-mixin/DebouncedStore.js +2 -2
  43. package/dist/mixins/debounce-mixin/index.d.ts +4 -1
  44. package/dist/mixins/debounce-mixin/index.js +1 -1
  45. package/dist/types.d.ts +3 -2
  46. package/dist/utils/filterQuery.js +2 -2
  47. package/dist/utils/getPaginate.d.ts +7 -0
  48. package/dist/utils/getPaginate.js +15 -0
  49. package/dist/utils/isMulti.js +2 -3
  50. package/dist/utils/isPaginated.d.ts +2 -0
  51. package/dist/utils/isPaginated.js +12 -0
  52. package/dist/utils/markHookForSkip.js +1 -1
  53. package/dist/utils/mergeQuery/index.js +38 -38
  54. package/dist/utils/pushSet.js +3 -3
  55. package/dist/utils/setResultEmpty.d.ts +2 -0
  56. package/dist/utils/setResultEmpty.js +29 -0
  57. package/package.json +19 -13
  58. package/src/hooks/runPerItem.ts +4 -1
  59. package/src/hooks/setData.ts +1 -0
  60. package/src/index.ts +6 -3
  61. package/src/mixins/debounce-mixin/DebouncedStore.ts +1 -1
  62. package/src/mixins/debounce-mixin/index.ts +7 -3
  63. package/src/types.ts +4 -3
  64. package/src/utils/filterQuery.ts +2 -2
  65. package/src/utils/getPaginate.ts +26 -0
  66. package/src/utils/isMulti.ts +2 -3
  67. package/src/utils/isPaginated.ts +12 -0
  68. package/src/utils/setResultEmpty.ts +28 -0
  69. package/tsconfig-esm.json +9 -0
  70. package/.github/workflows/node.js.yml +0 -45
  71. package/.vscode/launch.json +0 -28
  72. package/.vscode/settings.json +0 -14
  73. package/dist/utils/addHook.d.ts +0 -3
  74. package/dist/utils/addHook.js +0 -40
  75. package/src/utils/addHook.ts +0 -49
  76. package/test/hooks/checkMulti.test.ts +0 -121
  77. package/test/hooks/setData.test.ts +0 -333
  78. package/test/mixins/debounce-mixin.test.ts +0 -174
  79. package/test/utils/addHook.test.ts +0 -307
  80. package/test/utils/isMulti.test.ts +0 -53
  81. package/test/utils/markHookForSkip.test.ts +0 -292
  82. package/test/utils/mergeQuery.test.ts +0 -407
  83. package/test/utils/pushSet.test.ts +0 -66
  84. package/test/utils/shouldSkip.test.ts +0 -67
@@ -1,292 +0,0 @@
1
- import assert from "assert";
2
- import feathers from "@feathersjs/feathers";
3
- import { Service } from "feathers-memory";
4
- import { shouldSkip, markHookForSkip } from "../../src";
5
-
6
- describe("util - markHookForSkip", function() {
7
- it("returns hook object", function() {
8
- const context = markHookForSkip("test", "all", {});
9
- assert.ok(context, "returned context");
10
- assert.ok(context.params.skipHooks, "has skipHooks");
11
- });
12
-
13
- it("returns hook object for undefined context", function() {
14
- const context = markHookForSkip("test", "all");
15
- assert.ok(context, "returned context");
16
- assert.ok(context.params.skipHooks, "has skipHooks");
17
- });
18
-
19
- it("skips explicitly before hook", async function() {
20
- const app = feathers();
21
- app.use("service", new Service());
22
- const service = app.service("service");
23
- const ranInto = {};
24
- service.hooks({
25
- before: {
26
- all: [
27
- context => {
28
- markHookForSkip("test", "before", context);
29
- }
30
- ],
31
- find: [
32
- context => {
33
- if (shouldSkip("test", context)) { return context; }
34
- ranInto["find"] = true;
35
- },
36
- context => { context.result = null; }
37
- ],
38
- get: [
39
- context => {
40
- if (shouldSkip("test", context)) { return context; }
41
- ranInto["get"] = true;
42
- },
43
- context => { context.result = null; }
44
- ],
45
- create: [
46
- context => {
47
- if (shouldSkip("test", context)) { return context; }
48
- ranInto["create"] = true;
49
- },
50
- context => { context.result = null; }
51
- ],
52
- update: [
53
- context => {
54
- if (shouldSkip("test", context)) { return context; }
55
- ranInto["update"] = true;
56
- },
57
- context => { context.result = null; }
58
- ],
59
- patch: [
60
- context => {
61
- if (shouldSkip("test", context)) { return context; }
62
- ranInto["patch"] = true;
63
- },
64
- context => { context.result = null; }
65
- ],
66
- remove: [
67
- context => {
68
- if (shouldSkip("test", context)) { return context; }
69
- ranInto["remove"] = true;
70
- },
71
- context => { context.result = null; }
72
- ]
73
- }
74
- });
75
- const methods = {
76
- find: [],
77
- get: [1],
78
- create: [{}],
79
- update: [1, {}],
80
- patch: [1, {}],
81
- remove: [1]
82
- };
83
- const promises = Object.keys(methods).map(async method => {
84
- await service[method](...methods[method]);
85
- assert.ok(!Object.prototype.hasOwnProperty.call(ranInto, method), `'${method}': did not run into hook`);
86
- return true;
87
- });
88
-
89
- const results = await Promise.all(promises);
90
- assert.ok(results.every(x => x === true), "all ok");
91
- });
92
-
93
- it("skips explicitly after hook", async function() {
94
- const app = feathers();
95
- app.use("service", new Service());
96
- const service = app.service("service");
97
- const ranInto = {};
98
- service.hooks({
99
- before: {
100
- all: [
101
- context => {
102
- markHookForSkip("test", "after", context);
103
- },
104
- context => {
105
- context.result = null;
106
- return context;
107
- }
108
- ]
109
- },
110
- after: {
111
- find: [
112
- context => {
113
- if (shouldSkip("test", context)) { return context; }
114
- ranInto["find"] = true;
115
- },
116
- context => { context.result = null; }
117
- ],
118
- get: [
119
- context => {
120
- if (shouldSkip("test", context)) { return context; }
121
- ranInto["get"] = true;
122
- },
123
- context => { context.result = null; }
124
- ],
125
- create: [
126
- context => {
127
- if (shouldSkip("test", context)) { return context; }
128
- ranInto["create"] = true;
129
- },
130
- context => { context.result = null; }
131
- ],
132
- update: [
133
- context => {
134
- if (shouldSkip("test", context)) { return context; }
135
- ranInto["update"] = true;
136
- },
137
- context => { context.result = null; }
138
- ],
139
- patch: [
140
- context => {
141
- if (shouldSkip("test", context)) { return context; }
142
- ranInto["patch"] = true;
143
- },
144
- context => { context.result = null; }
145
- ],
146
- remove: [
147
- context => {
148
- if (shouldSkip("test", context)) { return context; }
149
- ranInto["remove"] = true;
150
- },
151
- context => { context.result = null; }
152
- ]
153
- }
154
- });
155
- const methods = {
156
- find: [],
157
- get: [1],
158
- create: [{}],
159
- update: [1, {}],
160
- patch: [1, {}],
161
- remove: [1]
162
- };
163
- const promises = Object.keys(methods).map(async method => {
164
- await service[method](...methods[method]);
165
- assert.ok(!Object.prototype.hasOwnProperty.call(ranInto, method), `'${method}': did not run into hook`);
166
- return true;
167
- });
168
-
169
- const results = await Promise.all(promises);
170
- assert.ok(results.every(x => x === true), "all ok");
171
- });
172
-
173
- it("skips all hooks", async function() {
174
- const app = feathers();
175
- app.use("service", new Service());
176
- const service = app.service("service");
177
- const ranIntoBefore = {};
178
- const ranIntoAfter = {};
179
- service.hooks({
180
- before: {
181
- all: [
182
- context => {
183
- markHookForSkip("test", "all", context);
184
- }
185
- ],
186
- find: [
187
- context => {
188
- if (shouldSkip("test", context)) { return context; }
189
- ranIntoBefore["find"] = true;
190
- },
191
- context => { context.result = null; }
192
- ],
193
- get: [
194
- context => {
195
- if (shouldSkip("test", context)) { return context; }
196
- ranIntoBefore["get"] = true;
197
- },
198
- context => { context.result = null; }
199
- ],
200
- create: [
201
- context => {
202
- if (shouldSkip("test", context)) { return context; }
203
- ranIntoBefore["create"] = true;
204
- },
205
- context => { context.result = null; }
206
- ],
207
- update: [
208
- context => {
209
- if (shouldSkip("test", context)) { return context; }
210
- ranIntoBefore["update"] = true;
211
- },
212
- context => { context.result = null; }
213
- ],
214
- patch: [
215
- context => {
216
- if (shouldSkip("test", context)) { return context; }
217
- ranIntoBefore["patch"] = true;
218
- },
219
- context => { context.result = null; }
220
- ],
221
- remove: [
222
- context => {
223
- if (shouldSkip("test", context)) { return context; }
224
- ranIntoBefore["remove"] = true;
225
- },
226
- context => { context.result = null; }
227
- ]
228
- },
229
- after: {
230
- find: [
231
- context => {
232
- if (shouldSkip("test", context)) { return context; }
233
- ranIntoAfter["find"] = true;
234
- },
235
- context => { context.result = null; }
236
- ],
237
- get: [
238
- context => {
239
- if (shouldSkip("test", context)) { return context; }
240
- ranIntoAfter["get"] = true;
241
- },
242
- context => { context.result = null; }
243
- ],
244
- create: [
245
- context => {
246
- if (shouldSkip("test", context)) { return context; }
247
- ranIntoAfter["create"] = true;
248
- },
249
- context => { context.result = null; }
250
- ],
251
- update: [
252
- context => {
253
- if (shouldSkip("test", context)) { return context; }
254
- ranIntoAfter["update"] = true;
255
- },
256
- context => { context.result = null; }
257
- ],
258
- patch: [
259
- context => {
260
- if (shouldSkip("test", context)) { return context; }
261
- ranIntoAfter["patch"] = true;
262
- },
263
- context => { context.result = null; }
264
- ],
265
- remove: [
266
- context => {
267
- if (shouldSkip("test", context)) { return context; }
268
- ranIntoAfter["remove"] = true;
269
- },
270
- context => { context.result = null; }
271
- ]
272
- }
273
- });
274
- const methods = {
275
- find: [],
276
- get: [1],
277
- create: [{}],
278
- update: [1, {}],
279
- patch: [1, {}],
280
- remove: [1]
281
- };
282
- const promises = Object.keys(methods).map(async method => {
283
- await service[method](...methods[method]);
284
- assert.ok(!Object.prototype.hasOwnProperty.call(ranIntoBefore, method), `'${method}': did not run into before hook`);
285
- assert.ok(!Object.prototype.hasOwnProperty.call(ranIntoAfter, method), `'${method}': did not run into after hook`);
286
- return true;
287
- });
288
-
289
- const results = await Promise.all(promises);
290
- assert.ok(results.every(x => x === true), "all ok");
291
- });
292
- });
@@ -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
- });