@autofleet/sadot 1.1.5 → 1.1.6-beta
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/.nvmrc +1 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.js +12 -2
- package/dist/api/v1/definition/index.d.ts +3 -0
- package/dist/api/v1/definition/index.js +116 -2
- package/dist/api/v1/definition/validations.d.ts +2 -0
- package/dist/api/v1/definition/validations.js +77 -2
- package/dist/api/v1/errors.d.ts +4 -0
- package/dist/api/v1/errors.js +12 -2
- package/dist/api/v1/index.d.ts +3 -0
- package/dist/api/v1/index.js +13 -2
- package/dist/api/v1/validator/index.d.ts +3 -0
- package/dist/api/v1/validator/index.js +143 -2
- package/dist/api/v1/validator/validations.d.ts +6 -23
- package/dist/api/v1/validator/validations.js +38 -2
- package/dist/errors/index.d.ts +24 -0
- package/dist/errors/index.js +66 -3
- package/dist/events/index.d.ts +5 -0
- package/dist/events/index.js +54 -2
- package/dist/hooks/create.d.ts +10 -0
- package/dist/hooks/create.js +95 -0
- package/dist/hooks/enrich.d.ts +25 -0
- package/dist/hooks/enrich.js +198 -2
- package/dist/hooks/find.d.ts +1 -0
- package/dist/hooks/find.js +29 -2
- package/dist/hooks/hooks.d.ts +17 -0
- package/dist/hooks/hooks.js +388 -2
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +17 -1
- package/dist/hooks/update.d.ts +10 -0
- package/dist/hooks/update.js +49 -0
- package/dist/hooks/utils/updateInstanceValues.d.ts +15 -0
- package/dist/hooks/utils/updateInstanceValues.js +50 -2
- package/dist/hooks/workaround.d.ts +10 -0
- package/dist/hooks/workaround.js +37 -0
- package/dist/index.d.ts +12 -22
- package/dist/index.js +67 -2
- package/dist/models/CustomFieldDefinition.d.ts +23 -29
- package/dist/models/CustomFieldDefinition.js +192 -2
- package/dist/models/CustomFieldEntries.d.ts +13 -14
- package/dist/models/CustomFieldEntries.js +123 -2
- package/dist/models/CustomFieldValue.d.ts +14 -20
- package/dist/models/CustomFieldValue.js +151 -2
- package/dist/models/CustomValidator.d.ts +15 -17
- package/dist/models/CustomValidator.js +98 -2
- package/dist/models/index.d.ts +18 -6
- package/dist/models/index.js +131 -2
- package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/models/tests/AssociatedTestModel.js +71 -2
- package/dist/models/tests/TestModel.d.ts +12 -0
- package/dist/models/tests/TestModel.js +69 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +10 -0
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +53 -2
- package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +13 -0
- package/dist/models/tests/contextAwareModels/ContextTestModel.js +47 -2
- package/dist/repository/definition.d.ts +36 -0
- package/dist/repository/definition.js +121 -2
- package/dist/repository/entries.d.ts +13 -0
- package/dist/repository/entries.js +92 -2
- package/dist/repository/utils/formatValues.d.ts +3 -0
- package/dist/repository/utils/formatValues.js +16 -2
- package/dist/repository/validator.d.ts +28 -0
- package/dist/repository/validator.js +69 -2
- package/dist/repository/value.d.ts +28 -0
- package/dist/repository/value.js +124 -2
- package/dist/scopes/filter.d.ts +29 -22
- package/dist/scopes/filter.js +75 -2
- package/dist/scopes/helpers/filter.helpers.d.ts +40 -15
- package/dist/scopes/helpers/filter.helpers.js +183 -25
- package/dist/scopes/index.d.ts +2 -0
- package/dist/scopes/index.js +6 -1
- package/dist/tests/api/test-api.d.ts +2 -0
- package/dist/tests/api/test-api.js +38 -0
- package/dist/tests/functional/searching/index.d.ts +8 -0
- package/dist/tests/functional/searching/index.js +44 -0
- package/dist/tests/helpers/commonHooks.d.ts +6 -0
- package/dist/tests/helpers/commonHooks.js +62 -0
- package/dist/tests/helpers/database-config.d.ts +16 -0
- package/dist/tests/helpers/database-config.js +17 -0
- package/dist/tests/helpers/index.d.ts +7 -0
- package/dist/tests/helpers/index.js +33 -0
- package/dist/tests/mocks/definition.mock.d.ts +48 -0
- package/dist/tests/mocks/definition.mock.js +78 -0
- package/dist/tests/mocks/events.mock.d.ts +4 -0
- package/dist/tests/mocks/events.mock.js +21 -0
- package/dist/tests/mocks/testModel.d.ts +12 -0
- package/dist/tests/mocks/testModel.js +35 -0
- package/dist/types/definition/index.d.ts +25 -0
- package/dist/types/definition/index.js +2 -0
- package/dist/types/entries/index.d.ts +25 -0
- package/dist/types/entries/index.js +2 -0
- package/dist/types/index.d.ts +46 -45
- package/dist/types/index.js +2 -0
- package/dist/types/value/index.d.ts +15 -0
- package/dist/types/value/index.js +2 -0
- package/dist/utils/constants/index.d.ts +17 -20
- package/dist/utils/constants/index.js +22 -2
- package/dist/utils/db/index.d.ts +4 -0
- package/dist/utils/db/index.js +24 -2
- package/dist/utils/helpers/index.d.ts +23 -28
- package/dist/utils/helpers/index.js +40 -2
- package/dist/utils/init.d.ts +7 -0
- package/dist/utils/init.js +112 -2
- package/dist/utils/logger/index.d.ts +3 -0
- package/dist/utils/logger/index.js +42 -2
- package/dist/utils/scopeAttributes.d.ts +2 -0
- package/dist/utils/scopeAttributes.js +11 -2
- package/dist/utils/validations/index.d.ts +8 -0
- package/dist/utils/validations/index.js +41 -2
- package/dist/utils/validations/schema/custom-fields.d.ts +2 -6
- package/dist/utils/validations/schema/custom-fields.js +9 -2
- package/dist/utils/validations/schema/validator-schema.d.ts +9 -0
- package/dist/utils/validations/schema/validator-schema.js +95 -2
- package/dist/utils/validations/type.d.ts +15 -0
- package/dist/utils/validations/type.js +2 -0
- package/dist/utils/validations/validators/index.d.ts +14 -0
- package/dist/utils/validations/validators/index.js +40 -2
- package/dist/utils/validations/validators/select.validator.d.ts +5 -0
- package/dist/utils/validations/validators/select.validator.js +12 -2
- package/dist/utils/validations/validators/status.validator.d.ts +12 -0
- package/dist/utils/validations/validators/status.validator.js +15 -2
- package/package.json +39 -40
- package/src/api/index.ts +10 -0
- package/src/api/v1/definition/index.ts +104 -0
- package/src/api/v1/definition/validations.ts +75 -0
- package/src/api/v1/errors.ts +13 -0
- package/src/api/v1/index.ts +11 -0
- package/src/api/v1/validator/index.ts +141 -0
- package/src/api/v1/validator/validations.ts +38 -0
- package/src/errors/index.ts +70 -0
- package/src/events/index.ts +63 -0
- package/src/hooks/create.ts +81 -0
- package/src/hooks/enrich.ts +255 -0
- package/src/hooks/find.ts +27 -0
- package/src/hooks/hooks.ts +479 -0
- package/src/hooks/index.ts +20 -0
- package/src/hooks/update.ts +55 -0
- package/src/hooks/utils/updateInstanceValues.ts +63 -0
- package/src/hooks/workaround.ts +47 -0
- package/src/index.ts +52 -0
- package/src/models/CustomFieldDefinition.ts +162 -0
- package/src/models/CustomFieldEntries.ts +81 -0
- package/src/models/CustomFieldValue.ts +118 -0
- package/src/models/CustomValidator.ts +78 -0
- package/src/models/index.ts +165 -0
- package/src/models/tests/AssociatedTestModel.ts +57 -0
- package/src/models/tests/TestModel.ts +54 -0
- package/src/models/tests/contextAwareModels/ContextAwareTestModel.ts +43 -0
- package/src/models/tests/contextAwareModels/ContextTestModel.ts +38 -0
- package/src/repository/definition.ts +175 -0
- package/src/repository/entries.ts +88 -0
- package/src/repository/utils/formatValues.ts +14 -0
- package/src/repository/validator.ts +119 -0
- package/src/repository/value.ts +116 -0
- package/src/scopes/filter.ts +100 -0
- package/src/scopes/helpers/filter.helpers.ts +227 -0
- package/src/scopes/index.ts +6 -0
- package/src/tests/api/test-api.ts +40 -0
- package/src/tests/functional/searching/index.ts +39 -0
- package/src/tests/helpers/commonHooks.ts +43 -0
- package/src/tests/helpers/database-config.ts +15 -0
- package/src/tests/helpers/index.ts +35 -0
- package/src/tests/mocks/definition.mock.ts +84 -0
- package/src/tests/mocks/events.mock.ts +21 -0
- package/src/tests/mocks/testModel.ts +37 -0
- package/src/types/definition/index.ts +24 -0
- package/src/types/entries/index.ts +27 -0
- package/src/types/index.ts +52 -0
- package/src/types/value/index.ts +14 -0
- package/src/utils/constants/index.ts +25 -0
- package/src/utils/db/index.ts +21 -0
- package/src/utils/helpers/index.ts +66 -0
- package/src/utils/init.ts +122 -0
- package/src/utils/logger/index.ts +14 -0
- package/src/utils/scopeAttributes.ts +12 -0
- package/src/utils/validations/index.ts +46 -0
- package/src/utils/validations/schema/README.md +93 -0
- package/src/utils/validations/schema/custom-fields.ts +8 -0
- package/src/utils/validations/schema/validator-schema.ts +106 -0
- package/src/utils/validations/type.ts +20 -0
- package/src/utils/validations/validators/index.ts +38 -0
- package/src/utils/validations/validators/select.validator.ts +12 -0
- package/src/utils/validations/validators/status.validator.ts +22 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js +0 -1
- package/dist/_virtual/rolldown_runtime.cjs +0 -1
- package/dist/api/index.cjs +0 -2
- package/dist/api/index.cjs.map +0 -1
- package/dist/api/index.js.map +0 -1
- package/dist/api/v1/definition/index.cjs +0 -2
- package/dist/api/v1/definition/index.cjs.map +0 -1
- package/dist/api/v1/definition/index.js.map +0 -1
- package/dist/api/v1/definition/validations.cjs +0 -2
- package/dist/api/v1/definition/validations.cjs.map +0 -1
- package/dist/api/v1/definition/validations.js.map +0 -1
- package/dist/api/v1/errors.cjs +0 -2
- package/dist/api/v1/errors.cjs.map +0 -1
- package/dist/api/v1/errors.js.map +0 -1
- package/dist/api/v1/index.cjs +0 -2
- package/dist/api/v1/index.cjs.map +0 -1
- package/dist/api/v1/index.js.map +0 -1
- package/dist/api/v1/validator/index.cjs +0 -2
- package/dist/api/v1/validator/index.cjs.map +0 -1
- package/dist/api/v1/validator/index.js.map +0 -1
- package/dist/api/v1/validator/validations.cjs +0 -2
- package/dist/api/v1/validator/validations.cjs.map +0 -1
- package/dist/api/v1/validator/validations.d.cts +0 -23
- package/dist/api/v1/validator/validations.js.map +0 -1
- package/dist/errors/index.cjs +0 -3
- package/dist/errors/index.cjs.map +0 -1
- package/dist/errors/index.js.map +0 -1
- package/dist/events/index.cjs +0 -2
- package/dist/events/index.cjs.map +0 -1
- package/dist/events/index.js.map +0 -1
- package/dist/hooks/enrich.cjs +0 -2
- package/dist/hooks/enrich.cjs.map +0 -1
- package/dist/hooks/enrich.js.map +0 -1
- package/dist/hooks/find.cjs +0 -2
- package/dist/hooks/find.cjs.map +0 -1
- package/dist/hooks/find.js.map +0 -1
- package/dist/hooks/hooks.cjs +0 -2
- package/dist/hooks/hooks.cjs.map +0 -1
- package/dist/hooks/hooks.js.map +0 -1
- package/dist/hooks/index.cjs +0 -1
- package/dist/hooks/utils/updateInstanceValues.cjs +0 -2
- package/dist/hooks/utils/updateInstanceValues.cjs.map +0 -1
- package/dist/hooks/utils/updateInstanceValues.js.map +0 -1
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -23
- package/dist/index.js.map +0 -1
- package/dist/models/CustomFieldDefinition.cjs +0 -2
- package/dist/models/CustomFieldDefinition.cjs.map +0 -1
- package/dist/models/CustomFieldDefinition.d.cts +0 -31
- package/dist/models/CustomFieldDefinition.js.map +0 -1
- package/dist/models/CustomFieldEntries.cjs +0 -2
- package/dist/models/CustomFieldEntries.cjs.map +0 -1
- package/dist/models/CustomFieldEntries.d.cts +0 -16
- package/dist/models/CustomFieldEntries.js.map +0 -1
- package/dist/models/CustomFieldValue.cjs +0 -2
- package/dist/models/CustomFieldValue.cjs.map +0 -1
- package/dist/models/CustomFieldValue.d.cts +0 -22
- package/dist/models/CustomFieldValue.js.map +0 -1
- package/dist/models/CustomValidator.cjs +0 -2
- package/dist/models/CustomValidator.cjs.map +0 -1
- package/dist/models/CustomValidator.d.cts +0 -19
- package/dist/models/CustomValidator.js.map +0 -1
- package/dist/models/index.cjs +0 -2
- package/dist/models/index.cjs.map +0 -1
- package/dist/models/index.d.cts +0 -6
- package/dist/models/index.js.map +0 -1
- package/dist/models/tests/AssociatedTestModel.cjs +0 -2
- package/dist/models/tests/AssociatedTestModel.cjs.map +0 -1
- package/dist/models/tests/AssociatedTestModel.js.map +0 -1
- package/dist/models/tests/TestModel.cjs +0 -2
- package/dist/models/tests/TestModel.cjs.map +0 -1
- package/dist/models/tests/TestModel.js.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs +0 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextTestModel.cjs +0 -2
- package/dist/models/tests/contextAwareModels/ContextTestModel.cjs.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextTestModel.js.map +0 -1
- package/dist/repository/definition.cjs +0 -2
- package/dist/repository/definition.cjs.map +0 -1
- package/dist/repository/definition.js.map +0 -1
- package/dist/repository/entries.cjs +0 -2
- package/dist/repository/entries.cjs.map +0 -1
- package/dist/repository/entries.js.map +0 -1
- package/dist/repository/utils/formatValues.cjs +0 -2
- package/dist/repository/utils/formatValues.cjs.map +0 -1
- package/dist/repository/utils/formatValues.js.map +0 -1
- package/dist/repository/validator.cjs +0 -2
- package/dist/repository/validator.cjs.map +0 -1
- package/dist/repository/validator.js.map +0 -1
- package/dist/repository/value.cjs +0 -2
- package/dist/repository/value.cjs.map +0 -1
- package/dist/repository/value.js.map +0 -1
- package/dist/scopes/filter.cjs +0 -2
- package/dist/scopes/filter.cjs.map +0 -1
- package/dist/scopes/filter.d.cts +0 -23
- package/dist/scopes/filter.js.map +0 -1
- package/dist/scopes/helpers/filter.helpers.cjs +0 -46
- package/dist/scopes/helpers/filter.helpers.cjs.map +0 -1
- package/dist/scopes/helpers/filter.helpers.d.cts +0 -17
- package/dist/scopes/helpers/filter.helpers.js.map +0 -1
- package/dist/scopes/index.cjs +0 -1
- package/dist/types/index.d.cts +0 -45
- package/dist/utils/constants/index.cjs +0 -2
- package/dist/utils/constants/index.cjs.map +0 -1
- package/dist/utils/constants/index.d.cts +0 -22
- package/dist/utils/constants/index.js.map +0 -1
- package/dist/utils/db/index.cjs +0 -2
- package/dist/utils/db/index.cjs.map +0 -1
- package/dist/utils/db/index.js.map +0 -1
- package/dist/utils/helpers/index.cjs +0 -2
- package/dist/utils/helpers/index.cjs.map +0 -1
- package/dist/utils/helpers/index.d.cts +0 -31
- package/dist/utils/helpers/index.js.map +0 -1
- package/dist/utils/init.cjs +0 -2
- package/dist/utils/init.cjs.map +0 -1
- package/dist/utils/init.js.map +0 -1
- package/dist/utils/logger/index.cjs +0 -2
- package/dist/utils/logger/index.cjs.map +0 -1
- package/dist/utils/logger/index.js.map +0 -1
- package/dist/utils/scopeAttributes.cjs +0 -2
- package/dist/utils/scopeAttributes.cjs.map +0 -1
- package/dist/utils/scopeAttributes.js.map +0 -1
- package/dist/utils/validations/index.cjs +0 -2
- package/dist/utils/validations/index.cjs.map +0 -1
- package/dist/utils/validations/index.js.map +0 -1
- package/dist/utils/validations/schema/custom-fields.cjs +0 -2
- package/dist/utils/validations/schema/custom-fields.cjs.map +0 -1
- package/dist/utils/validations/schema/custom-fields.d.cts +0 -7
- package/dist/utils/validations/schema/custom-fields.js.map +0 -1
- package/dist/utils/validations/schema/validator-schema.cjs +0 -2
- package/dist/utils/validations/schema/validator-schema.cjs.map +0 -1
- package/dist/utils/validations/schema/validator-schema.js.map +0 -1
- package/dist/utils/validations/validators/index.cjs +0 -2
- package/dist/utils/validations/validators/index.cjs.map +0 -1
- package/dist/utils/validations/validators/index.js.map +0 -1
- package/dist/utils/validations/validators/select.validator.cjs +0 -2
- package/dist/utils/validations/validators/select.validator.cjs.map +0 -1
- package/dist/utils/validations/validators/select.validator.js.map +0 -1
- package/dist/utils/validations/validators/status.validator.cjs +0 -2
- package/dist/utils/validations/validators/status.validator.cjs.map +0 -1
- package/dist/utils/validations/validators/status.validator.js.map +0 -1
package/dist/hooks/hooks.js
CHANGED
|
@@ -1,2 +1,388 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate = void 0;
|
|
30
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
31
|
+
const joi_1 = __importDefault(require("joi"));
|
|
32
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
33
|
+
const errors_1 = require("@autofleet/errors");
|
|
34
|
+
const ajv_errors_1 = __importDefault(require("ajv-errors"));
|
|
35
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
36
|
+
const ValidatorRepo = __importStar(require("../repository/validator"));
|
|
37
|
+
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
38
|
+
const errors_2 = require("../errors");
|
|
39
|
+
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
40
|
+
const updateInstanceValues_1 = __importDefault(require("./utils/updateInstanceValues"));
|
|
41
|
+
const constants_1 = require("../utils/constants");
|
|
42
|
+
// Include all required fields for proper validation
|
|
43
|
+
const CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL = ['id', 'schema', 'modelType', 'entityId', 'disabled'];
|
|
44
|
+
// Initialize Ajv with relaxed settings to avoid warnings
|
|
45
|
+
const ajv = new ajv_1.default({
|
|
46
|
+
allErrors: true,
|
|
47
|
+
strict: false, // Disable strict mode to avoid warnings
|
|
48
|
+
strictTypes: false, // Disable strict type checking
|
|
49
|
+
$data: true, // Enable $data references
|
|
50
|
+
});
|
|
51
|
+
(0, ajv_formats_1.default)(ajv);
|
|
52
|
+
(0, ajv_errors_1.default)(ajv);
|
|
53
|
+
/**
|
|
54
|
+
* Helper function to manually copy object properties
|
|
55
|
+
* This is more efficient for large objects and avoids excessive object creation
|
|
56
|
+
*/
|
|
57
|
+
// eslint-disable-next-line prefer-object-spread
|
|
58
|
+
const manualObjectCopy = (sourceObj, additionalProps) => ({ __proto__: null, ...sourceObj, ...additionalProps });
|
|
59
|
+
/**
|
|
60
|
+
* Fetches complete custom fields for an instance by merging DB values with update values
|
|
61
|
+
* This is needed for partial updates to ensure all related fields are available for validation
|
|
62
|
+
*/
|
|
63
|
+
const getCompleteCustomFields = async (instance, options) => {
|
|
64
|
+
// If we don't have an instance id or no custom fields being updated, return original fields
|
|
65
|
+
if (!instance.id || !instance.customFields || Object.keys(instance.customFields).length === 0) {
|
|
66
|
+
return instance.customFields || {};
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const ModelClass = instance.constructor;
|
|
70
|
+
// Only select the customFields column to minimize data transfer
|
|
71
|
+
const currentCustomFields = await ModelClass.findOne({
|
|
72
|
+
where: { id: instance.id },
|
|
73
|
+
attributes: ['customFields'],
|
|
74
|
+
transaction: options.transaction,
|
|
75
|
+
raw: true, // Get plain object instead of model instance for better performance
|
|
76
|
+
});
|
|
77
|
+
if (currentCustomFields?.customFields) {
|
|
78
|
+
// Merge existing fields with update fields using our helper function
|
|
79
|
+
const completeFields = manualObjectCopy(currentCustomFields.customFields, instance.customFields);
|
|
80
|
+
logger_1.default.debug('sadot - fetched complete custom fields for validation', {
|
|
81
|
+
fieldsCount: Object.keys(completeFields).length,
|
|
82
|
+
updateFieldsCount: Object.keys(instance.customFields).length,
|
|
83
|
+
});
|
|
84
|
+
return completeFields;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
logger_1.default.error('sadot - error fetching complete model for validation', { error });
|
|
89
|
+
// Continue with partial data if we can't fetch the complete model
|
|
90
|
+
}
|
|
91
|
+
return instance.customFields || {};
|
|
92
|
+
};
|
|
93
|
+
const buildPreChangeState = (instance) => {
|
|
94
|
+
const beforeFull = { ...instance.dataValues };
|
|
95
|
+
const changedKeys = instance.changed?.() || [];
|
|
96
|
+
changedKeys.forEach((key) => {
|
|
97
|
+
const prevVal = instance.previous?.(key);
|
|
98
|
+
if (prevVal !== undefined) {
|
|
99
|
+
beforeFull[key] = prevVal;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
const prevCF = instance.previous?.('customFields');
|
|
103
|
+
if (prevCF !== undefined) {
|
|
104
|
+
beforeFull.customFields = prevCF;
|
|
105
|
+
}
|
|
106
|
+
return beforeFull;
|
|
107
|
+
};
|
|
108
|
+
const formatAjvErrors = (errors) => errors.reduce((acc, err) => {
|
|
109
|
+
const basePath = (err.instancePath || '')
|
|
110
|
+
.split('/')
|
|
111
|
+
.filter(Boolean)
|
|
112
|
+
.join('.')
|
|
113
|
+
.replace(/^after\./, '');
|
|
114
|
+
const missingProp = err.keyword === 'required' ? `.${err.params?.missingProperty}` : '';
|
|
115
|
+
const key = (basePath + missingProp).replace(/^\./, '') || 'root';
|
|
116
|
+
const message = err.message || 'Invalid value';
|
|
117
|
+
acc[key] = message;
|
|
118
|
+
return acc;
|
|
119
|
+
}, {});
|
|
120
|
+
/**
|
|
121
|
+
* Validates the model using custom validators
|
|
122
|
+
*/
|
|
123
|
+
const validateModel = async (instance, options, scopeAttributes, modelOptions = {}, isCreate = false) => {
|
|
124
|
+
var _a;
|
|
125
|
+
const modelType = instance.constructor.name;
|
|
126
|
+
logger_1.default.debug('sadot - validating model', { isCreate, modelType });
|
|
127
|
+
const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
|
|
128
|
+
logger_1.default.debug('sadot - identifiers', { identifiers });
|
|
129
|
+
// Skip if no identifiers
|
|
130
|
+
if (!identifiers || Object.keys(identifiers).length === 0) {
|
|
131
|
+
logger_1.default.debug('sadot - skipping validation: no identifiers');
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// Find the entityId from identifiers (fleetId, businessModelId, etc.)
|
|
135
|
+
const entityId = Object.values(identifiers)[0]; // Get the first value as entityId
|
|
136
|
+
logger_1.default.debug('sadot - entityId', { entityId });
|
|
137
|
+
if (!entityId) {
|
|
138
|
+
logger_1.default.debug('sadot - skipping validation: no entityId');
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
let validatorsPromise;
|
|
142
|
+
let cacheKey;
|
|
143
|
+
if (options.transaction) {
|
|
144
|
+
// eslint-disable-next-line no-param-reassign
|
|
145
|
+
(_a = options.transaction).validationsCache || (_a.validationsCache = new Map());
|
|
146
|
+
cacheKey = `${modelType}-${entityId}`;
|
|
147
|
+
validatorsPromise = options.transaction.validationsCache.get(cacheKey);
|
|
148
|
+
}
|
|
149
|
+
if (!validatorsPromise) {
|
|
150
|
+
validatorsPromise = ValidatorRepo.findAllByModelType(modelType, entityId, {
|
|
151
|
+
transaction: options.transaction,
|
|
152
|
+
attributes: CUSTOM_VALIDATOR_ATTRIBUTES_TO_PULL,
|
|
153
|
+
modelOptions,
|
|
154
|
+
});
|
|
155
|
+
if (options.transaction) {
|
|
156
|
+
options?.transaction?.validationsCache.set(cacheKey, validatorsPromise);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const validators = await validatorsPromise;
|
|
160
|
+
logger_1.default.debug('sadot - validators found', { count: validators.length });
|
|
161
|
+
if (!validators.length) {
|
|
162
|
+
logger_1.default.debug('sadot - skipping validation: no validators found');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
// For updates, get the previous values
|
|
166
|
+
let originalValues = null;
|
|
167
|
+
if (!isCreate) {
|
|
168
|
+
originalValues = buildPreChangeState(instance);
|
|
169
|
+
}
|
|
170
|
+
// Get complete custom fields by merging DB values with update values
|
|
171
|
+
// This is especially important for partial updates to ensure all related fields are available
|
|
172
|
+
const completeCustomFields = !isCreate
|
|
173
|
+
? await getCompleteCustomFields(instance, options)
|
|
174
|
+
: instance.customFields || {};
|
|
175
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
176
|
+
for (const validator of validators) {
|
|
177
|
+
const { schema } = validator;
|
|
178
|
+
const typedSchema = schema;
|
|
179
|
+
logger_1.default.debug('sadot - validating with schema', {
|
|
180
|
+
schema,
|
|
181
|
+
hasAfterProps: !!typedSchema.properties?.after,
|
|
182
|
+
hasBeforeProps: !!typedSchema.properties?.before,
|
|
183
|
+
});
|
|
184
|
+
if (isCreate) {
|
|
185
|
+
// For create operations, we only need the 'after' state
|
|
186
|
+
if (typedSchema.properties?.after) {
|
|
187
|
+
const validateSchema = ajv.compile({
|
|
188
|
+
...schema,
|
|
189
|
+
// Focus only on the 'after' validation part for create
|
|
190
|
+
properties: {
|
|
191
|
+
after: typedSchema.properties.after,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
const isValid = validateSchema(JSON.parse(JSON.stringify({
|
|
195
|
+
after: {
|
|
196
|
+
...instance.dataValues,
|
|
197
|
+
customFields: completeCustomFields,
|
|
198
|
+
},
|
|
199
|
+
})));
|
|
200
|
+
if (!isValid) {
|
|
201
|
+
const errorDetails = validateSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
|
|
202
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
203
|
+
throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], undefined, {
|
|
204
|
+
customError: formattedErrors,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
// For update operations, we need both before and after
|
|
211
|
+
const validateSchema = ajv.compile(typedSchema);
|
|
212
|
+
// Create after object with our helper function
|
|
213
|
+
const afterObj = manualObjectCopy(instance.dataValues);
|
|
214
|
+
// Add complete custom fields
|
|
215
|
+
afterObj.customFields = completeCustomFields;
|
|
216
|
+
// Create validation payload
|
|
217
|
+
const payload = {
|
|
218
|
+
before: originalValues,
|
|
219
|
+
after: afterObj,
|
|
220
|
+
};
|
|
221
|
+
// Validate
|
|
222
|
+
const isValid = validateSchema(JSON.parse(JSON.stringify(payload)));
|
|
223
|
+
logger_1.default.debug('sadot - validation result', {
|
|
224
|
+
isValid,
|
|
225
|
+
test: {
|
|
226
|
+
before: originalValues,
|
|
227
|
+
after: afterObj,
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
if (!isValid) {
|
|
231
|
+
const errorDetails = validateSchema
|
|
232
|
+
.errors
|
|
233
|
+
?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
|
|
234
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
235
|
+
throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], undefined, {
|
|
236
|
+
customError: formattedErrors,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const getFieldDefinitions = async ({ modelType, modelOptions, identifiers, options, }) => {
|
|
243
|
+
const { include, useEntityIdFromInclude } = modelOptions;
|
|
244
|
+
const where = {
|
|
245
|
+
modelType,
|
|
246
|
+
disabled: false,
|
|
247
|
+
...(!useEntityIdFromInclude && { entityId: identifiers }),
|
|
248
|
+
};
|
|
249
|
+
const fieldDefinitions = await DefinitionRepo.findAll(where, {
|
|
250
|
+
withDisabled: false,
|
|
251
|
+
transaction: options.transaction,
|
|
252
|
+
include: include?.(identifiers),
|
|
253
|
+
});
|
|
254
|
+
return fieldDefinitions;
|
|
255
|
+
};
|
|
256
|
+
const formatDates = (fieldDefinitions, instance) => {
|
|
257
|
+
(fieldDefinitions || []).forEach((fieldDefinition) => {
|
|
258
|
+
const { fieldType, name } = fieldDefinition;
|
|
259
|
+
if ([constants_1.CustomFieldDefinitionType.DATE, constants_1.CustomFieldDefinitionType.DATETIME].includes(fieldType)) {
|
|
260
|
+
const value = instance.customFields?.[name];
|
|
261
|
+
if (value) {
|
|
262
|
+
const { value: joiValue, error: validationError } = joi_1.default.date().validate(value);
|
|
263
|
+
if (validationError) {
|
|
264
|
+
throw new errors_2.InvalidValueError(value, name, validationError);
|
|
265
|
+
}
|
|
266
|
+
// eslint-disable-next-line no-param-reassign
|
|
267
|
+
instance.customFields[name] = joiValue.toISOString();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* Hook to handle validation and custom fields during creation
|
|
274
|
+
*/
|
|
275
|
+
const beforeCreate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCustomFieldsEntries: false }) => async (instance, options) => {
|
|
276
|
+
logger_1.default.debug('sadot - before create hook');
|
|
277
|
+
const { fields } = options;
|
|
278
|
+
const modelType = instance.constructor.name;
|
|
279
|
+
const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
|
|
280
|
+
// Step 1: Handle custom fields default values and required fields
|
|
281
|
+
const fieldDefinitions = await getFieldDefinitions({
|
|
282
|
+
modelType, modelOptions, identifiers, options,
|
|
283
|
+
});
|
|
284
|
+
// Apply default values
|
|
285
|
+
const fieldsWithDefaultValue = fieldDefinitions.filter((def) => ![null, undefined].includes(def.defaultValue));
|
|
286
|
+
if (fieldsWithDefaultValue.length) {
|
|
287
|
+
// eslint-disable-next-line no-param-reassign
|
|
288
|
+
instance.customFields || (instance.customFields = {});
|
|
289
|
+
fieldsWithDefaultValue
|
|
290
|
+
.filter((def) => (instance.customFields?.[def.name] === undefined))
|
|
291
|
+
.forEach(({ name, defaultValue }) => {
|
|
292
|
+
// eslint-disable-next-line no-param-reassign
|
|
293
|
+
instance.customFields[name] = defaultValue;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
// Check for required fields
|
|
297
|
+
const requiredFieldsNames = Array.from(new Set(fieldDefinitions.filter(({ required }) => required).map(({ name }) => name)));
|
|
298
|
+
const { customFields } = instance;
|
|
299
|
+
const fieldsNames = Object.keys(customFields ?? {});
|
|
300
|
+
const missingFields = requiredFieldsNames.filter((name) => !fieldsNames.includes(name));
|
|
301
|
+
if (missingFields?.length) {
|
|
302
|
+
throw new errors_2.MissingRequiredCustomFieldError(missingFields);
|
|
303
|
+
}
|
|
304
|
+
// Step 2: Validate the model data (including custom fields)
|
|
305
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, true);
|
|
306
|
+
// format date and datetime fields
|
|
307
|
+
formatDates(fieldDefinitions, instance);
|
|
308
|
+
// Step 3: Save custom field values if they exist
|
|
309
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
310
|
+
if (customFieldsIdx === -1 || !customFields || !Object.keys(customFields).length) {
|
|
311
|
+
// No custom fields to update
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
// Save custom field values
|
|
315
|
+
await (0, updateInstanceValues_1.default)({
|
|
316
|
+
modelId: instance.id,
|
|
317
|
+
modelType,
|
|
318
|
+
identifiers,
|
|
319
|
+
customFields,
|
|
320
|
+
options: {
|
|
321
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
322
|
+
transaction: options.transaction,
|
|
323
|
+
modelOptions,
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
// Remove customFields from fields array after handling
|
|
327
|
+
// eslint-disable-next-line no-param-reassign
|
|
328
|
+
fields.splice(customFieldsIdx, 1);
|
|
329
|
+
};
|
|
330
|
+
exports.beforeCreate = beforeCreate;
|
|
331
|
+
/**
|
|
332
|
+
* Hook to handle validation and custom fields during update
|
|
333
|
+
*/
|
|
334
|
+
const beforeUpdate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCustomFieldsEntries: false }) => async (instance, options) => {
|
|
335
|
+
logger_1.default.debug('sadot - before update hook');
|
|
336
|
+
const { fields } = options;
|
|
337
|
+
const modelType = instance.constructor.name;
|
|
338
|
+
const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
|
|
339
|
+
const fieldDefinitions = await getFieldDefinitions({
|
|
340
|
+
modelType, modelOptions, identifiers, options,
|
|
341
|
+
});
|
|
342
|
+
// Step 1: Validate the model data (including custom fields)
|
|
343
|
+
await validateModel(instance, options, scopeAttributes, modelOptions, false);
|
|
344
|
+
// format date and datetime fields
|
|
345
|
+
formatDates(fieldDefinitions, instance);
|
|
346
|
+
// Step 2: Update custom field values if they exist
|
|
347
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
348
|
+
if (customFieldsIdx > -1) {
|
|
349
|
+
const { customFields } = instance;
|
|
350
|
+
if (!Object.keys(customFields).length) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
// Save custom field values
|
|
354
|
+
await (0, updateInstanceValues_1.default)({
|
|
355
|
+
modelId: instance.id,
|
|
356
|
+
modelType,
|
|
357
|
+
identifiers,
|
|
358
|
+
customFields,
|
|
359
|
+
options: {
|
|
360
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
361
|
+
transaction: options.transaction,
|
|
362
|
+
modelOptions,
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
// Remove customFields from fields array after handling
|
|
366
|
+
// eslint-disable-next-line no-param-reassign
|
|
367
|
+
fields.splice(customFieldsIdx, 1);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
exports.beforeUpdate = beforeUpdate;
|
|
371
|
+
/**
|
|
372
|
+
* Hook to enable individual hooks for bulk create operations
|
|
373
|
+
*/
|
|
374
|
+
const beforeBulkCreate = (options) => {
|
|
375
|
+
// This will activate the beforeCreate hook on each instance
|
|
376
|
+
// eslint-disable-next-line no-param-reassign
|
|
377
|
+
options.individualHooks = true;
|
|
378
|
+
};
|
|
379
|
+
exports.beforeBulkCreate = beforeBulkCreate;
|
|
380
|
+
/**
|
|
381
|
+
* Hook to enable individual hooks for bulk update operations
|
|
382
|
+
*/
|
|
383
|
+
const beforeBulkUpdate = (options) => {
|
|
384
|
+
// This will activate the beforeUpdate hook on each instance
|
|
385
|
+
// eslint-disable-next-line no-param-reassign
|
|
386
|
+
options.individualHooks = true;
|
|
387
|
+
};
|
|
388
|
+
exports.beforeBulkUpdate = beforeBulkUpdate;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import enrichResults from './enrich';
|
|
2
|
+
import { beforeFind } from './find';
|
|
3
|
+
import workaround from './workaround';
|
|
4
|
+
import { beforeCreate, beforeUpdate, beforeBulkCreate, beforeBulkUpdate } from './hooks';
|
|
5
|
+
export { enrichResults, beforeFind, workaround, beforeCreate, beforeUpdate, beforeBulkCreate, beforeBulkUpdate, };
|
package/dist/hooks/index.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.beforeBulkUpdate = exports.beforeBulkCreate = exports.beforeUpdate = exports.beforeCreate = exports.workaround = exports.beforeFind = exports.enrichResults = void 0;
|
|
7
|
+
const enrich_1 = __importDefault(require("./enrich"));
|
|
8
|
+
exports.enrichResults = enrich_1.default;
|
|
9
|
+
const find_1 = require("./find");
|
|
10
|
+
Object.defineProperty(exports, "beforeFind", { enumerable: true, get: function () { return find_1.beforeFind; } });
|
|
11
|
+
const workaround_1 = __importDefault(require("./workaround"));
|
|
12
|
+
exports.workaround = workaround_1.default;
|
|
13
|
+
const hooks_1 = require("./hooks");
|
|
14
|
+
Object.defineProperty(exports, "beforeCreate", { enumerable: true, get: function () { return hooks_1.beforeCreate; } });
|
|
15
|
+
Object.defineProperty(exports, "beforeUpdate", { enumerable: true, get: function () { return hooks_1.beforeUpdate; } });
|
|
16
|
+
Object.defineProperty(exports, "beforeBulkCreate", { enumerable: true, get: function () { return hooks_1.beforeBulkCreate; } });
|
|
17
|
+
Object.defineProperty(exports, "beforeBulkUpdate", { enumerable: true, get: function () { return hooks_1.beforeBulkUpdate; } });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CustomFieldOptions, ModelOptions } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* A hook to update the custom fields when updating a model (more then one instance).
|
|
4
|
+
*/
|
|
5
|
+
export declare const beforeBulkUpdate: (options: any) => void;
|
|
6
|
+
/**
|
|
7
|
+
* A hook to update the custom fields when updating a model instance.
|
|
8
|
+
* TODO - cleanup if update fail
|
|
9
|
+
*/
|
|
10
|
+
export declare const beforeUpdate: (scopeAttributes: string[], modelOptions?: ModelOptions, sadotOptions?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => (instance: any, options: any) => Promise<void>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.beforeUpdate = exports.beforeBulkUpdate = void 0;
|
|
7
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
8
|
+
const scopeAttributes_1 = __importDefault(require("../utils/scopeAttributes"));
|
|
9
|
+
const updateInstanceValues_1 = __importDefault(require("./utils/updateInstanceValues"));
|
|
10
|
+
/**
|
|
11
|
+
* A hook to update the custom fields when updating a model (more then one instance).
|
|
12
|
+
*/
|
|
13
|
+
const beforeBulkUpdate = (options) => {
|
|
14
|
+
// This will activate the beforeUpdate hook on each updating instance.
|
|
15
|
+
// eslint-disable-next-line no-param-reassign
|
|
16
|
+
options.individualHooks = true;
|
|
17
|
+
};
|
|
18
|
+
exports.beforeBulkUpdate = beforeBulkUpdate;
|
|
19
|
+
/**
|
|
20
|
+
* A hook to update the custom fields when updating a model instance.
|
|
21
|
+
* TODO - cleanup if update fail
|
|
22
|
+
*/
|
|
23
|
+
const beforeUpdate = (scopeAttributes, modelOptions = {}, sadotOptions = { useCustomFieldsEntries: false }) => async (instance, options) => {
|
|
24
|
+
logger_1.default.debug('sadot - before update hook');
|
|
25
|
+
const { fields } = options;
|
|
26
|
+
const modelType = instance.constructor.name;
|
|
27
|
+
const identifiers = (0, scopeAttributes_1.default)(instance, scopeAttributes);
|
|
28
|
+
const customFieldsIdx = fields.indexOf('customFields');
|
|
29
|
+
if (customFieldsIdx > -1) {
|
|
30
|
+
const { customFields } = instance;
|
|
31
|
+
if (!Object.keys(customFields).length) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await (0, updateInstanceValues_1.default)({
|
|
35
|
+
modelId: instance.id,
|
|
36
|
+
modelType,
|
|
37
|
+
identifiers,
|
|
38
|
+
customFields,
|
|
39
|
+
options: {
|
|
40
|
+
useCustomFieldsEntries: sadotOptions.useCustomFieldsEntries,
|
|
41
|
+
transaction: options.transaction,
|
|
42
|
+
modelOptions,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
// eslint-disable-next-line no-param-reassign
|
|
46
|
+
fields.splice(customFieldsIdx, 1);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
exports.beforeUpdate = beforeUpdate;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Transaction } from 'sequelize';
|
|
2
|
+
import type { ModelOptions } from '../../types';
|
|
3
|
+
interface UpdateInstanceValuesParams {
|
|
4
|
+
modelId: string;
|
|
5
|
+
modelType: string;
|
|
6
|
+
identifiers: string[];
|
|
7
|
+
customFields: Record<string, any>;
|
|
8
|
+
options?: {
|
|
9
|
+
transaction?: Transaction;
|
|
10
|
+
modelOptions?: ModelOptions;
|
|
11
|
+
useCustomFieldsEntries?: boolean;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
declare const updateInstanceValues: ({ modelId, modelType, identifiers, customFields, options, }: UpdateInstanceValuesParams) => Promise<void>;
|
|
15
|
+
export default updateInstanceValues;
|
|
@@ -1,2 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const ValueRepo = __importStar(require("../../repository/value"));
|
|
27
|
+
const EntriesRepo = __importStar(require("../../repository/entries"));
|
|
28
|
+
const updateInstanceValues = async ({ modelId, modelType, identifiers, customFields, options = {
|
|
29
|
+
modelOptions: {},
|
|
30
|
+
useCustomFieldsEntries: false,
|
|
31
|
+
}, }) => {
|
|
32
|
+
await ValueRepo.updateValues(modelType, modelId, identifiers, customFields, {
|
|
33
|
+
...options,
|
|
34
|
+
modelOptions: options.modelOptions ?? {},
|
|
35
|
+
});
|
|
36
|
+
/*
|
|
37
|
+
T.Y TODO - Once we're ready to switch from custom_field_values to custom_field_entries, we should remove the ValueRepo.updateValues call.
|
|
38
|
+
Currently, We're updating both tables to keep the data in sync, but not all microservices are using the new table yet.
|
|
39
|
+
*/
|
|
40
|
+
if (!options?.useCustomFieldsEntries) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const { dataValues: { customFields: oldCustomFields } } = await EntriesRepo.findEntriesByModelId(modelId, options) ?? { dataValues: {} };
|
|
44
|
+
const newCustomFields = { ...oldCustomFields, ...customFields };
|
|
45
|
+
await EntriesRepo.updateEntries(modelId, modelType, newCustomFields, identifiers, {
|
|
46
|
+
...options,
|
|
47
|
+
modelOptions: options.modelOptions ?? {},
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
exports.default = updateInstanceValues;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workaround to a bug in sequelize.🐛
|
|
3
|
+
*
|
|
4
|
+
* **afterFind hook** isn't working on nested (included) models.
|
|
5
|
+
* The solution here is to add a global afterFind hook,
|
|
6
|
+
* which manually calls the afterFind hook of each model, recursively
|
|
7
|
+
* https://github.com/sequelize/sequelize/issues/4627
|
|
8
|
+
*/
|
|
9
|
+
declare const handleChildrenAfterFindHook: (instances: any, options: any, level?: number) => any;
|
|
10
|
+
export default handleChildrenAfterFindHook;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Workaround to a bug in sequelize.🐛
|
|
4
|
+
*
|
|
5
|
+
* **afterFind hook** isn't working on nested (included) models.
|
|
6
|
+
* The solution here is to add a global afterFind hook,
|
|
7
|
+
* which manually calls the afterFind hook of each model, recursively
|
|
8
|
+
* https://github.com/sequelize/sequelize/issues/4627
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
const handleChildrenAfterFindHook = async (instances, options, level = 0) => {
|
|
12
|
+
if (!instances)
|
|
13
|
+
return Promise.resolve();
|
|
14
|
+
if (Array.isArray(instances)) {
|
|
15
|
+
return Promise.all(instances.map((instance) => {
|
|
16
|
+
const { options: instanceOptions } = instance.constructor;
|
|
17
|
+
return handleChildrenAfterFindHook(instance, instanceOptions, level);
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
const instance = instances;
|
|
21
|
+
const { constructor } = instance;
|
|
22
|
+
/**
|
|
23
|
+
* Root model will have already run their "afterFind" hook.
|
|
24
|
+
* Only run children "afterFind" hooks.
|
|
25
|
+
*/
|
|
26
|
+
if (level >= 1) {
|
|
27
|
+
await constructor.runHooks('afterFind', instance, options);
|
|
28
|
+
}
|
|
29
|
+
const { associations } = constructor;
|
|
30
|
+
const associatedNames = Object.keys(instance).filter((attribute) => Object.keys(associations).includes(attribute));
|
|
31
|
+
if (associatedNames.length) {
|
|
32
|
+
const childInstances = associatedNames.map((name) => instance[name]);
|
|
33
|
+
return handleChildrenAfterFindHook(childInstances, options, level + 1);
|
|
34
|
+
}
|
|
35
|
+
return Promise.resolve();
|
|
36
|
+
};
|
|
37
|
+
exports.default = handleChildrenAfterFindHook;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { CustomFieldOptions, ModelFetcher, Models } from "./types/index.js";
|
|
8
|
-
import { CustomFieldsSchema } from "./utils/validations/schema/custom-fields.js";
|
|
9
|
-
import { generateCustomFieldSearchQueryPayload, generateRandomString } from "./utils/helpers/index.js";
|
|
10
|
-
import { customFieldsSortScope } from "./scopes/filter.js";
|
|
11
|
-
import { Sequelize } from "sequelize-typescript";
|
|
12
|
-
import { Application } from "express";
|
|
13
|
-
|
|
14
|
-
//#region src/index.d.ts
|
|
1
|
+
import type { Application } from 'express';
|
|
2
|
+
import type { Sequelize } from 'sequelize-typescript';
|
|
3
|
+
import type { CustomFieldOptions, ModelFetcher, Models } from './types';
|
|
4
|
+
export * from './utils/validations/schema/custom-fields';
|
|
5
|
+
export * from './utils/constants';
|
|
6
|
+
export * from './utils/helpers';
|
|
15
7
|
/**
|
|
16
|
-
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
17
|
-
* @see {@link 'custom-fields/config'} for configurations
|
|
18
|
-
*/
|
|
19
|
-
declare const useCustomFields: (app:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export { CUSTOM_FIELDS_FILTER_SCOPE, CustomFieldDefinition, CustomFieldDefinitionType, CustomFieldEntries, CustomFieldValue, CustomFieldsSchema, CustomValidator, customFieldsSortScope, useCustomFields as default, disableCustomFields, generateCustomFieldSearchQueryPayload, generateRandomString, supportedEntities };
|
|
23
|
-
//# sourceMappingURL=index.d.ts.map
|
|
8
|
+
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
9
|
+
* @see {@link 'custom-fields/config'} for configurations
|
|
10
|
+
*/
|
|
11
|
+
declare const useCustomFields: (app: Application | null, getModel: ModelFetcher, options: CustomFieldOptions) => Promise<Sequelize>;
|
|
12
|
+
export default useCustomFields;
|
|
13
|
+
export declare const disableCustomFields: (models: Models[], getModel: ModelFetcher) => void;
|