@autofleet/sadot 0.5.4-beta.25 → 0.5.4-beta.3
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/create.d.ts +1 -2
- package/dist/hooks/create.js +4 -8
- package/dist/hooks/enrich.d.ts +1 -2
- package/dist/hooks/enrich.js +5 -11
- package/dist/hooks/update.d.ts +1 -2
- package/dist/hooks/update.js +3 -4
- package/dist/index.js +0 -1
- package/dist/models/index.d.ts +1 -3
- package/dist/models/index.js +2 -8
- package/dist/repository/definition.d.ts +4 -7
- package/dist/repository/definition.js +15 -28
- package/dist/repository/value.d.ts +1 -5
- package/dist/repository/value.js +3 -12
- package/dist/tests/helpers/database-config.d.ts +0 -1
- package/dist/tests/helpers/database-config.js +0 -1
- package/dist/tests/helpers/index.js +0 -2
- package/dist/tests/mocks/definition.mock.d.ts +0 -6
- package/dist/tests/mocks/definition.mock.js +1 -7
- package/dist/types/index.d.ts +2 -20
- package/dist/utils/init.d.ts +4 -5
- package/dist/utils/init.js +7 -13
- package/package.json +2 -20
- package/src/api/v1/definition/index.ts +4 -2
- package/src/hooks/create.ts +5 -13
- package/src/hooks/enrich.ts +5 -15
- package/src/hooks/update.ts +3 -6
- package/src/index.ts +1 -4
- package/src/models/index.ts +1 -7
- package/src/repository/definition.ts +19 -39
- package/src/repository/value.ts +6 -17
- package/src/tests/helpers/database-config.ts +0 -1
- package/src/tests/helpers/index.ts +1 -5
- package/src/tests/mocks/definition.mock.ts +0 -7
- package/src/types/index.ts +3 -25
- package/src/utils/init.ts +10 -19
- package/.env +0 -3
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +0 -10
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +0 -55
- package/dist/models/tests/contextAwareModels/ContextModel.d.ts +0 -13
- package/dist/models/tests/contextAwareModels/ContextModel.js +0 -47
- package/dist/utils/scopeAttributes.d.ts +0 -2
- package/dist/utils/scopeAttributes.js +0 -11
- package/src/models/tests/contextAwareModels/ContextAwareTestModel.ts +0 -45
- package/src/models/tests/contextAwareModels/ContextModel.ts +0 -38
- package/src/utils/scopeAttributes.ts +0 -12
package/dist/hooks/create.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ModelOptions } from '../types';
|
|
2
1
|
/**
|
|
3
2
|
* A hook to create the custom fields when updating a model (more then one instance).
|
|
4
3
|
*/
|
|
@@ -7,4 +6,4 @@ export declare const beforeBulkCreate: (options: any) => void;
|
|
|
7
6
|
* A hook to create the custom fields when updating a model instance.
|
|
8
7
|
* TODO - cleanup if update fail
|
|
9
8
|
*/
|
|
10
|
-
export declare const beforeCreate: (scopeAttributes: string[]
|
|
9
|
+
export declare const beforeCreate: (scopeAttributes: string[]) => (instance: any, options: any) => Promise<void>;
|
package/dist/hooks/create.js
CHANGED
|
@@ -31,7 +31,6 @@ const logger_1 = __importDefault(require("../utils/logger"));
|
|
|
31
31
|
const ValueRepo = __importStar(require("../repository/value"));
|
|
32
32
|
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
33
33
|
const errors_1 = require("../errors");
|
|
34
|
-
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
35
34
|
/**
|
|
36
35
|
* A hook to create the custom fields when updating a model (more then one instance).
|
|
37
36
|
*/
|
|
@@ -45,13 +44,13 @@ exports.beforeBulkCreate = beforeBulkCreate;
|
|
|
45
44
|
* A hook to create the custom fields when updating a model instance.
|
|
46
45
|
* TODO - cleanup if update fail
|
|
47
46
|
*/
|
|
48
|
-
const beforeCreate = (scopeAttributes
|
|
47
|
+
const beforeCreate = (scopeAttributes) => async (instance, options) => {
|
|
49
48
|
logger_1.default.debug('sadot - before create hook');
|
|
50
49
|
const { fields } = options;
|
|
51
50
|
const modelType = instance.constructor.name;
|
|
52
|
-
const identifiers = (
|
|
51
|
+
const identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
53
52
|
// get all model's required definitions
|
|
54
|
-
const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType, instance.id, identifiers
|
|
53
|
+
const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType, instance.id, identifiers);
|
|
55
54
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
56
55
|
const { customFields } = instance;
|
|
57
56
|
if (customFieldsIdx > -1 && customFields) {
|
|
@@ -60,10 +59,7 @@ const beforeCreate = (scopeAttributes, modelOptions = {}) => async (instance, op
|
|
|
60
59
|
if (missingFields?.length > 0) {
|
|
61
60
|
throw new errors_1.MissingRequiredCustomFieldError(missingFields);
|
|
62
61
|
}
|
|
63
|
-
await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, {
|
|
64
|
-
transaction: options.transaction,
|
|
65
|
-
modelOptions,
|
|
66
|
-
});
|
|
62
|
+
await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, { transaction: options.transaction });
|
|
67
63
|
// eslint-disable-next-line no-param-reassign
|
|
68
64
|
fields.splice(customFieldsIdx, 1);
|
|
69
65
|
}
|
package/dist/hooks/enrich.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { ModelOptions } from '../types';
|
|
2
1
|
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
3
2
|
/**
|
|
4
3
|
* A hook to attach the custom fields when fetching a model instances.
|
|
5
4
|
*/
|
|
6
|
-
declare const enrichResults: (modelType: string, scopeAttributes: string[], hookType?: SupportedHookTypes
|
|
5
|
+
declare const enrichResults: (modelType: string, scopeAttributes: string[], hookType?: SupportedHookTypes) => (instancesOrInstance: any | any[], options: any) => Promise<void>;
|
|
7
6
|
export default enrichResults;
|
package/dist/hooks/enrich.js
CHANGED
|
@@ -22,15 +22,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
26
|
/* eslint-disable no-param-reassign */
|
|
30
27
|
const ValueRepo = __importStar(require("../repository/value"));
|
|
31
28
|
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
32
|
-
const logger_1 = __importDefault(require("../utils/logger"));
|
|
33
|
-
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
34
29
|
/**
|
|
35
30
|
* Serialize custom fields value into the format of {[name] -> [fieldData]}
|
|
36
31
|
*/
|
|
@@ -45,7 +40,7 @@ const serializeCustomFields = (customFieldValues, customFieldDefinitionsHash) =>
|
|
|
45
40
|
/**
|
|
46
41
|
* A hook to attach the custom fields when fetching a model instances.
|
|
47
42
|
*/
|
|
48
|
-
const enrichResults = (modelType, scopeAttributes, hookType
|
|
43
|
+
const enrichResults = (modelType, scopeAttributes, hookType) => async (instancesOrInstance, options) => {
|
|
49
44
|
if (options.originalAttributes?.length > 0
|
|
50
45
|
&& !options.originalAttributes?.includes?.('customFields')) {
|
|
51
46
|
return;
|
|
@@ -55,21 +50,20 @@ const enrichResults = (modelType, scopeAttributes, hookType, modelOptions = {})
|
|
|
55
50
|
? instancesOrInstance
|
|
56
51
|
: [instancesOrInstance];
|
|
57
52
|
instances = instances.filter(Boolean);
|
|
58
|
-
const identifiers = (
|
|
59
|
-
|
|
53
|
+
const identifiers = instances.map((instance) => scopeAttributes
|
|
54
|
+
.map((attr) => instance[attr])).flat();
|
|
60
55
|
const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
|
|
61
|
-
console.log({ uniqueIdentifiers });
|
|
62
56
|
const identifierCustomFieldDefinitionsMapping = uniqueIdentifiers.reduce((map, identifier) => ({
|
|
63
57
|
...map,
|
|
64
58
|
[identifier]: [],
|
|
65
59
|
}), {});
|
|
66
|
-
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(modelType, uniqueIdentifiers, { transaction: options.transaction
|
|
60
|
+
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(modelType, uniqueIdentifiers, { transaction: options.transaction });
|
|
67
61
|
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
68
62
|
...map,
|
|
69
63
|
[definition.id]: definition,
|
|
70
64
|
}), {});
|
|
71
65
|
customFieldDefinitions.forEach((cfd) => {
|
|
72
|
-
identifierCustomFieldDefinitionsMapping[cfd.entityId]
|
|
66
|
+
identifierCustomFieldDefinitionsMapping[cfd.entityId].push(cfd);
|
|
73
67
|
});
|
|
74
68
|
// Get the values per instates ids:
|
|
75
69
|
const instancesIds = instances.map((i) => i[primaryKey]);
|
package/dist/hooks/update.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ModelOptions } from '../types';
|
|
2
1
|
/**
|
|
3
2
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
4
3
|
*/
|
|
@@ -7,4 +6,4 @@ export declare const beforeBulkUpdate: (options: any) => void;
|
|
|
7
6
|
* A hook to update the custom fields when updating a model instance.
|
|
8
7
|
* TODO - cleanup if update fail
|
|
9
8
|
*/
|
|
10
|
-
export declare const beforeUpdate: (scopeAttributes: string[]
|
|
9
|
+
export declare const beforeUpdate: (scopeAttributes: string[]) => (instance: any, options: any) => Promise<void>;
|
package/dist/hooks/update.js
CHANGED
|
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.beforeUpdate = exports.beforeBulkUpdate = void 0;
|
|
30
30
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
31
31
|
const ValueRepo = __importStar(require("../repository/value"));
|
|
32
|
-
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
33
32
|
/**
|
|
34
33
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
35
34
|
*/
|
|
@@ -43,15 +42,15 @@ exports.beforeBulkUpdate = beforeBulkUpdate;
|
|
|
43
42
|
* A hook to update the custom fields when updating a model instance.
|
|
44
43
|
* TODO - cleanup if update fail
|
|
45
44
|
*/
|
|
46
|
-
const beforeUpdate = (scopeAttributes
|
|
45
|
+
const beforeUpdate = (scopeAttributes) => async (instance, options) => {
|
|
47
46
|
logger_1.default.debug('sadot - before update hook');
|
|
48
47
|
const { fields } = options;
|
|
49
48
|
const modelType = instance.constructor.name;
|
|
50
|
-
const identifiers = (
|
|
49
|
+
const identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
51
50
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
52
51
|
if (customFieldsIdx > -1) {
|
|
53
52
|
const { customFields } = instance;
|
|
54
|
-
await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, { transaction: options.transaction
|
|
53
|
+
await ValueRepo.updateValues(modelType, instance.id, identifiers, customFields, { transaction: options.transaction });
|
|
55
54
|
// eslint-disable-next-line no-param-reassign
|
|
56
55
|
fields.splice(customFieldsIdx, 1);
|
|
57
56
|
}
|
package/dist/index.js
CHANGED
|
@@ -42,7 +42,6 @@ const useCustomFields = async (app, getModel, options) => {
|
|
|
42
42
|
(0, init_1.addHooks)(models, getModel);
|
|
43
43
|
await (0, models_1.initTables)(sequelize, options.getUser);
|
|
44
44
|
(0, init_1.addScopes)(models, getModel);
|
|
45
|
-
(0, init_1.applyCustomAssociation)(models);
|
|
46
45
|
logger_1.default.debug('sadot - custom fields finished initializing with models', models);
|
|
47
46
|
return sequelize;
|
|
48
47
|
};
|
package/dist/models/index.d.ts
CHANGED
|
@@ -2,9 +2,7 @@ import type { Sequelize } from 'sequelize-typescript';
|
|
|
2
2
|
import CustomFieldDefinition from './CustomFieldDefinition';
|
|
3
3
|
import CustomFieldValue from './CustomFieldValue';
|
|
4
4
|
import TestModel from './tests/TestModel';
|
|
5
|
-
import ContextAwareTestModel from './tests/contextAwareModels/ContextAwareTestModel';
|
|
6
|
-
import ContextModel from './tests/contextAwareModels/ContextModel';
|
|
7
5
|
import AssociatedTestModel from './tests/AssociatedTestModel';
|
|
8
6
|
declare const initTables: (sequelize: Sequelize, getUser: any) => Promise<void>;
|
|
9
7
|
declare const initTestModels: (sequelize: Sequelize) => Promise<void>;
|
|
10
|
-
export { CustomFieldValue, CustomFieldDefinition, TestModel, AssociatedTestModel,
|
|
8
|
+
export { CustomFieldValue, CustomFieldDefinition, TestModel, AssociatedTestModel, initTables, initTestModels, };
|
package/dist/models/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.initTestModels = exports.initTables = exports.
|
|
6
|
+
exports.initTestModels = exports.initTables = exports.AssociatedTestModel = exports.TestModel = exports.CustomFieldDefinition = exports.CustomFieldValue = void 0;
|
|
7
7
|
/* eslint-disable no-param-reassign */
|
|
8
8
|
const sequelize_1 = require("sequelize");
|
|
9
9
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
@@ -13,14 +13,10 @@ const CustomFieldValue_1 = __importDefault(require("./CustomFieldValue"));
|
|
|
13
13
|
exports.CustomFieldValue = CustomFieldValue_1.default;
|
|
14
14
|
const TestModel_1 = __importDefault(require("./tests/TestModel"));
|
|
15
15
|
exports.TestModel = TestModel_1.default;
|
|
16
|
-
const ContextAwareTestModel_1 = __importDefault(require("./tests/contextAwareModels/ContextAwareTestModel"));
|
|
17
|
-
exports.ContextAwareTestModel = ContextAwareTestModel_1.default;
|
|
18
|
-
const ContextModel_1 = __importDefault(require("./tests/contextAwareModels/ContextModel"));
|
|
19
|
-
exports.ContextModel = ContextModel_1.default;
|
|
20
16
|
const AssociatedTestModel_1 = __importDefault(require("./tests/AssociatedTestModel"));
|
|
21
17
|
exports.AssociatedTestModel = AssociatedTestModel_1.default;
|
|
22
18
|
const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.default];
|
|
23
|
-
const testModels = [TestModel_1.default, AssociatedTestModel_1.default
|
|
19
|
+
const testModels = [TestModel_1.default, AssociatedTestModel_1.default];
|
|
24
20
|
const SADOT_MIGRATION_PREFIX = 'sadot-migration';
|
|
25
21
|
const SCHEMA_VERSION = 1;
|
|
26
22
|
const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
|
|
@@ -86,8 +82,6 @@ const initTestModels = async (sequelize) => {
|
|
|
86
82
|
logger_1.default.info('custom-fields: test models added');
|
|
87
83
|
await TestModel_1.default.sync({ alter: true });
|
|
88
84
|
await AssociatedTestModel_1.default.sync({ alter: true });
|
|
89
|
-
await ContextModel_1.default.sync({ alter: true });
|
|
90
|
-
await ContextAwareTestModel_1.default.sync({ alter: true });
|
|
91
85
|
logger_1.default.info('custom-fields: test models synced');
|
|
92
86
|
};
|
|
93
87
|
exports.initTestModels = initTestModels;
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { FindOptions, WhereOptions } from 'sequelize';
|
|
2
1
|
import { CustomFieldDefinition } from '../models';
|
|
3
2
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
4
|
-
import { ModelOptions } from '../types';
|
|
5
3
|
export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
6
|
-
export declare const findAll: (where:
|
|
4
|
+
export declare const findAll: (where: any, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
7
5
|
export declare const findByIds: (ids: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
8
6
|
export declare const findById: (id: string, options?: any) => Promise<CustomFieldDefinition | null>;
|
|
9
|
-
export declare const
|
|
10
|
-
|
|
11
|
-
}) => Promise<CustomFieldDefinition[]>;
|
|
7
|
+
export declare const findByEntityId: (entityId: string, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
8
|
+
export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
12
9
|
export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
|
|
13
10
|
export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
14
11
|
export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
@@ -17,4 +14,4 @@ export declare const destroy: (id: string) => Promise<any>;
|
|
|
17
14
|
/**
|
|
18
15
|
* Return the names of the required fields for a given model
|
|
19
16
|
*/
|
|
20
|
-
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[]
|
|
17
|
+
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[]) => Promise<string[]>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
3
|
+
exports.getRequiredFields = exports.destroy = exports.disable = exports.update = exports.findDefinitionsByModels = exports.findByWhere = exports.findByEntityIds = exports.findByEntityId = exports.findById = exports.findByIds = exports.findAll = exports.create = void 0;
|
|
4
4
|
const sequelize_1 = require("sequelize");
|
|
5
5
|
const models_1 = require("../models");
|
|
6
6
|
const create = (data) => models_1.CustomFieldDefinition.create(data);
|
|
@@ -13,8 +13,6 @@ const findAll = (where, options = { withDisabled: false }) => {
|
|
|
13
13
|
where,
|
|
14
14
|
transaction: options.transaction,
|
|
15
15
|
raw: true,
|
|
16
|
-
include: options.include,
|
|
17
|
-
logging: console.log,
|
|
18
16
|
});
|
|
19
17
|
};
|
|
20
18
|
exports.findAll = findAll;
|
|
@@ -28,21 +26,19 @@ const findById = (id, options = { withDisabled: false }) => {
|
|
|
28
26
|
return models_1.CustomFieldDefinition.scope('userScope').findByPk(id);
|
|
29
27
|
};
|
|
30
28
|
exports.findById = findById;
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
const findByEntityId = async (entityId, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
30
|
+
where: { entityId },
|
|
31
|
+
transaction: options.transaction,
|
|
32
|
+
});
|
|
33
|
+
exports.findByEntityId = findByEntityId;
|
|
34
|
+
const findByEntityIds = async (modelType, entityIds, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
35
|
+
where: {
|
|
34
36
|
modelType,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
where,
|
|
41
|
-
transaction: options.transaction,
|
|
42
|
-
include: include?.(entityIds),
|
|
43
|
-
raw: true,
|
|
44
|
-
});
|
|
45
|
-
};
|
|
37
|
+
entityId: entityIds,
|
|
38
|
+
},
|
|
39
|
+
transaction: options.transaction,
|
|
40
|
+
raw: true,
|
|
41
|
+
});
|
|
46
42
|
exports.findByEntityIds = findByEntityIds;
|
|
47
43
|
const findByWhere = (where) => models_1.CustomFieldDefinition.scope('userScope').findOne({
|
|
48
44
|
where,
|
|
@@ -72,19 +68,10 @@ exports.destroy = destroy;
|
|
|
72
68
|
/**
|
|
73
69
|
* Return the names of the required fields for a given model
|
|
74
70
|
*/
|
|
75
|
-
const getRequiredFields = async (modelType, modelId, entityId
|
|
71
|
+
const getRequiredFields = async (modelType, modelId, entityId) => {
|
|
76
72
|
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
77
|
-
const { include, useEntityIdFromInclude } = modelOptions;
|
|
78
|
-
const where = {
|
|
79
|
-
modelType,
|
|
80
|
-
required: true,
|
|
81
|
-
};
|
|
82
|
-
if (!useEntityIdFromInclude) {
|
|
83
|
-
where.entityId = entityIds;
|
|
84
|
-
}
|
|
85
73
|
const requiredFields = await models_1.CustomFieldDefinition.findAll({
|
|
86
|
-
where,
|
|
87
|
-
include: include?.(entityIds),
|
|
74
|
+
where: { required: true, modelType, entityId: { [sequelize_1.Op.in]: entityIds } },
|
|
88
75
|
logging: true,
|
|
89
76
|
});
|
|
90
77
|
const requiredFieldsNames = requiredFields.map((definition) => definition.name);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { FindOptions } from 'sequelize';
|
|
2
1
|
import { CustomFieldValue } from '../models';
|
|
3
2
|
import { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
|
|
4
|
-
import { ModelOptions } from '../types';
|
|
5
3
|
export declare const findByModelIdAndDefinition: (modelId: string, customFieldDefinitionId: string) => Promise<CustomFieldValue[]>;
|
|
6
4
|
export declare const create: (data: CreateCustomFieldValue, withAssociations?: boolean) => Promise<CustomFieldValue>;
|
|
7
5
|
export declare const findAllValues: () => Promise<CustomFieldValue[]>;
|
|
@@ -22,7 +20,5 @@ export declare const findValuesByModelIds: (modelIds: string[], options?: any) =
|
|
|
22
20
|
* Create new value record if not exists, but fails if value's definition not exist.
|
|
23
21
|
* Return the updated values
|
|
24
22
|
*/
|
|
25
|
-
export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?:
|
|
26
|
-
modelOptions?: ModelOptions;
|
|
27
|
-
}) => Promise<CustomFieldValue[]>;
|
|
23
|
+
export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?: any) => Promise<CustomFieldValue[]>;
|
|
28
24
|
export declare const deleteValue: (id: string, options?: any) => Promise<any>;
|
package/dist/repository/value.js
CHANGED
|
@@ -27,6 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.deleteValue = exports.updateValues = exports.findValuesByModelIds = exports.findValuesByModelId = exports.findAllValues = exports.create = exports.findByModelIdAndDefinition = void 0;
|
|
30
|
+
/* eslint-disable max-len */
|
|
30
31
|
const models_1 = require("../models");
|
|
31
32
|
const DefinitionRepo = __importStar(require("./definition"));
|
|
32
33
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
@@ -73,18 +74,8 @@ exports.findValuesByModelIds = findValuesByModelIds;
|
|
|
73
74
|
*/
|
|
74
75
|
const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
|
|
75
76
|
const names = Object.keys(valuesToUpdate);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
});
|
|
79
|
-
const { modelOptions, transaction } = options;
|
|
80
|
-
const where = {
|
|
81
|
-
modelType,
|
|
82
|
-
name: names,
|
|
83
|
-
};
|
|
84
|
-
if (!options.modelOptions?.useEntityIdFromInclude) {
|
|
85
|
-
where.entityId = identifiers;
|
|
86
|
-
}
|
|
87
|
-
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) || [];
|
|
77
|
+
const fieldDefinitions = await DefinitionRepo.findAll({ modelType, name: names, entityId: identifiers }, { withDisabled: true, transaction: options.transaction }) || [];
|
|
78
|
+
logger_1.default.info(`custom-fields: updating values for ${modelType} ${modelId}`, { valuesToUpdate, entityId: identifiers });
|
|
88
79
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
89
80
|
if (fieldDefinitions.length !== names.length) {
|
|
90
81
|
logger_1.default.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
@@ -6,7 +6,6 @@ exports.default = {
|
|
|
6
6
|
password: process.env.DB_PASSWORD || null,
|
|
7
7
|
database: process.env.DB_NAME || 'sadot_package_test',
|
|
8
8
|
host: process.env.DB_HOST || '127.0.0.1',
|
|
9
|
-
port: process.env.DB_PORT || 5432,
|
|
10
9
|
dialect: process.env.DB_TYPE || 'postgres',
|
|
11
10
|
define: {
|
|
12
11
|
underscored: true,
|
|
@@ -7,8 +7,6 @@ const cleanup = async () => {
|
|
|
7
7
|
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
|
|
8
8
|
await models_1.CustomFieldDefinition.unscoped().destroy({ where: {} });
|
|
9
9
|
await models_1.TestModel.destroy({ where: {} });
|
|
10
|
-
await models_1.ContextAwareTestModel.destroy({ where: {} });
|
|
11
|
-
await models_1.ContextModel.destroy({ where: {} });
|
|
12
10
|
}
|
|
13
11
|
};
|
|
14
12
|
exports.cleanup = cleanup;
|
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
2
|
-
export declare const contextAwareFieldDefinition: {
|
|
3
|
-
name: string;
|
|
4
|
-
modelType: string;
|
|
5
|
-
fieldType: string;
|
|
6
|
-
entityType: string;
|
|
7
|
-
};
|
|
8
2
|
export declare const coolFieldDefinition: CreateCustomFieldDefinition;
|
|
9
3
|
export declare const coolFieldDefinition2: {
|
|
10
4
|
name: string;
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDefinitions = exports.createDefinition = exports.rangeField = exports.enumField = exports.booleanField = exports.coolFieldDefinition3 = exports.coolFieldDefinition2 = exports.coolFieldDefinition =
|
|
3
|
+
exports.createDefinitions = exports.createDefinition = exports.rangeField = exports.enumField = exports.booleanField = exports.coolFieldDefinition3 = exports.coolFieldDefinition2 = exports.coolFieldDefinition = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
|
-
exports.contextAwareFieldDefinition = {
|
|
6
|
-
name: 'cool field',
|
|
7
|
-
modelType: 'ContextAwareTestModel',
|
|
8
|
-
fieldType: 'number',
|
|
9
|
-
entityType: 'fleetId',
|
|
10
|
-
};
|
|
11
5
|
exports.coolFieldDefinition = {
|
|
12
6
|
name: 'cool field',
|
|
13
7
|
modelType: 'TestModel',
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
|
-
import { Includeable } from 'sequelize';
|
|
2
|
-
import { ModelCtor } from 'sequelize-typescript';
|
|
3
1
|
export type ModelFetcher = (name: string) => any;
|
|
4
2
|
export type ModelOptions = {
|
|
5
|
-
include?: (entityIds: string[]) => Includeable[];
|
|
6
|
-
/**
|
|
7
|
-
* A function that will be called to resolve the entity id.
|
|
8
|
-
* @param {string | string[]} - The entity id.
|
|
9
|
-
* @returns {void}
|
|
10
|
-
*/
|
|
11
|
-
customAssociation?: (model: ModelCtor) => void;
|
|
12
|
-
/**
|
|
13
|
-
* Whether to use the entity id from the instance per scope attribute
|
|
14
|
-
* @param {string} -
|
|
15
|
-
* @returns {void}
|
|
16
|
-
*/
|
|
17
|
-
useEntityIdFromInclude?: boolean;
|
|
18
|
-
};
|
|
19
|
-
export type Models = {
|
|
20
3
|
name: string;
|
|
21
|
-
scopeAttributes:
|
|
22
|
-
modelOptions?: ModelOptions;
|
|
4
|
+
scopeAttributes: string[];
|
|
23
5
|
creationWebhookHandler?: (instance: any) => any;
|
|
24
6
|
updateWebhookHandler?: (instance: any) => any;
|
|
25
7
|
deletionWebhookHandler?: (instance: any) => any;
|
|
26
8
|
};
|
|
27
9
|
export type CustomFieldOptions = {
|
|
28
|
-
models:
|
|
10
|
+
models: ModelOptions[];
|
|
29
11
|
databaseConfig: any;
|
|
30
12
|
getUser: () => any;
|
|
31
13
|
};
|
package/dist/utils/init.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { ModelFetcher,
|
|
2
|
-
export declare const addHooks: (models:
|
|
3
|
-
export declare const removeHooks: (models:
|
|
4
|
-
export declare const addScopes: (models:
|
|
5
|
-
export declare const applyCustomAssociation: (models: Models[]) => void;
|
|
1
|
+
import type { ModelFetcher, ModelOptions } from '../types';
|
|
2
|
+
export declare const addHooks: (models: ModelOptions[], getModel: ModelFetcher) => void;
|
|
3
|
+
export declare const removeHooks: (models: ModelOptions[], getModel: ModelFetcher) => void;
|
|
4
|
+
export declare const addScopes: (models: ModelOptions[], getModel: ModelFetcher) => void;
|
package/dist/utils/init.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.addScopes = exports.removeHooks = exports.addHooks = void 0;
|
|
7
7
|
const sequelize_1 = require("sequelize");
|
|
8
8
|
const common_types_1 = require("@autofleet/common-types");
|
|
9
9
|
const models_1 = require("../models");
|
|
@@ -12,7 +12,7 @@ const scopes_1 = require("../scopes");
|
|
|
12
12
|
const logger_1 = __importDefault(require("./logger"));
|
|
13
13
|
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
14
14
|
const addHooks = (models, getModel) => {
|
|
15
|
-
models.forEach(async ({ name, scopeAttributes
|
|
15
|
+
models.forEach(async ({ name, scopeAttributes }) => {
|
|
16
16
|
try {
|
|
17
17
|
const model = getModel(name);
|
|
18
18
|
if (!model) {
|
|
@@ -31,11 +31,11 @@ const addHooks = (models, getModel) => {
|
|
|
31
31
|
model.addHook('beforeFind', 'sadot-beforeFind', (0, hooks_1.beforeFind)(scopeAttributes));
|
|
32
32
|
model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', hooks_1.beforeBulkCreate);
|
|
33
33
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
|
|
34
|
-
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes
|
|
35
|
-
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes
|
|
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
|
|
38
|
-
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes
|
|
34
|
+
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes));
|
|
35
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes));
|
|
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));
|
|
38
|
+
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
39
39
|
}
|
|
40
40
|
catch (e) {
|
|
41
41
|
logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
@@ -98,9 +98,3 @@ const addScopes = (models, getModel) => {
|
|
|
98
98
|
});
|
|
99
99
|
};
|
|
100
100
|
exports.addScopes = addScopes;
|
|
101
|
-
const applyCustomAssociation = (models) => {
|
|
102
|
-
models.forEach(({ modelOptions }) => {
|
|
103
|
-
modelOptions?.customAssociation?.(models_1.CustomFieldDefinition);
|
|
104
|
-
});
|
|
105
|
-
};
|
|
106
|
-
exports.applyCustomAssociation = applyCustomAssociation;
|
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.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,24 +9,7 @@
|
|
|
9
9
|
"linter": "./node_modules/.bin/eslint .",
|
|
10
10
|
"test": "jest --forceExit --runInBand",
|
|
11
11
|
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
|
|
12
|
-
"
|
|
13
|
-
"dev": "nodemon",
|
|
14
|
-
"watch": "npm-watch build-to-local-repo"
|
|
15
|
-
},
|
|
16
|
-
"watch": {
|
|
17
|
-
"build-to-local-repo": {
|
|
18
|
-
"extensions": [
|
|
19
|
-
"js",
|
|
20
|
-
"jsx",
|
|
21
|
-
"ts",
|
|
22
|
-
"tsx",
|
|
23
|
-
"css"
|
|
24
|
-
],
|
|
25
|
-
"patterns": [
|
|
26
|
-
"src"
|
|
27
|
-
],
|
|
28
|
-
"quiet": false
|
|
29
|
-
}
|
|
12
|
+
"dev": "nodemon"
|
|
30
13
|
},
|
|
31
14
|
"dependencies": {
|
|
32
15
|
"@autofleet/common-types": "^1.7.29",
|
|
@@ -51,7 +34,6 @@
|
|
|
51
34
|
"eslint-config-airbnb-typescript": "^12.0.0",
|
|
52
35
|
"eslint-plugin-import": "^2.22.1",
|
|
53
36
|
"jest": "^27.0.5",
|
|
54
|
-
"npm-watch": "^0.11.0",
|
|
55
37
|
"ts-jest": "^27.0.3",
|
|
56
38
|
"ts-node": "^8.6.2",
|
|
57
39
|
"typescript": "^4.9.5",
|
|
@@ -65,8 +65,10 @@ router.get('/', async (req, res) => {
|
|
|
65
65
|
if (entityIds?.length > 0) {
|
|
66
66
|
where.entityId = entityIds;
|
|
67
67
|
}
|
|
68
|
-
const customFieldDefinitions = await DefinitionRepo.findAll(
|
|
69
|
-
{
|
|
68
|
+
const customFieldDefinitions = await DefinitionRepo.findAll(
|
|
69
|
+
{ ...where },
|
|
70
|
+
{ withDisabled: true },
|
|
71
|
+
);
|
|
70
72
|
return res.json(customFieldDefinitions);
|
|
71
73
|
} catch (err) {
|
|
72
74
|
logger.error('Failed to fetch custom field definitions', err);
|
package/src/hooks/create.ts
CHANGED
|
@@ -2,8 +2,6 @@ import logger from '../utils/logger';
|
|
|
2
2
|
import * as ValueRepo from '../repository/value';
|
|
3
3
|
import * as DefinitionRepo from '../repository/definition';
|
|
4
4
|
import { MissingRequiredCustomFieldError } from '../errors';
|
|
5
|
-
import { ModelOptions } from '../types';
|
|
6
|
-
import buildAttributesByInstance from '../utils/scopeAttributes';
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
* A hook to create the custom fields when updating a model (more then one instance).
|
|
@@ -17,20 +15,18 @@ export const beforeBulkCreate = (options): void => {
|
|
|
17
15
|
* A hook to create the custom fields when updating a model instance.
|
|
18
16
|
* TODO - cleanup if update fail
|
|
19
17
|
*/
|
|
20
|
-
export const beforeCreate = (scopeAttributes: string[]
|
|
18
|
+
export const beforeCreate = (scopeAttributes: string[]) => async (
|
|
21
19
|
instance,
|
|
22
20
|
options,
|
|
23
21
|
): Promise<void> => {
|
|
24
22
|
logger.debug('sadot - before create hook');
|
|
25
23
|
const { fields } = options;
|
|
26
24
|
const modelType = instance.constructor.name;
|
|
27
|
-
|
|
28
|
-
const identifiers = buildAttributesByInstance(instance, scopeAttributes);
|
|
29
|
-
|
|
25
|
+
const identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
30
26
|
// get all model's required definitions
|
|
31
27
|
const requiredFieldsNames = await DefinitionRepo.getRequiredFields(modelType,
|
|
32
|
-
instance.id,
|
|
33
|
-
|
|
28
|
+
instance.id,
|
|
29
|
+
identifiers);
|
|
34
30
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
35
31
|
const { customFields } = instance;
|
|
36
32
|
if (customFieldsIdx > -1 && customFields) {
|
|
@@ -45,12 +41,8 @@ export const beforeCreate = (scopeAttributes: string[], modelOptions: ModelOptio
|
|
|
45
41
|
instance.id,
|
|
46
42
|
identifiers,
|
|
47
43
|
customFields,
|
|
48
|
-
{
|
|
49
|
-
transaction: options.transaction,
|
|
50
|
-
modelOptions,
|
|
51
|
-
},
|
|
44
|
+
{ transaction: options.transaction },
|
|
52
45
|
);
|
|
53
|
-
|
|
54
46
|
// eslint-disable-next-line no-param-reassign
|
|
55
47
|
fields.splice(customFieldsIdx, 1);
|
|
56
48
|
} else if (requiredFieldsNames?.length > 0) {
|