feathers-utils 6.0.0 → 7.0.1

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 (59) hide show
  1. package/README.md +3 -4
  2. package/dist/index.cjs +81 -99
  3. package/dist/index.d.cts +35 -33
  4. package/dist/index.d.mts +35 -33
  5. package/dist/index.d.ts +35 -33
  6. package/dist/index.mjs +81 -99
  7. package/package.json +29 -32
  8. package/src/.DS_Store +0 -0
  9. package/src/filters/array.ts +11 -13
  10. package/src/filters/index.ts +2 -2
  11. package/src/filters/object.ts +11 -11
  12. package/src/hooks/.DS_Store +0 -0
  13. package/src/hooks/checkMulti.ts +98 -82
  14. package/src/hooks/createRelated.ts +41 -41
  15. package/src/hooks/forEach.ts +32 -32
  16. package/src/hooks/from-client-for-server/common.ts +1 -1
  17. package/src/hooks/from-client-for-server/index.ts +2 -2
  18. package/src/hooks/from-client-for-server/paramsForServer.ts +32 -32
  19. package/src/hooks/from-client-for-server/paramsFromClient.ts +25 -25
  20. package/src/hooks/index.ts +9 -9
  21. package/src/hooks/onDelete.ts +54 -55
  22. package/src/hooks/parseFields.ts +13 -13
  23. package/src/hooks/removeRelated.ts +22 -20
  24. package/src/hooks/runPerItem.ts +17 -18
  25. package/src/hooks/setData.ts +295 -264
  26. package/src/index.ts +6 -6
  27. package/src/mixins/debounce-mixin/DebouncedStore.ts +29 -29
  28. package/src/mixins/debounce-mixin/debounceMixin.ts +17 -17
  29. package/src/mixins/debounce-mixin/index.ts +3 -3
  30. package/src/mixins/debounce-mixin/types.ts +9 -9
  31. package/src/mixins/debounce-mixin/utils.ts +3 -3
  32. package/src/mixins/index.ts +1 -1
  33. package/src/types.ts +3 -5
  34. package/src/typesInternal.ts +14 -14
  35. package/src/utility-types/index.ts +48 -48
  36. package/src/utils/_utils.internal.ts +5 -5
  37. package/src/utils/defineHooks.ts +8 -8
  38. package/src/utils/deflattenQuery.ts +31 -31
  39. package/src/utils/filterQuery.ts +58 -58
  40. package/src/utils/flattenQuery.ts +54 -54
  41. package/src/utils/getItemsIsArray.ts +148 -149
  42. package/src/utils/getPaginate.ts +31 -31
  43. package/src/utils/index.ts +17 -17
  44. package/src/utils/isMulti.ts +48 -40
  45. package/src/utils/isPaginated.ts +30 -30
  46. package/src/utils/markHookForSkip.ts +177 -178
  47. package/src/utils/mergeQuery/index.ts +3 -3
  48. package/src/utils/mergeQuery/mergeArrays.ts +67 -67
  49. package/src/utils/mergeQuery/mergeQuery.ts +211 -211
  50. package/src/utils/mergeQuery/types.ts +12 -12
  51. package/src/utils/mergeQuery/utils.ts +224 -224
  52. package/src/utils/optimizeBatchPatch.ts +42 -42
  53. package/src/utils/pushSet.ts +57 -57
  54. package/src/utils/setQueryKeySafely.ts +68 -68
  55. package/src/utils/setResultEmpty.ts +125 -123
  56. package/src/utils/shouldSkip.ts +72 -72
  57. package/src/utils/toJSON.ts +4 -4
  58. package/src/utils/validateQueryProperty.ts +10 -10
  59. package/src/hooks/makeSequelizeQuery.ts_ +0 -90
@@ -1,16 +1,16 @@
1
- import type { Id, Params } from "@feathersjs/feathers";
2
- import { deepEqual } from "fast-equals";
3
- import type { KeyOf } from "../typesInternal";
1
+ import type { Id, Params } from '@feathersjs/feathers'
2
+ import { deepEqual } from 'fast-equals'
3
+ import type { KeyOf } from '../typesInternal.js'
4
4
 
5
5
  export type OptimizeBatchPatchOptions<IdKey extends string> = {
6
6
  /** the key of the id property */
7
- id?: IdKey;
8
- };
7
+ id?: IdKey
8
+ }
9
9
 
10
10
  export type OptimizeBatchPatchResultItem<
11
11
  T = Record<string, unknown>,
12
12
  P = Params,
13
- > = [Id, T, P | undefined];
13
+ > = [Id | null, T, P | undefined]
14
14
 
15
15
  export function optimizeBatchPatch<
16
16
  T extends Record<string, any>,
@@ -21,23 +21,23 @@ export function optimizeBatchPatch<
21
21
  items: T[],
22
22
  options?: OptimizeBatchPatchOptions<IdKey>,
23
23
  ): OptimizeBatchPatchResultItem<R, P>[] {
24
- const map: { ids: Id[]; data: R }[] = [];
24
+ const map: { ids: Id[]; data: R }[] = []
25
25
 
26
- const idKey = options?.id ?? "id";
26
+ const idKey = options?.id ?? 'id'
27
27
 
28
28
  for (const _data of items) {
29
- const data = _data as unknown as R;
30
- const id = _data[idKey];
31
- delete data[idKey as any];
29
+ const data = _data as unknown as R
30
+ const id = _data[idKey]
31
+ delete (data as any)[idKey as any]
32
32
 
33
33
  const index = map.findIndex((item) => {
34
- return deepEqual(item.data, data);
35
- });
34
+ return deepEqual(item.data, data)
35
+ })
36
36
 
37
37
  if (index === -1) {
38
- map.push({ ids: [id], data });
38
+ map.push({ ids: [id], data })
39
39
  } else {
40
- map[index].ids.push(id);
40
+ map[index].ids.push(id)
41
41
  }
42
42
  }
43
43
 
@@ -52,47 +52,47 @@ export function optimizeBatchPatch<
52
52
  [idKey]: { $in: ids },
53
53
  },
54
54
  },
55
- ] as OptimizeBatchPatchResultItem<R, P>);
56
- });
55
+ ] as OptimizeBatchPatchResultItem<R, P>)
56
+ })
57
57
  }
58
58
 
59
59
  if (import.meta.vitest) {
60
- const { it, expect } = import.meta.vitest;
61
- it("optimizeBatchPatch", () => {
60
+ const { it, expect } = import.meta.vitest
61
+ it('optimizeBatchPatch', () => {
62
62
  expect(
63
63
  optimizeBatchPatch(
64
64
  [
65
- { id: "1", name: "John" },
66
- { id: "2", name: "Jane" },
67
- { id: "3", name: "John" },
68
- { id: "4", name: "Jane" },
69
- { id: 5, name: "Jack" },
65
+ { id: '1', name: 'John' },
66
+ { id: '2', name: 'Jane' },
67
+ { id: '3', name: 'John' },
68
+ { id: '4', name: 'Jane' },
69
+ { id: 5, name: 'Jack' },
70
70
  ],
71
- { id: "id" },
71
+ { id: 'id' },
72
72
  ),
73
73
  ).toEqual([
74
- [null, { name: "John" }, { query: { id: { $in: ["1", "3"] } } }],
75
- [null, { name: "Jane" }, { query: { id: { $in: ["2", "4"] } } }],
76
- [5, { name: "Jack" }, undefined],
77
- ]);
78
- });
74
+ [null, { name: 'John' }, { query: { id: { $in: ['1', '3'] } } }],
75
+ [null, { name: 'Jane' }, { query: { id: { $in: ['2', '4'] } } }],
76
+ [5, { name: 'Jack' }, undefined],
77
+ ])
78
+ })
79
79
 
80
- it("optimizeBatchPatch with _id", () => {
80
+ it('optimizeBatchPatch with _id', () => {
81
81
  expect(
82
82
  optimizeBatchPatch(
83
83
  [
84
- { _id: "1", name: "John" },
85
- { _id: "2", name: "Jane" },
86
- { _id: "3", name: "John" },
87
- { _id: "4", name: "Jane" },
88
- { _id: 5, name: "Jack" },
84
+ { _id: '1', name: 'John' },
85
+ { _id: '2', name: 'Jane' },
86
+ { _id: '3', name: 'John' },
87
+ { _id: '4', name: 'Jane' },
88
+ { _id: 5, name: 'Jack' },
89
89
  ],
90
- { id: "_id" },
90
+ { id: '_id' },
91
91
  ),
92
92
  ).toEqual([
93
- [null, { name: "John" }, { query: { _id: { $in: ["1", "3"] } } }],
94
- [null, { name: "Jane" }, { query: { _id: { $in: ["2", "4"] } } }],
95
- [5, { name: "Jack" }, undefined],
96
- ]);
97
- });
93
+ [null, { name: 'John' }, { query: { _id: { $in: ['1', '3'] } } }],
94
+ [null, { name: 'Jane' }, { query: { _id: { $in: ['2', '4'] } } }],
95
+ [5, { name: 'Jack' }, undefined],
96
+ ])
97
+ })
98
98
  }
@@ -1,10 +1,10 @@
1
- import { deepEqual as _isEqual } from "fast-equals";
2
- import _get from "lodash/get.js";
3
- import _set from "lodash/set.js";
4
- import type { Path } from "../typesInternal";
1
+ import { deepEqual as _isEqual } from 'fast-equals'
2
+ import _get from 'lodash/get.js'
3
+ import _set from 'lodash/set.js'
4
+ import type { Path } from '../typesInternal.js'
5
5
 
6
6
  export interface PushSetOptions {
7
- unique?: boolean;
7
+ unique?: boolean
8
8
  }
9
9
 
10
10
  /**
@@ -16,83 +16,83 @@ export const pushSet = (
16
16
  val: unknown,
17
17
  options?: PushSetOptions,
18
18
  ): unknown[] => {
19
- options = options || {};
20
- let arr = _get(obj, path);
19
+ options = options || {}
20
+ let arr = _get(obj, path)
21
21
  if (!arr || !Array.isArray(arr)) {
22
- arr = [val];
23
- _set(obj, path, arr);
24
- return arr;
22
+ arr = [val]
23
+ _set(obj, path, arr)
24
+ return arr
25
25
  } else {
26
26
  if (options.unique && arr.some((x) => _isEqual(x, val))) {
27
- return arr;
27
+ return arr
28
28
  }
29
- arr.push(val);
30
- return arr;
29
+ arr.push(val)
30
+ return arr
31
31
  }
32
- };
32
+ }
33
33
 
34
34
  if (import.meta.vitest) {
35
- const { describe, it, assert } = import.meta.vitest;
35
+ const { describe, it, assert } = import.meta.vitest
36
36
 
37
- it("pushes to existing array", function () {
37
+ it('pushes to existing array', function () {
38
38
  const obj = {
39
39
  arr: [1],
40
- };
41
- pushSet(obj, ["arr"], 2);
42
- assert.deepStrictEqual(obj, { arr: [1, 2] });
43
- });
40
+ }
41
+ pushSet(obj, ['arr'], 2)
42
+ assert.deepStrictEqual(obj, { arr: [1, 2] })
43
+ })
44
44
 
45
- it("sets array for not existing", function () {
46
- const obj = {};
47
- pushSet(obj, ["arr"], 2);
48
- assert.deepStrictEqual(obj, { arr: [2] });
49
- });
45
+ it('sets array for not existing', function () {
46
+ const obj = {}
47
+ pushSet(obj, ['arr'], 2)
48
+ assert.deepStrictEqual(obj, { arr: [2] })
49
+ })
50
50
 
51
- it("adds existing element", function () {
51
+ it('adds existing element', function () {
52
52
  const obj = {
53
53
  arr: [1],
54
- };
55
- pushSet(obj, ["arr"], 1);
56
- assert.deepStrictEqual(obj, { arr: [1, 1] });
57
- });
54
+ }
55
+ pushSet(obj, ['arr'], 1)
56
+ assert.deepStrictEqual(obj, { arr: [1, 1] })
57
+ })
58
58
 
59
- it("skips existing element", function () {
59
+ it('skips existing element', function () {
60
60
  const obj = {
61
61
  arr: [1],
62
- };
63
- pushSet(obj, ["arr"], 1, { unique: true });
64
- assert.deepStrictEqual(obj, { arr: [1] });
65
- });
62
+ }
63
+ pushSet(obj, ['arr'], 1, { unique: true })
64
+ assert.deepStrictEqual(obj, { arr: [1] })
65
+ })
66
66
 
67
- describe("deep dot notation", function () {
68
- it("deep: pushes to existing array", function () {
67
+ describe('deep dot notation', function () {
68
+ it('deep: pushes to existing array', function () {
69
69
  const obj = {
70
70
  nested: { deep: [{ arr: [1] }] },
71
- };
72
- pushSet(obj, ["nested", "deep", 0, "arr"], 2);
73
- assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [1, 2] }] } });
74
- });
71
+ }
72
+ pushSet(obj, ['nested', 'deep', 0, 'arr'], 2)
73
+ assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [1, 2] }] } })
74
+ })
75
75
 
76
- it("deep: sets array for not existing", function () {
77
- const obj = {};
78
- pushSet(obj, ["nested", "deep", 0, "arr"], 2);
79
- assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [2] }] } });
80
- });
76
+ it('deep: sets array for not existing', function () {
77
+ const obj = {}
78
+ pushSet(obj, ['nested', 'deep', 0, 'arr'], 2)
79
+ assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [2] }] } })
80
+ })
81
81
 
82
- it("deep: adds existing element", function () {
82
+ it('deep: adds existing element', function () {
83
83
  const obj = {
84
84
  nested: { deep: [{ arr: [1] }] },
85
- };
86
- pushSet(obj, ["nested", "deep", 0, "arr"], 1);
87
- assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [1, 1] }] } });
88
- });
85
+ }
86
+ pushSet(obj, ['nested', 'deep', 0, 'arr'], 1)
87
+ assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [1, 1] }] } })
88
+ })
89
89
 
90
- it("deep: skips existing element", function () {
90
+ it('deep: skips existing element', function () {
91
91
  const obj = {
92
92
  nested: { deep: [{ arr: [1] }] },
93
- };
94
- pushSet(obj, ["nested", "deep", 0, "arr"], 1, { unique: true });
95
- assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [1] }] } });
96
- });
97
- });
93
+ }
94
+ pushSet(obj, ['nested', 'deep', 0, 'arr'], 1, { unique: true })
95
+ assert.deepStrictEqual(obj, { nested: { deep: [{ arr: [1] }] } })
96
+ })
97
+ })
98
98
  }
@@ -1,194 +1,194 @@
1
- import { isPlainObject } from "./_utils.internal";
2
- import type { Params } from "@feathersjs/feathers";
1
+ import { isPlainObject } from './_utils.internal.js'
2
+ import type { Params } from '@feathersjs/feathers'
3
3
 
4
4
  export type SetQueryKeySafelyOptions = {
5
- mutate?: boolean;
6
- };
5
+ mutate?: boolean
6
+ }
7
7
 
8
8
  export const setQueryKeySafely = (
9
9
  params: Params,
10
10
  key: string,
11
11
  value: any,
12
- operator = "$eq",
12
+ operator = '$eq',
13
13
  options?: SetQueryKeySafelyOptions,
14
14
  ): Params => {
15
- const { mutate = false } = options || {};
15
+ const { mutate = false } = options || {}
16
16
 
17
17
  // TODO: mutate params
18
18
  if (!mutate) {
19
- params = structuredClone(params);
19
+ params = structuredClone(params)
20
20
  }
21
21
 
22
22
  if (!params.query) {
23
- params.query = {};
23
+ params.query = {}
24
24
  }
25
25
 
26
26
  // if the key is not in the query, just add it and return
27
27
  if (!(key in params.query)) {
28
- if (operator === "$eq") {
29
- params.query[key] = value;
28
+ if (operator === '$eq') {
29
+ params.query[key] = value
30
30
  } else {
31
31
  params.query[key] = {
32
32
  [operator]: value,
33
- };
33
+ }
34
34
  }
35
35
 
36
- return params;
36
+ return params
37
37
  }
38
38
 
39
39
  if (isPlainObject(params.query[key]) && !(operator in params.query[key])) {
40
- params.query[key][operator] = value;
40
+ params.query[key][operator] = value
41
41
  } else {
42
- params.query.$and ??= [];
42
+ params.query.$and ??= []
43
43
 
44
44
  params.query.$and.push(
45
- operator === "$eq"
45
+ operator === '$eq'
46
46
  ? { [key]: value }
47
47
  : {
48
48
  [key]: {
49
49
  [operator]: value,
50
50
  },
51
51
  },
52
- );
52
+ )
53
53
  }
54
54
 
55
- return params;
56
- };
55
+ return params
56
+ }
57
57
 
58
58
  if (import.meta.vitest) {
59
- const { it, assert } = import.meta.vitest;
59
+ const { it, assert } = import.meta.vitest
60
60
 
61
- it("does not mutate by default", function () {
61
+ it('does not mutate by default', function () {
62
62
  const params: Params = {
63
63
  query: {
64
64
  test: true,
65
65
  },
66
- };
66
+ }
67
67
 
68
- const result = setQueryKeySafely(params, "test", false);
68
+ const result = setQueryKeySafely(params, 'test', false)
69
69
  assert.deepStrictEqual(params, {
70
70
  query: {
71
71
  test: true,
72
72
  },
73
- });
73
+ })
74
74
 
75
75
  assert.deepStrictEqual(result, {
76
76
  query: {
77
77
  test: true,
78
78
  $and: [{ test: false }],
79
79
  },
80
- });
81
- });
80
+ })
81
+ })
82
82
 
83
- it("does not mutate explicitely", function () {
83
+ it('does not mutate explicitely', function () {
84
84
  const params: Params = {
85
85
  query: {
86
86
  test: true,
87
87
  },
88
- };
88
+ }
89
89
 
90
- const result = setQueryKeySafely(params, "test", false, "$eq", {
90
+ const result = setQueryKeySafely(params, 'test', false, '$eq', {
91
91
  mutate: false,
92
- });
92
+ })
93
93
 
94
94
  assert.deepStrictEqual(params, {
95
95
  query: {
96
96
  test: true,
97
97
  },
98
- });
98
+ })
99
99
 
100
100
  assert.deepStrictEqual(result, {
101
101
  query: {
102
102
  test: true,
103
103
  $and: [{ test: false }],
104
104
  },
105
- });
106
- });
105
+ })
106
+ })
107
107
 
108
- it("does mutate explicitely", function () {
108
+ it('does mutate explicitely', function () {
109
109
  const params: Params = {
110
110
  query: {
111
111
  test: true,
112
112
  },
113
- };
113
+ }
114
114
 
115
- const result = setQueryKeySafely(params, "test", false, "$eq", {
115
+ const result = setQueryKeySafely(params, 'test', false, '$eq', {
116
116
  mutate: true,
117
- });
117
+ })
118
118
 
119
- assert.equal(params, result);
120
- });
119
+ assert.equal(params, result)
120
+ })
121
121
 
122
- it("adds a $eq filter for non existent key", function () {
122
+ it('adds a $eq filter for non existent key', function () {
123
123
  const params: Params = {
124
124
  query: {},
125
- };
125
+ }
126
126
 
127
- const result = setQueryKeySafely(params, "test", true);
127
+ const result = setQueryKeySafely(params, 'test', true)
128
128
  assert.deepStrictEqual(result, {
129
129
  query: {
130
130
  test: true,
131
131
  },
132
- });
133
- });
132
+ })
133
+ })
134
134
 
135
- it("adds a $ne filter for non existent key", function () {
135
+ it('adds a $ne filter for non existent key', function () {
136
136
  const params = {
137
137
  query: {},
138
- };
138
+ }
139
139
 
140
- const result = setQueryKeySafely(params, "test", true, "$ne");
140
+ const result = setQueryKeySafely(params, 'test', true, '$ne')
141
141
  assert.deepStrictEqual(result, {
142
142
  query: {
143
143
  test: {
144
144
  $ne: true,
145
145
  },
146
146
  },
147
- });
148
- });
147
+ })
148
+ })
149
149
 
150
- it("adds a $eq filter for existing key", function () {
150
+ it('adds a $eq filter for existing key', function () {
151
151
  const params: Params = {
152
152
  query: {
153
153
  test: true,
154
154
  },
155
- };
155
+ }
156
156
 
157
- const result = setQueryKeySafely(params, "test", false);
157
+ const result = setQueryKeySafely(params, 'test', false)
158
158
  assert.deepStrictEqual(result, {
159
159
  query: {
160
160
  test: true,
161
161
  $and: [{ test: false }],
162
162
  },
163
- });
164
- });
163
+ })
164
+ })
165
165
 
166
- it("adds a $in filter for existing key with value", function () {
166
+ it('adds a $in filter for existing key with value', function () {
167
167
  const params: Params = {
168
168
  query: {
169
169
  test: true,
170
170
  },
171
- };
171
+ }
172
172
 
173
- const result = setQueryKeySafely(params, "test", [true], "$in");
173
+ const result = setQueryKeySafely(params, 'test', [true], '$in')
174
174
  assert.deepStrictEqual(result, {
175
175
  query: {
176
176
  test: true,
177
177
  $and: [{ test: { $in: [true] } }],
178
178
  },
179
- });
180
- });
179
+ })
180
+ })
181
181
 
182
- it("adds a $in filter for existing key with object", function () {
182
+ it('adds a $in filter for existing key with object', function () {
183
183
  const params: Params = {
184
184
  query: {
185
185
  test: {
186
186
  $eq: true,
187
187
  },
188
188
  },
189
- };
189
+ }
190
190
 
191
- const result = setQueryKeySafely(params, "test", [true], "$in");
191
+ const result = setQueryKeySafely(params, 'test', [true], '$in')
192
192
  assert.deepStrictEqual(result, {
193
193
  query: {
194
194
  test: {
@@ -196,19 +196,19 @@ if (import.meta.vitest) {
196
196
  $in: [true],
197
197
  },
198
198
  },
199
- });
200
- });
199
+ })
200
+ })
201
201
 
202
- it("adds a $in filter for existing $in", function () {
202
+ it('adds a $in filter for existing $in', function () {
203
203
  const params: Params = {
204
204
  query: {
205
205
  test: {
206
206
  $in: [true],
207
207
  },
208
208
  },
209
- };
209
+ }
210
210
 
211
- const result = setQueryKeySafely(params, "test", [false], "$in");
211
+ const result = setQueryKeySafely(params, 'test', [false], '$in')
212
212
  assert.deepStrictEqual(result, {
213
213
  query: {
214
214
  test: {
@@ -216,6 +216,6 @@ if (import.meta.vitest) {
216
216
  },
217
217
  $and: [{ test: { $in: [false] } }],
218
218
  },
219
- });
220
- });
219
+ })
220
+ })
221
221
  }