@autofleet/sadot 0.4.2 → 0.5.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.
- package/dist/hooks/enrich.d.ts +2 -1
- package/dist/hooks/enrich.js +6 -1
- package/dist/scopes/filter.js +13 -2
- package/dist/tests/mocks/testModel.d.ts +1 -1
- package/dist/utils/init.js +1 -1
- package/package.json +1 -1
- package/src/hooks/enrich.ts +12 -1
- package/src/scopes/filter.ts +14 -2
- package/src/tests/mocks/testModel.ts +2 -2
- package/src/utils/init.ts +1 -1
package/dist/hooks/enrich.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
1
2
|
/**
|
|
2
3
|
* A hook to attach the custom fields when fetching a model instances.
|
|
3
4
|
*/
|
|
4
|
-
declare const enrichResults: (modelType: string, scopeAttributes: string[]) => (instancesOrInstance: any | any[], options: any) => Promise<void>;
|
|
5
|
+
declare const enrichResults: (modelType: string, scopeAttributes: string[], hookType?: SupportedHookTypes) => (instancesOrInstance: any | any[], options: any) => Promise<void>;
|
|
5
6
|
export default enrichResults;
|
package/dist/hooks/enrich.js
CHANGED
|
@@ -40,7 +40,7 @@ const serializeCustomFields = (customFieldValues, customFieldDefinitionsHash) =>
|
|
|
40
40
|
/**
|
|
41
41
|
* A hook to attach the custom fields when fetching a model instances.
|
|
42
42
|
*/
|
|
43
|
-
const enrichResults = (modelType, scopeAttributes) => async (instancesOrInstance, options) => {
|
|
43
|
+
const enrichResults = (modelType, scopeAttributes, hookType) => async (instancesOrInstance, options) => {
|
|
44
44
|
if (options.originalAttributes?.length > 0
|
|
45
45
|
&& !options.originalAttributes?.includes?.('customFields')) {
|
|
46
46
|
return;
|
|
@@ -103,6 +103,11 @@ const enrichResults = (modelType, scopeAttributes) => async (instancesOrInstance
|
|
|
103
103
|
// if raw:
|
|
104
104
|
delete instance?.[attribute];
|
|
105
105
|
});
|
|
106
|
+
// sequelize will think customFields changed also in 'find', so we need to mark it as unchanged
|
|
107
|
+
if (hookType === 'afterFind') {
|
|
108
|
+
// changed() could be undefined, i.e in raw: true
|
|
109
|
+
instance?.changed?.('customFields', false);
|
|
110
|
+
}
|
|
106
111
|
});
|
|
107
112
|
};
|
|
108
113
|
exports.default = enrichResults;
|
package/dist/scopes/filter.js
CHANGED
|
@@ -13,16 +13,27 @@ const constants_1 = require("../utils/constants");
|
|
|
13
13
|
* @returns {Function} - A function that takes conditions and returns the Sequelize options object.
|
|
14
14
|
*/
|
|
15
15
|
const customFieldsFilterScope = (name) => (conditions) => {
|
|
16
|
+
if (!conditions || Object.keys(conditions).length === 0) {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
16
19
|
// Build the WHERE clause for custom field filtering
|
|
17
|
-
const
|
|
20
|
+
const conditionsStrings = Object.entries(conditions)
|
|
18
21
|
.map(([key, value]) => {
|
|
19
22
|
if (Array.isArray(value)) {
|
|
23
|
+
if (value.length === 0) {
|
|
24
|
+
// if empty array, the condition is ignored
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
20
27
|
const values = value.map((v) => `'${v}'`).join(',');
|
|
21
28
|
return `custom_fields->>'${key}' IN (${values})`;
|
|
22
29
|
}
|
|
23
30
|
return `custom_fields->>'${key}' = '${value}'`;
|
|
24
31
|
})
|
|
25
|
-
.
|
|
32
|
+
.filter(Boolean);
|
|
33
|
+
if (conditionsStrings.length === 0) {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
const customFieldConditions = conditionsStrings.join(' AND ');
|
|
26
37
|
const subQuery = `${'SELECT model_id FROM ('
|
|
27
38
|
+ 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
|
|
28
39
|
+ 'FROM custom_field_values AS cv '
|
|
@@ -3,7 +3,7 @@ export declare const createTestModel: (payload?: {}) => Promise<TestModel>;
|
|
|
3
3
|
export declare const createTestModels: (fleetId: any, total: number) => Promise<TestModel[]>;
|
|
4
4
|
export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean]>;
|
|
5
5
|
export declare const destroyTestModels: () => Promise<number>;
|
|
6
|
-
export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel>;
|
|
6
|
+
export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel | null>;
|
|
7
7
|
export declare const getSomeTestModels: (options?: {
|
|
8
8
|
limit: number;
|
|
9
9
|
}) => Promise<TestModel[]>;
|
package/dist/utils/init.js
CHANGED
|
@@ -32,7 +32,7 @@ const addHooks = (models, getModel) => {
|
|
|
32
32
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
|
|
33
33
|
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes));
|
|
34
34
|
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes));
|
|
35
|
-
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
35
|
+
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind'));
|
|
36
36
|
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
37
37
|
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
38
38
|
}
|
package/package.json
CHANGED
package/src/hooks/enrich.ts
CHANGED
|
@@ -5,6 +5,8 @@ import CustomFieldValue from '../models/CustomFieldValue';
|
|
|
5
5
|
import CustomFieldDefinition from '../models/CustomFieldDefinition';
|
|
6
6
|
import { SerializedCustomFields } from '../types/definition';
|
|
7
7
|
|
|
8
|
+
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* Serialize custom fields value into the format of {[name] -> [fieldData]}
|
|
10
12
|
*/
|
|
@@ -24,7 +26,11 @@ const serializeCustomFields = (
|
|
|
24
26
|
/**
|
|
25
27
|
* A hook to attach the custom fields when fetching a model instances.
|
|
26
28
|
*/
|
|
27
|
-
const enrichResults = (
|
|
29
|
+
const enrichResults = (
|
|
30
|
+
modelType: string,
|
|
31
|
+
scopeAttributes: string[],
|
|
32
|
+
hookType?: SupportedHookTypes,
|
|
33
|
+
) => async (
|
|
28
34
|
instancesOrInstance: any | any[],
|
|
29
35
|
options,
|
|
30
36
|
): Promise<void> => {
|
|
@@ -113,6 +119,11 @@ const enrichResults = (modelType: string, scopeAttributes: string[]) => async (
|
|
|
113
119
|
// if raw:
|
|
114
120
|
delete instance?.[attribute];
|
|
115
121
|
});
|
|
122
|
+
// sequelize will think customFields changed also in 'find', so we need to mark it as unchanged
|
|
123
|
+
if (hookType === 'afterFind') {
|
|
124
|
+
// changed() could be undefined, i.e in raw: true
|
|
125
|
+
instance?.changed?.('customFields', false);
|
|
126
|
+
}
|
|
116
127
|
});
|
|
117
128
|
};
|
|
118
129
|
|
package/src/scopes/filter.ts
CHANGED
|
@@ -29,18 +29,30 @@ export type CustomFieldFilterOptions = {
|
|
|
29
29
|
export const customFieldsFilterScope = (
|
|
30
30
|
name: string,
|
|
31
31
|
) => (conditions: Record<string, ConditionValue>): CustomFieldFilterOptions => {
|
|
32
|
+
if (!conditions || Object.keys(conditions).length === 0) {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
32
35
|
// Build the WHERE clause for custom field filtering
|
|
33
|
-
const
|
|
36
|
+
const conditionsStrings = Object.entries(conditions)
|
|
34
37
|
.map(
|
|
35
38
|
([key, value]) => {
|
|
36
39
|
if (Array.isArray(value)) {
|
|
40
|
+
if (value.length === 0) {
|
|
41
|
+
// if empty array, the condition is ignored
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
37
44
|
const values = value.map((v) => `'${v}'`).join(',');
|
|
38
45
|
return `custom_fields->>'${key}' IN (${values})`;
|
|
39
46
|
}
|
|
40
47
|
return `custom_fields->>'${key}' = '${value}'`;
|
|
41
48
|
},
|
|
42
49
|
)
|
|
43
|
-
.
|
|
50
|
+
.filter(Boolean);
|
|
51
|
+
|
|
52
|
+
if (conditionsStrings.length === 0) {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
const customFieldConditions = conditionsStrings.join(' AND ');
|
|
44
56
|
|
|
45
57
|
const subQuery = `${'SELECT model_id FROM ('
|
|
46
58
|
+ 'SELECT cv.model_id, jsonb_object_agg(cd.name, cv.value) AS custom_fields '
|
|
@@ -3,7 +3,7 @@ import { TestModel, AssociatedTestModel } from '../../models';
|
|
|
3
3
|
export const createTestModel = (payload = {}) => TestModel.create(payload);
|
|
4
4
|
|
|
5
5
|
export const createTestModels = (fleetId, total: number) => {
|
|
6
|
-
const models = [];
|
|
6
|
+
const models: Promise<TestModel>[] = [];
|
|
7
7
|
for (let i = 0; i < total; i += 1) {
|
|
8
8
|
models.push(createTestModel({ fleetId }));
|
|
9
9
|
}
|
|
@@ -17,7 +17,7 @@ export const destroyTestModels = () => TestModel.destroy({ truncate: true });
|
|
|
17
17
|
export const getTestModel = (
|
|
18
18
|
id: string,
|
|
19
19
|
options = {},
|
|
20
|
-
): Promise<TestModel> => TestModel.findByPk(id, options);
|
|
20
|
+
): Promise<TestModel | null> => TestModel.findByPk(id, options);
|
|
21
21
|
|
|
22
22
|
export const getSomeTestModels = (
|
|
23
23
|
options = { limit: 1 },
|
package/src/utils/init.ts
CHANGED
|
@@ -38,7 +38,7 @@ export const addHooks = (models: ModelOptions[], getModel: ModelFetcher): void =
|
|
|
38
38
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', beforeBulkUpdate);
|
|
39
39
|
model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes));
|
|
40
40
|
model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes));
|
|
41
|
-
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes));
|
|
41
|
+
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind'));
|
|
42
42
|
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes));
|
|
43
43
|
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes));
|
|
44
44
|
} catch (e) {
|