@autofleet/sadot 0.5.4-beta.21 → 0.5.4-beta.22
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/api/v1/definition/index.js +1 -1
- package/dist/hooks/enrich.d.ts +1 -1
- package/dist/hooks/enrich.js +2 -2
- package/dist/repository/definition.js +1 -7
- package/dist/repository/value.js +2 -2
- package/dist/tests/mocks/testModel.d.ts +1 -1
- package/dist/tests/mocks/testModel.js +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/utils/init.js +3 -3
- package/dist/utils/scopeAttributes.d.ts +1 -1
- package/dist/utils/scopeAttributes.js +14 -7
- package/package.json +3 -3
- package/src/api/v1/definition/index.ts +1 -1
- package/src/hooks/enrich.ts +1 -2
- package/src/repository/definition.ts +2 -5
- package/src/repository/value.ts +2 -2
- package/src/tests/mocks/testModel.ts +1 -1
- package/src/types/index.ts +1 -1
- package/src/utils/init.ts +3 -3
- package/src/utils/scopeAttributes.ts +20 -9
|
@@ -83,7 +83,7 @@ router.get('/', async (req, res) => {
|
|
|
83
83
|
if (entityIds?.length > 0) {
|
|
84
84
|
where.entityId = entityIds;
|
|
85
85
|
}
|
|
86
|
-
const customFieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true });
|
|
86
|
+
const customFieldDefinitions = await DefinitionRepo.findAll({ ...where }, { withDisabled: true });
|
|
87
87
|
return res.json(customFieldDefinitions);
|
|
88
88
|
}
|
|
89
89
|
catch (err) {
|
package/dist/hooks/enrich.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
|
3
3
|
/**
|
|
4
4
|
* A hook to attach the custom fields when fetching a model instances.
|
|
5
5
|
*/
|
|
6
|
-
declare const enrichResults: (modelType: string, scopeAttributes: string[],
|
|
6
|
+
declare const enrichResults: (modelType: string, scopeAttributes: string[], hookType?: SupportedHookTypes, modelOptions?: ModelOptions) => (instancesOrInstance: any | any[], options: any) => Promise<void>;
|
|
7
7
|
export default enrichResults;
|
package/dist/hooks/enrich.js
CHANGED
|
@@ -45,7 +45,7 @@ const serializeCustomFields = (customFieldValues, customFieldDefinitionsHash) =>
|
|
|
45
45
|
/**
|
|
46
46
|
* A hook to attach the custom fields when fetching a model instances.
|
|
47
47
|
*/
|
|
48
|
-
const enrichResults = (modelType, scopeAttributes,
|
|
48
|
+
const enrichResults = (modelType, scopeAttributes, hookType, modelOptions = {}) => async (instancesOrInstance, options) => {
|
|
49
49
|
const { scopeAttributeReplacer } = modelOptions;
|
|
50
50
|
if (options.originalAttributes?.length > 0
|
|
51
51
|
&& !options.originalAttributes?.includes?.('customFields')) {
|
|
@@ -63,7 +63,7 @@ const enrichResults = (modelType, scopeAttributes, getScope, hookType, modelOpti
|
|
|
63
63
|
...map,
|
|
64
64
|
[identifier]: [],
|
|
65
65
|
}), {});
|
|
66
|
-
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(modelType, uniqueIdentifiers, { transaction: options.transaction
|
|
66
|
+
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(modelType, uniqueIdentifiers, { transaction: options.transaction });
|
|
67
67
|
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
68
68
|
...map,
|
|
69
69
|
[definition.id]: definition,
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.getRequiredFields = exports.destroy = exports.disable = exports.update = exports.findDefinitionsByModels = exports.findByWhere = exports.findByEntityIds = exports.findById = exports.findByIds = exports.findAll = exports.create = void 0;
|
|
7
4
|
const sequelize_1 = require("sequelize");
|
|
8
5
|
const models_1 = require("../models");
|
|
9
|
-
const logger_1 = __importDefault(require("../utils/logger"));
|
|
10
6
|
const create = (data) => models_1.CustomFieldDefinition.create(data);
|
|
11
7
|
exports.create = create;
|
|
12
8
|
const findAll = (where, options = { withDisabled: false }) => {
|
|
13
9
|
const queryModel = options.withDisabled
|
|
14
10
|
? models_1.CustomFieldDefinition.unscoped()
|
|
15
11
|
: models_1.CustomFieldDefinition;
|
|
16
|
-
logger_1.default.info('sadot - whereQuery', { options });
|
|
17
12
|
return queryModel.scope('userScope').findAll({
|
|
18
13
|
where,
|
|
14
|
+
transaction: options.transaction,
|
|
19
15
|
raw: true,
|
|
20
|
-
logging: true,
|
|
21
16
|
});
|
|
22
17
|
};
|
|
23
18
|
exports.findAll = findAll;
|
|
@@ -37,7 +32,6 @@ const findByEntityIds = async (modelType, entityIds, options = {}) => models_1.C
|
|
|
37
32
|
entityId: entityIds,
|
|
38
33
|
},
|
|
39
34
|
transaction: options.transaction,
|
|
40
|
-
include: options.include,
|
|
41
35
|
raw: true,
|
|
42
36
|
});
|
|
43
37
|
exports.findByEntityIds = findByEntityIds;
|
package/dist/repository/value.js
CHANGED
|
@@ -77,11 +77,11 @@ const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, opt
|
|
|
77
77
|
logger_1.default.info(`custom-fields: updating values for ${modelType} ${modelId}`, {
|
|
78
78
|
names, options, valuesToUpdate, entityId: identifiers,
|
|
79
79
|
});
|
|
80
|
-
const fieldDefinitions =
|
|
80
|
+
const fieldDefinitions = await DefinitionRepo.findAll({
|
|
81
81
|
modelType,
|
|
82
82
|
name: names,
|
|
83
83
|
entityId: identifiers,
|
|
84
|
-
}, { withDisabled: true, transaction: options.transaction })
|
|
84
|
+
}, { withDisabled: true, transaction: options.transaction }) || [];
|
|
85
85
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
86
86
|
if (fieldDefinitions.length !== names.length) {
|
|
87
87
|
logger_1.default.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TestModel } from '../../models';
|
|
2
|
-
export declare const createTestModel: (payload?: {}
|
|
2
|
+
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>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getTestModelWithAssociation = exports.createTestModelWithAssociation = exports.updateTestModel = exports.getSomeTestModels = exports.getTestModel = exports.destroyTestModels = exports.upsertTestModel = exports.createTestModels = exports.createTestModel = void 0;
|
|
4
4
|
const models_1 = require("../../models");
|
|
5
|
-
const createTestModel = (payload = {}
|
|
5
|
+
const createTestModel = (payload = {}) => models_1.TestModel.create(payload);
|
|
6
6
|
exports.createTestModel = createTestModel;
|
|
7
7
|
const createTestModels = (fleetId, total) => {
|
|
8
8
|
const models = [];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type ModelFetcher = (name: string) => any;
|
|
2
2
|
export type ModelOptions = {
|
|
3
|
-
scopeAttributeReplacer?: (attributes?: string[]) => Promise<
|
|
3
|
+
scopeAttributeReplacer?: (attributes?: string[]) => Promise<(string | undefined)[]>;
|
|
4
4
|
};
|
|
5
5
|
export type Models = {
|
|
6
6
|
name: string;
|
package/dist/utils/init.js
CHANGED
|
@@ -33,9 +33,9 @@ const addHooks = (models, getModel) => {
|
|
|
33
33
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
|
|
34
34
|
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes, modelOptions));
|
|
35
35
|
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes, modelOptions));
|
|
36
|
-
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind',
|
|
37
|
-
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null,
|
|
38
|
-
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null,
|
|
36
|
+
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind', modelOptions));
|
|
37
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions));
|
|
38
|
+
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions));
|
|
39
39
|
}
|
|
40
40
|
catch (e) {
|
|
41
41
|
logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ModelOptions } from '../types';
|
|
2
|
-
declare const getScopeAttributes: (instance: any, scopeAttributes: string[], scopeAttributeReplacer?: ModelOptions['scopeAttributeReplacer']) => Promise<
|
|
2
|
+
declare const getScopeAttributes: (instance: any, scopeAttributes: string[], scopeAttributeReplacer?: ModelOptions['scopeAttributeReplacer']) => Promise<string[]>;
|
|
3
3
|
export default getScopeAttributes;
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const attributeCache = new Map();
|
|
4
|
+
const mapAttributeToInstance = (scopeAttributes, instance) => scopeAttributes.map((attr) => instance[attr]);
|
|
5
|
+
const buildAttributesByInstance = (instance, scopeAttributes) => {
|
|
4
6
|
if (Array.isArray(instance)) {
|
|
5
|
-
return instance.
|
|
7
|
+
return instance.flatMap((ins) => mapAttributeToInstance(scopeAttributes, ins));
|
|
6
8
|
}
|
|
7
|
-
return scopeAttributes
|
|
9
|
+
return mapAttributeToInstance(scopeAttributes, instance);
|
|
8
10
|
};
|
|
9
11
|
const getScopeAttributes = async (instance, scopeAttributes, scopeAttributeReplacer) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
const attributes = buildAttributesByInstance(instance, scopeAttributes);
|
|
13
|
+
const attributeCacheKey = attributes.join();
|
|
14
|
+
const cachedAttributes = attributeCache.get(attributeCacheKey);
|
|
15
|
+
if (cachedAttributes) {
|
|
16
|
+
return cachedAttributes;
|
|
13
17
|
}
|
|
14
|
-
|
|
18
|
+
const attributeResult = (scopeAttributeReplacer
|
|
19
|
+
? await scopeAttributeReplacer(attributes) : attributes);
|
|
20
|
+
attributeCache.set(attributeCacheKey, attributeResult);
|
|
21
|
+
return attributeResult;
|
|
15
22
|
};
|
|
16
23
|
exports.default = getScopeAttributes;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sadot",
|
|
3
|
-
"version": "0.5.4-beta.
|
|
3
|
+
"version": "0.5.4-beta.22",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -35,7 +35,6 @@
|
|
|
35
35
|
"@autofleet/logger": "^2.0.5",
|
|
36
36
|
"express": "^4.18.2",
|
|
37
37
|
"joi": "^17.7.0",
|
|
38
|
-
"npm-watch": "^0.11.0",
|
|
39
38
|
"pg": "^8.10.0",
|
|
40
39
|
"reflect-metadata": "^0.1.13",
|
|
41
40
|
"sequelize": "^6.31.1",
|
|
@@ -52,6 +51,7 @@
|
|
|
52
51
|
"eslint-config-airbnb-typescript": "^12.0.0",
|
|
53
52
|
"eslint-plugin-import": "^2.22.1",
|
|
54
53
|
"jest": "^27.0.5",
|
|
54
|
+
"npm-watch": "^0.11.0",
|
|
55
55
|
"ts-jest": "^27.0.3",
|
|
56
56
|
"ts-node": "^8.6.2",
|
|
57
57
|
"typescript": "^4.9.5",
|
|
@@ -59,4 +59,4 @@
|
|
|
59
59
|
},
|
|
60
60
|
"author": "Autofleet",
|
|
61
61
|
"license": "ISC"
|
|
62
|
-
}
|
|
62
|
+
}
|
|
@@ -65,7 +65,7 @@ router.get('/', async (req, res) => {
|
|
|
65
65
|
if (entityIds?.length > 0) {
|
|
66
66
|
where.entityId = entityIds;
|
|
67
67
|
}
|
|
68
|
-
const customFieldDefinitions = await DefinitionRepo.findAll(where,
|
|
68
|
+
const customFieldDefinitions = await DefinitionRepo.findAll({ ...where },
|
|
69
69
|
{ withDisabled: true });
|
|
70
70
|
return res.json(customFieldDefinitions);
|
|
71
71
|
} catch (err) {
|
package/src/hooks/enrich.ts
CHANGED
|
@@ -32,7 +32,6 @@ const serializeCustomFields = (
|
|
|
32
32
|
const enrichResults = (
|
|
33
33
|
modelType: string,
|
|
34
34
|
scopeAttributes: string[],
|
|
35
|
-
getScope,
|
|
36
35
|
hookType?: SupportedHookTypes,
|
|
37
36
|
modelOptions: ModelOptions = {},
|
|
38
37
|
) => async (
|
|
@@ -68,7 +67,7 @@ const enrichResults = (
|
|
|
68
67
|
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(
|
|
69
68
|
modelType,
|
|
70
69
|
uniqueIdentifiers,
|
|
71
|
-
{ transaction: options.transaction
|
|
70
|
+
{ transaction: options.transaction },
|
|
72
71
|
);
|
|
73
72
|
|
|
74
73
|
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
} from 'sequelize';
|
|
4
4
|
import { CustomFieldDefinition } from '../models';
|
|
5
5
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
6
|
-
import logger from '../utils/logger';
|
|
7
6
|
|
|
8
7
|
export const create = (data: CreateCustomFieldDefinition): Promise<CustomFieldDefinition> =>
|
|
9
8
|
CustomFieldDefinition.create(data);
|
|
@@ -16,11 +15,10 @@ export const findAll = (
|
|
|
16
15
|
? CustomFieldDefinition.unscoped()
|
|
17
16
|
: CustomFieldDefinition;
|
|
18
17
|
|
|
19
|
-
logger.info('sadot - whereQuery', { options });
|
|
20
18
|
return queryModel.scope('userScope').findAll({
|
|
21
19
|
where,
|
|
20
|
+
transaction: options.transaction,
|
|
22
21
|
raw: true,
|
|
23
|
-
logging: true,
|
|
24
22
|
});
|
|
25
23
|
};
|
|
26
24
|
|
|
@@ -50,7 +48,6 @@ export const findByEntityIds = async (
|
|
|
50
48
|
entityId: entityIds,
|
|
51
49
|
},
|
|
52
50
|
transaction: options.transaction,
|
|
53
|
-
include: options.include,
|
|
54
51
|
raw: true,
|
|
55
52
|
});
|
|
56
53
|
|
|
@@ -108,6 +105,6 @@ export const getRequiredFields = async (
|
|
|
108
105
|
},
|
|
109
106
|
logging: true,
|
|
110
107
|
});
|
|
111
|
-
const requiredFieldsNames = requiredFields.map((definition
|
|
108
|
+
const requiredFieldsNames = requiredFields.map((definition) => definition.name);
|
|
112
109
|
return [...new Set(requiredFieldsNames)];
|
|
113
110
|
};
|
package/src/repository/value.ts
CHANGED
|
@@ -57,14 +57,14 @@ export const updateValues = async (
|
|
|
57
57
|
names, options, valuesToUpdate, entityId: identifiers,
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
-
const fieldDefinitions =
|
|
60
|
+
const fieldDefinitions = await DefinitionRepo.findAll(
|
|
61
61
|
{
|
|
62
62
|
modelType,
|
|
63
63
|
name: names,
|
|
64
64
|
entityId: identifiers,
|
|
65
65
|
},
|
|
66
66
|
{ withDisabled: true, transaction: options.transaction },
|
|
67
|
-
)
|
|
67
|
+
) || [];
|
|
68
68
|
|
|
69
69
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
70
70
|
if (fieldDefinitions.length !== names.length) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TestModel, AssociatedTestModel } from '../../models';
|
|
2
2
|
|
|
3
|
-
export const createTestModel = (payload = {}
|
|
3
|
+
export const createTestModel = (payload = {}) => TestModel.create(payload);
|
|
4
4
|
|
|
5
5
|
export const createTestModels = (fleetId, total: number) => {
|
|
6
6
|
const models: Promise<TestModel>[] = [];
|
package/src/types/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type ModelFetcher = (name: string) => any;
|
|
2
2
|
|
|
3
3
|
export type ModelOptions = {
|
|
4
|
-
scopeAttributeReplacer?: (attributes?: string[]) => Promise<
|
|
4
|
+
scopeAttributeReplacer?: (attributes?: string[]) => Promise<(string | undefined)[]>;
|
|
5
5
|
}
|
|
6
6
|
export type Models = {
|
|
7
7
|
name: string;
|
package/src/utils/init.ts
CHANGED
|
@@ -42,9 +42,9 @@ export const addHooks = (models: Models[], getModel: ModelFetcher): void => {
|
|
|
42
42
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', beforeBulkUpdate);
|
|
43
43
|
model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes, modelOptions));
|
|
44
44
|
model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes, modelOptions));
|
|
45
|
-
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind',
|
|
46
|
-
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes, null,
|
|
47
|
-
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes, null,
|
|
45
|
+
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind', modelOptions));
|
|
46
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes, null, modelOptions));
|
|
47
|
+
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes, null, modelOptions));
|
|
48
48
|
} catch (e) {
|
|
49
49
|
logger.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
50
50
|
}
|
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
import { ModelOptions } from '../types';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const attributeCache = new Map<string, string[]>();
|
|
4
|
+
|
|
5
|
+
const mapAttributeToInstance = (scopeAttributes: string[], instance: any) =>
|
|
6
|
+
scopeAttributes.map((attr) => instance[attr]);
|
|
7
|
+
|
|
8
|
+
const buildAttributesByInstance = (instance: any, scopeAttributes: string[]) => {
|
|
4
9
|
if (Array.isArray(instance)) {
|
|
5
|
-
return instance.
|
|
6
|
-
scopeAttributes.map((attr) => ins[attr])).flat();
|
|
10
|
+
return instance.flatMap((ins) => mapAttributeToInstance(scopeAttributes, ins));
|
|
7
11
|
}
|
|
8
|
-
return scopeAttributes
|
|
12
|
+
return mapAttributeToInstance(scopeAttributes, instance);
|
|
9
13
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
|
|
15
|
+
const getScopeAttributes = async (instance: any, scopeAttributes: string[],
|
|
16
|
+
scopeAttributeReplacer?: ModelOptions['scopeAttributeReplacer']): Promise<string[]> => {
|
|
17
|
+
const attributes = buildAttributesByInstance(instance, scopeAttributes);
|
|
18
|
+
const attributeCacheKey = attributes.join();
|
|
19
|
+
const cachedAttributes = attributeCache.get(attributeCacheKey);
|
|
20
|
+
if (cachedAttributes) {
|
|
21
|
+
return cachedAttributes;
|
|
14
22
|
}
|
|
15
|
-
|
|
23
|
+
const attributeResult = (scopeAttributeReplacer
|
|
24
|
+
? await scopeAttributeReplacer(attributes) : attributes);
|
|
25
|
+
attributeCache.set(attributeCacheKey, attributeResult);
|
|
26
|
+
return attributeResult;
|
|
16
27
|
};
|
|
17
28
|
|
|
18
29
|
export default getScopeAttributes;
|