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.
- package/README.md +3 -4
- package/dist/index.cjs +81 -99
- package/dist/index.d.cts +35 -33
- package/dist/index.d.mts +35 -33
- package/dist/index.d.ts +35 -33
- package/dist/index.mjs +81 -99
- package/package.json +29 -32
- package/src/.DS_Store +0 -0
- package/src/filters/array.ts +11 -13
- package/src/filters/index.ts +2 -2
- package/src/filters/object.ts +11 -11
- package/src/hooks/.DS_Store +0 -0
- package/src/hooks/checkMulti.ts +98 -82
- package/src/hooks/createRelated.ts +41 -41
- package/src/hooks/forEach.ts +32 -32
- package/src/hooks/from-client-for-server/common.ts +1 -1
- package/src/hooks/from-client-for-server/index.ts +2 -2
- package/src/hooks/from-client-for-server/paramsForServer.ts +32 -32
- package/src/hooks/from-client-for-server/paramsFromClient.ts +25 -25
- package/src/hooks/index.ts +9 -9
- package/src/hooks/onDelete.ts +54 -55
- package/src/hooks/parseFields.ts +13 -13
- package/src/hooks/removeRelated.ts +22 -20
- package/src/hooks/runPerItem.ts +17 -18
- package/src/hooks/setData.ts +295 -264
- package/src/index.ts +6 -6
- package/src/mixins/debounce-mixin/DebouncedStore.ts +29 -29
- package/src/mixins/debounce-mixin/debounceMixin.ts +17 -17
- package/src/mixins/debounce-mixin/index.ts +3 -3
- package/src/mixins/debounce-mixin/types.ts +9 -9
- package/src/mixins/debounce-mixin/utils.ts +3 -3
- package/src/mixins/index.ts +1 -1
- package/src/types.ts +3 -5
- package/src/typesInternal.ts +14 -14
- package/src/utility-types/index.ts +48 -48
- package/src/utils/_utils.internal.ts +5 -5
- package/src/utils/defineHooks.ts +8 -8
- package/src/utils/deflattenQuery.ts +31 -31
- package/src/utils/filterQuery.ts +58 -58
- package/src/utils/flattenQuery.ts +54 -54
- package/src/utils/getItemsIsArray.ts +148 -149
- package/src/utils/getPaginate.ts +31 -31
- package/src/utils/index.ts +17 -17
- package/src/utils/isMulti.ts +48 -40
- package/src/utils/isPaginated.ts +30 -30
- package/src/utils/markHookForSkip.ts +177 -178
- package/src/utils/mergeQuery/index.ts +3 -3
- package/src/utils/mergeQuery/mergeArrays.ts +67 -67
- package/src/utils/mergeQuery/mergeQuery.ts +211 -211
- package/src/utils/mergeQuery/types.ts +12 -12
- package/src/utils/mergeQuery/utils.ts +224 -224
- package/src/utils/optimizeBatchPatch.ts +42 -42
- package/src/utils/pushSet.ts +57 -57
- package/src/utils/setQueryKeySafely.ts +68 -68
- package/src/utils/setResultEmpty.ts +125 -123
- package/src/utils/shouldSkip.ts +72 -72
- package/src/utils/toJSON.ts +4 -4
- package/src/utils/validateQueryProperty.ts +10 -10
- package/src/hooks/makeSequelizeQuery.ts_ +0 -90
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BadRequest } from
|
|
2
|
-
import type { Query } from
|
|
3
|
-
import { isObject } from
|
|
1
|
+
import { BadRequest } from '@feathersjs/errors'
|
|
2
|
+
import type { Query } from '@feathersjs/feathers'
|
|
3
|
+
import { isObject } from './_utils.internal.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* util to validate a query for operators
|
|
@@ -10,22 +10,22 @@ export const validateQueryProperty = (
|
|
|
10
10
|
operators: string[] = [],
|
|
11
11
|
): Query => {
|
|
12
12
|
if (!isObject(query)) {
|
|
13
|
-
return query
|
|
13
|
+
return query
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
for (const key of Object.keys(query)) {
|
|
17
|
-
if (key.startsWith(
|
|
18
|
-
throw new BadRequest(`Invalid query parameter ${key}`, query)
|
|
17
|
+
if (key.startsWith('$') && !operators.includes(key)) {
|
|
18
|
+
throw new BadRequest(`Invalid query parameter ${key}`, query)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
const value = query[key]
|
|
21
|
+
const value = query[key]
|
|
22
22
|
|
|
23
23
|
if (isObject(value)) {
|
|
24
|
-
query[key] = validateQueryProperty(value, operators)
|
|
24
|
+
query[key] = validateQueryProperty(value, operators)
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
return {
|
|
29
29
|
...query,
|
|
30
|
-
}
|
|
31
|
-
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import _get from "lodash/get.js";
|
|
2
|
-
import _isObject from "lodash/isObject.js";
|
|
3
|
-
import _transform from "lodash/transform.js";
|
|
4
|
-
import _intersection from "lodash/intersection.js";
|
|
5
|
-
import { HookContext, Query } from "@feathersjs/feathers";
|
|
6
|
-
|
|
7
|
-
import { Model } from "sequelize";
|
|
8
|
-
|
|
9
|
-
function replaceKeysDeep(
|
|
10
|
-
obj: Record<string, unknown>,
|
|
11
|
-
keysMap: Record<string, string>
|
|
12
|
-
) { // keysMap = { oldKey1: newKey1, oldKey2: newKey2, etc...
|
|
13
|
-
return _transform(obj, function(
|
|
14
|
-
result: Record<string, unknown>,
|
|
15
|
-
value,
|
|
16
|
-
key
|
|
17
|
-
) { // transform to a new object
|
|
18
|
-
|
|
19
|
-
const currentKey = keysMap[key] || key; // if the key is in keysMap use the replacement, if not use the original key
|
|
20
|
-
|
|
21
|
-
result[currentKey] = _isObject(value) ? replaceKeysDeep(value as Record<string, unknown>, keysMap) : value; // if the key is an object run it through the inner function - replaceKeys
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const makeIncludeArray = (
|
|
26
|
-
query: Query,
|
|
27
|
-
Model: Model,
|
|
28
|
-
operators
|
|
29
|
-
) => {
|
|
30
|
-
const queryKeys = Object.keys(query);
|
|
31
|
-
const { associations: associationsByKey } = Model;
|
|
32
|
-
const associationsKeys = Object.keys(associationsByKey);
|
|
33
|
-
|
|
34
|
-
const associationKeysInQuery = _intersection(queryKeys, associationsKeys);
|
|
35
|
-
|
|
36
|
-
const result = [];
|
|
37
|
-
|
|
38
|
-
for (let i = 0, n = associationKeysInQuery.length; i < n; i++) {
|
|
39
|
-
const key = associationKeysInQuery[i];
|
|
40
|
-
const model = associationsByKey[key].target;
|
|
41
|
-
const where = operators ? replaceKeysDeep(query[key], operators) : query[key];
|
|
42
|
-
const as = key;
|
|
43
|
-
const include = makeIncludeArray(where, model, operators);
|
|
44
|
-
delete query[key];
|
|
45
|
-
|
|
46
|
-
const association = {
|
|
47
|
-
model,
|
|
48
|
-
as,
|
|
49
|
-
attributes: [],
|
|
50
|
-
required: true
|
|
51
|
-
};
|
|
52
|
-
if (include.length > 0) {
|
|
53
|
-
association.include = include;
|
|
54
|
-
}
|
|
55
|
-
if (Object.keys(where).length > 0) {
|
|
56
|
-
association.where = where;
|
|
57
|
-
}
|
|
58
|
-
if (include.length > 0 || Object.keys(where).length > 0) {
|
|
59
|
-
result.push(association);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return result;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const makeSequelizeQuery = () => {
|
|
67
|
-
return (context: HookContext): HookContext => {
|
|
68
|
-
const { params } = context;
|
|
69
|
-
const { query = {} } = params;
|
|
70
|
-
|
|
71
|
-
const { Model } = context.service;
|
|
72
|
-
if (!Model || !Model.sequelize) { return context; }
|
|
73
|
-
const operators = _get(context, "service.options.operators");
|
|
74
|
-
|
|
75
|
-
const include = makeIncludeArray(query, Model, operators);
|
|
76
|
-
|
|
77
|
-
if (include.length > 0) {
|
|
78
|
-
params.sequelize = params.sequelize || { include: [] };
|
|
79
|
-
if (params.sequelize.include) {
|
|
80
|
-
params.sequelize.include.push(...include);
|
|
81
|
-
} else {
|
|
82
|
-
params.sequelize.include = include;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return context;
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export default makeSequelizeQuery;
|