@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/src/hooks/enrich.ts
CHANGED
|
@@ -4,9 +4,6 @@ import * as DefinitionRepo from '../repository/definition';
|
|
|
4
4
|
import CustomFieldValue from '../models/CustomFieldValue';
|
|
5
5
|
import CustomFieldDefinition from '../models/CustomFieldDefinition';
|
|
6
6
|
import { SerializedCustomFields } from '../types/definition';
|
|
7
|
-
import logger from '../utils/logger';
|
|
8
|
-
import { ModelOptions } from '../types';
|
|
9
|
-
import buildAttributesByInstance from '../utils/scopeAttributes';
|
|
10
7
|
|
|
11
8
|
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
12
9
|
|
|
@@ -33,7 +30,6 @@ const enrichResults = (
|
|
|
33
30
|
modelType: string,
|
|
34
31
|
scopeAttributes: string[],
|
|
35
32
|
hookType?: SupportedHookTypes,
|
|
36
|
-
modelOptions: ModelOptions = {},
|
|
37
33
|
) => async (
|
|
38
34
|
instancesOrInstance: any | any[],
|
|
39
35
|
options,
|
|
@@ -52,24 +48,19 @@ const enrichResults = (
|
|
|
52
48
|
|
|
53
49
|
instances = instances.filter(Boolean);
|
|
54
50
|
|
|
55
|
-
const identifiers =
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
const identifiers = instances.map((instance) =>
|
|
52
|
+
scopeAttributes
|
|
53
|
+
.map((attr) => instance[attr])).flat();
|
|
58
54
|
|
|
59
55
|
const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
|
|
60
|
-
|
|
61
|
-
console.log({ uniqueIdentifiers });
|
|
62
|
-
|
|
63
56
|
const identifierCustomFieldDefinitionsMapping = uniqueIdentifiers.reduce((map, identifier) => ({
|
|
64
57
|
...map,
|
|
65
58
|
[identifier]: [],
|
|
66
|
-
|
|
67
59
|
}), {});
|
|
68
|
-
|
|
69
60
|
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(
|
|
70
61
|
modelType,
|
|
71
62
|
uniqueIdentifiers,
|
|
72
|
-
{ transaction: options.transaction
|
|
63
|
+
{ transaction: options.transaction },
|
|
73
64
|
);
|
|
74
65
|
|
|
75
66
|
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
@@ -78,7 +69,7 @@ const enrichResults = (
|
|
|
78
69
|
}), {});
|
|
79
70
|
|
|
80
71
|
customFieldDefinitions.forEach((cfd) => {
|
|
81
|
-
identifierCustomFieldDefinitionsMapping[cfd.entityId]
|
|
72
|
+
identifierCustomFieldDefinitionsMapping[cfd.entityId].push(cfd);
|
|
82
73
|
});
|
|
83
74
|
|
|
84
75
|
// Get the values per instates ids:
|
|
@@ -113,7 +104,6 @@ const enrichResults = (
|
|
|
113
104
|
|
|
114
105
|
scopeAttributes.forEach((attribute) => {
|
|
115
106
|
const identifier = instance[attribute];
|
|
116
|
-
|
|
117
107
|
const entityCustomFieldDefinitions = identifierCustomFieldDefinitionsMapping[identifier];
|
|
118
108
|
if (entityCustomFieldDefinitions?.length > 0) {
|
|
119
109
|
entityCustomFieldDefinitions.forEach((customFieldDefinition) => {
|
package/src/hooks/update.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import logger from '../utils/logger';
|
|
2
2
|
import * as ValueRepo from '../repository/value';
|
|
3
|
-
import { ModelOptions } from '../types';
|
|
4
|
-
import buildAttributesByInstance from '../utils/scopeAttributes';
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* A hook to update the custom fields when updating a model (more then one instance).
|
|
@@ -16,14 +14,14 @@ export const beforeBulkUpdate = (options): void => {
|
|
|
16
14
|
* A hook to update the custom fields when updating a model instance.
|
|
17
15
|
* TODO - cleanup if update fail
|
|
18
16
|
*/
|
|
19
|
-
export const beforeUpdate = (scopeAttributes: string[]
|
|
17
|
+
export const beforeUpdate = (scopeAttributes: string[]) => async (
|
|
20
18
|
instance,
|
|
21
19
|
options,
|
|
22
20
|
): Promise<void> => {
|
|
23
21
|
logger.debug('sadot - before update hook');
|
|
24
22
|
const { fields } = options;
|
|
25
23
|
const modelType = instance.constructor.name;
|
|
26
|
-
const identifiers =
|
|
24
|
+
const identifiers = scopeAttributes.map((attribute) => instance[attribute]);
|
|
27
25
|
|
|
28
26
|
const customFieldsIdx = fields.indexOf('customFields');
|
|
29
27
|
if (customFieldsIdx > -1) {
|
|
@@ -33,8 +31,7 @@ export const beforeUpdate = (scopeAttributes: string[], modelOptions: ModelOptio
|
|
|
33
31
|
instance.id,
|
|
34
32
|
identifiers,
|
|
35
33
|
customFields,
|
|
36
|
-
{ transaction: options.transaction
|
|
37
|
-
|
|
34
|
+
{ transaction: options.transaction },
|
|
38
35
|
);
|
|
39
36
|
// eslint-disable-next-line no-param-reassign
|
|
40
37
|
fields.splice(customFieldsIdx, 1);
|
package/src/index.ts
CHANGED
|
@@ -7,9 +7,7 @@ import api from './api';
|
|
|
7
7
|
import initDB from './utils/db';
|
|
8
8
|
import logger from './utils/logger';
|
|
9
9
|
import type { CustomFieldOptions, ModelFetcher } from './types';
|
|
10
|
-
import {
|
|
11
|
-
addHooks, addScopes, applyCustomAssociation, removeHooks,
|
|
12
|
-
} from './utils/init';
|
|
10
|
+
import { addHooks, addScopes, removeHooks } from './utils/init';
|
|
13
11
|
|
|
14
12
|
export * from './utils/validations/custom-fields';
|
|
15
13
|
|
|
@@ -36,7 +34,6 @@ const useCustomFields = async (
|
|
|
36
34
|
addHooks(models, getModel);
|
|
37
35
|
await initTables(sequelize, options.getUser);
|
|
38
36
|
addScopes(models, getModel);
|
|
39
|
-
applyCustomAssociation(models);
|
|
40
37
|
logger.debug('sadot - custom fields finished initializing with models', models);
|
|
41
38
|
return sequelize;
|
|
42
39
|
};
|
package/src/models/index.ts
CHANGED
|
@@ -5,12 +5,10 @@ import logger from '../utils/logger';
|
|
|
5
5
|
import CustomFieldDefinition from './CustomFieldDefinition';
|
|
6
6
|
import CustomFieldValue from './CustomFieldValue';
|
|
7
7
|
import TestModel from './tests/TestModel';
|
|
8
|
-
import ContextAwareTestModel from './tests/contextAwareModels/ContextAwareTestModel';
|
|
9
|
-
import ContextModel from './tests/contextAwareModels/ContextModel';
|
|
10
8
|
import AssociatedTestModel from './tests/AssociatedTestModel';
|
|
11
9
|
|
|
12
10
|
const productionModels = [CustomFieldDefinition, CustomFieldValue];
|
|
13
|
-
const testModels = [TestModel, AssociatedTestModel
|
|
11
|
+
const testModels = [TestModel, AssociatedTestModel];
|
|
14
12
|
|
|
15
13
|
const SADOT_MIGRATION_PREFIX = 'sadot-migration';
|
|
16
14
|
const SCHEMA_VERSION = 1;
|
|
@@ -90,8 +88,6 @@ const initTestModels = async (sequelize: Sequelize): Promise<void> => {
|
|
|
90
88
|
logger.info('custom-fields: test models added');
|
|
91
89
|
await TestModel.sync({ alter: true });
|
|
92
90
|
await AssociatedTestModel.sync({ alter: true });
|
|
93
|
-
await ContextModel.sync({ alter: true });
|
|
94
|
-
await ContextAwareTestModel.sync({ alter: true });
|
|
95
91
|
logger.info('custom-fields: test models synced');
|
|
96
92
|
};
|
|
97
93
|
|
|
@@ -100,8 +96,6 @@ export {
|
|
|
100
96
|
CustomFieldDefinition,
|
|
101
97
|
TestModel,
|
|
102
98
|
AssociatedTestModel,
|
|
103
|
-
ContextAwareTestModel,
|
|
104
|
-
ContextModel,
|
|
105
99
|
initTables,
|
|
106
100
|
initTestModels,
|
|
107
101
|
};
|
|
@@ -1,28 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FindOptions,
|
|
3
|
-
Op, WhereOptions,
|
|
4
|
-
} from 'sequelize';
|
|
1
|
+
import { Op, WhereOptions } from 'sequelize';
|
|
5
2
|
import { CustomFieldDefinition } from '../models';
|
|
6
3
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
7
|
-
import { ModelOptions } from '../types';
|
|
8
4
|
|
|
9
5
|
export const create = (data: CreateCustomFieldDefinition): Promise<CustomFieldDefinition> =>
|
|
10
6
|
CustomFieldDefinition.create(data);
|
|
11
7
|
|
|
12
8
|
export const findAll = (
|
|
13
|
-
where:
|
|
9
|
+
where: any,
|
|
14
10
|
options: any = { withDisabled: false },
|
|
15
11
|
): Promise<CustomFieldDefinition[]> => {
|
|
16
12
|
const queryModel = options.withDisabled
|
|
17
13
|
? CustomFieldDefinition.unscoped()
|
|
18
14
|
: CustomFieldDefinition;
|
|
19
|
-
|
|
20
15
|
return queryModel.scope('userScope').findAll({
|
|
21
16
|
where,
|
|
22
17
|
transaction: options.transaction,
|
|
23
18
|
raw: true,
|
|
24
|
-
include: options.include,
|
|
25
|
-
logging: console.log,
|
|
26
19
|
});
|
|
27
20
|
};
|
|
28
21
|
|
|
@@ -42,26 +35,26 @@ export const findById = (
|
|
|
42
35
|
return CustomFieldDefinition.scope('userScope').findByPk(id);
|
|
43
36
|
};
|
|
44
37
|
|
|
38
|
+
export const findByEntityId = async (
|
|
39
|
+
entityId: string,
|
|
40
|
+
options: any = {},
|
|
41
|
+
): Promise<CustomFieldDefinition[]> => CustomFieldDefinition.findAll({
|
|
42
|
+
where: { entityId },
|
|
43
|
+
transaction: options.transaction,
|
|
44
|
+
});
|
|
45
|
+
|
|
45
46
|
export const findByEntityIds = async (
|
|
46
47
|
modelType: string,
|
|
47
48
|
entityIds: string[],
|
|
48
|
-
options:
|
|
49
|
-
): Promise<CustomFieldDefinition[]> => {
|
|
50
|
-
|
|
51
|
-
const where: WhereOptions = {
|
|
49
|
+
options: any = {},
|
|
50
|
+
): Promise<CustomFieldDefinition[]> => CustomFieldDefinition.findAll({
|
|
51
|
+
where: {
|
|
52
52
|
modelType,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return CustomFieldDefinition.findAll({
|
|
59
|
-
where,
|
|
60
|
-
transaction: options.transaction,
|
|
61
|
-
include: include?.(entityIds),
|
|
62
|
-
raw: true,
|
|
63
|
-
});
|
|
64
|
-
};
|
|
53
|
+
entityId: entityIds,
|
|
54
|
+
},
|
|
55
|
+
transaction: options.transaction,
|
|
56
|
+
raw: true,
|
|
57
|
+
});
|
|
65
58
|
|
|
66
59
|
export const findByWhere = (where): Promise<CustomFieldDefinition | null> =>
|
|
67
60
|
CustomFieldDefinition.scope('userScope').findOne({
|
|
@@ -107,23 +100,10 @@ export const getRequiredFields = async (
|
|
|
107
100
|
modelType: string,
|
|
108
101
|
modelId: string | string[],
|
|
109
102
|
entityId: string | string[],
|
|
110
|
-
modelOptions: ModelOptions = {},
|
|
111
103
|
): Promise<string[]> => {
|
|
112
104
|
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
113
|
-
const { include, useEntityIdFromInclude } = modelOptions;
|
|
114
|
-
|
|
115
|
-
const where: WhereOptions = {
|
|
116
|
-
modelType,
|
|
117
|
-
required: true,
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
if (!useEntityIdFromInclude) {
|
|
121
|
-
where.entityId = entityIds;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
105
|
const requiredFields = await CustomFieldDefinition.findAll({
|
|
125
|
-
where,
|
|
126
|
-
include: include?.(entityIds),
|
|
106
|
+
where: { required: true, modelType, entityId: { [Op.in]: entityIds } },
|
|
127
107
|
logging: true,
|
|
128
108
|
});
|
|
129
109
|
const requiredFieldsNames = requiredFields.map((definition) => definition.name);
|
package/src/repository/value.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/* eslint-disable max-len */
|
|
2
|
-
import { FindOptions, WhereOptions } from 'sequelize';
|
|
3
2
|
import { CustomFieldValue, CustomFieldDefinition } from '../models';
|
|
4
3
|
import * as DefinitionRepo from './definition';
|
|
5
4
|
import { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
|
|
6
5
|
import logger from '../utils/logger';
|
|
7
6
|
import { MissingDefinitionError } from '../errors';
|
|
8
|
-
import { ModelOptions } from '../types';
|
|
9
7
|
|
|
10
8
|
export const findByModelIdAndDefinition = async (modelId: string, customFieldDefinitionId: string) =>
|
|
11
9
|
CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [CustomFieldDefinition] });
|
|
@@ -52,24 +50,15 @@ export const updateValues = async (
|
|
|
52
50
|
modelId: string,
|
|
53
51
|
identifiers: string[],
|
|
54
52
|
valuesToUpdate: ValuesToUpdate,
|
|
55
|
-
options:
|
|
53
|
+
options: any = {},
|
|
56
54
|
): Promise<CustomFieldValue[]> => {
|
|
57
55
|
const names = Object.keys(valuesToUpdate);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const where: WhereOptions = {
|
|
64
|
-
modelType,
|
|
65
|
-
name: names,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
if (!options.modelOptions?.useEntityIdFromInclude) {
|
|
69
|
-
where.entityId = identifiers;
|
|
70
|
-
}
|
|
71
|
-
const fieldDefinitions = await DefinitionRepo.findAll(where, { withDisabled: true, transaction, include: modelOptions.include?.(identifiers) }) || [];
|
|
56
|
+
const fieldDefinitions = await DefinitionRepo.findAll(
|
|
57
|
+
{ modelType, name: names, entityId: identifiers },
|
|
58
|
+
{ withDisabled: true, transaction: options.transaction },
|
|
59
|
+
) || [];
|
|
72
60
|
|
|
61
|
+
logger.info(`custom-fields: updating values for ${modelType} ${modelId}`, { valuesToUpdate, entityId: identifiers });
|
|
73
62
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
74
63
|
if (fieldDefinitions.length !== names.length) {
|
|
75
64
|
logger.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
@@ -4,7 +4,6 @@ export default {
|
|
|
4
4
|
password: process.env.DB_PASSWORD || null,
|
|
5
5
|
database: process.env.DB_NAME || 'sadot_package_test',
|
|
6
6
|
host: process.env.DB_HOST || '127.0.0.1',
|
|
7
|
-
port: process.env.DB_PORT || 5432,
|
|
8
7
|
dialect: process.env.DB_TYPE || 'postgres',
|
|
9
8
|
define: {
|
|
10
9
|
underscored: true,
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ContextAwareTestModel, ContextModel, CustomFieldDefinition, TestModel,
|
|
3
|
-
} from '../../models';
|
|
1
|
+
import { CustomFieldDefinition, TestModel } from '../../models';
|
|
4
2
|
|
|
5
3
|
// eslint-disable-next-line import/prefer-default-export
|
|
6
4
|
export const cleanup = async (): Promise<void> => {
|
|
7
5
|
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
|
|
8
6
|
await CustomFieldDefinition.unscoped().destroy({ where: {} });
|
|
9
7
|
await TestModel.destroy({ where: {} });
|
|
10
|
-
await ContextAwareTestModel.destroy({ where: {} });
|
|
11
|
-
await ContextModel.destroy({ where: {} });
|
|
12
8
|
}
|
|
13
9
|
};
|
|
14
10
|
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
2
|
import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
3
3
|
|
|
4
|
-
export const contextAwareFieldDefinition = {
|
|
5
|
-
name: 'cool field',
|
|
6
|
-
modelType: 'ContextAwareTestModel',
|
|
7
|
-
fieldType: 'number',
|
|
8
|
-
entityType: 'fleetId',
|
|
9
|
-
};
|
|
10
|
-
|
|
11
4
|
export const coolFieldDefinition: CreateCustomFieldDefinition = {
|
|
12
5
|
name: 'cool field',
|
|
13
6
|
modelType: 'TestModel',
|
package/src/types/index.ts
CHANGED
|
@@ -1,37 +1,15 @@
|
|
|
1
|
-
import { Includeable } from 'sequelize';
|
|
2
|
-
import { ModelCtor } from 'sequelize-typescript';
|
|
3
|
-
|
|
4
1
|
export type ModelFetcher = (name: string) => any;
|
|
5
2
|
|
|
6
3
|
export type ModelOptions = {
|
|
7
|
-
include?: (entityIds: string[]) => Includeable[];
|
|
8
|
-
/**
|
|
9
|
-
* A function that will be called to resolve the entity id.
|
|
10
|
-
* @param {string | string[]} - The entity id.
|
|
11
|
-
* @returns {void}
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
customAssociation?: (model: ModelCtor) => void;
|
|
15
|
-
/**
|
|
16
|
-
* Whether to use the entity id from the instance per scope attribute
|
|
17
|
-
* @param {string} -
|
|
18
|
-
* @returns {void}
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
useEntityIdFromInclude?: boolean
|
|
22
|
-
}
|
|
23
|
-
export type Models = {
|
|
24
4
|
name: string;
|
|
25
|
-
scopeAttributes:
|
|
26
|
-
modelOptions?: ModelOptions;
|
|
5
|
+
scopeAttributes: string[];
|
|
27
6
|
creationWebhookHandler?: (instance: any) => any;
|
|
28
7
|
updateWebhookHandler?: (instance: any) => any;
|
|
29
8
|
deletionWebhookHandler?: (instance: any) => any;
|
|
30
9
|
}
|
|
31
10
|
|
|
32
|
-
export type CustomFieldOptions
|
|
33
|
-
models:
|
|
11
|
+
export type CustomFieldOptions= {
|
|
12
|
+
models: ModelOptions[];
|
|
34
13
|
databaseConfig: any;
|
|
35
14
|
getUser: () => any;
|
|
36
|
-
|
|
37
15
|
};
|
package/src/utils/init.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { DataTypes } from 'sequelize';
|
|
|
2
2
|
import { ModelCtor } from 'sequelize-typescript';
|
|
3
3
|
import { customFields } from '@autofleet/common-types';
|
|
4
4
|
import {
|
|
5
|
-
CustomFieldDefinition,
|
|
6
5
|
CustomFieldValue,
|
|
7
6
|
} from '../models';
|
|
8
7
|
import {
|
|
@@ -15,14 +14,12 @@ import {
|
|
|
15
14
|
} from '../hooks';
|
|
16
15
|
import { customFieldsFilterScope } from '../scopes';
|
|
17
16
|
import logger from './logger';
|
|
18
|
-
import type { ModelFetcher,
|
|
17
|
+
import type { ModelFetcher, ModelOptions } from '../types';
|
|
19
18
|
|
|
20
19
|
const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
21
20
|
|
|
22
|
-
export const addHooks = (models:
|
|
23
|
-
models.forEach(async ({
|
|
24
|
-
name, scopeAttributes, modelOptions,
|
|
25
|
-
}) => {
|
|
21
|
+
export const addHooks = (models: ModelOptions[], getModel: ModelFetcher): void => {
|
|
22
|
+
models.forEach(async ({ name, scopeAttributes }) => {
|
|
26
23
|
try {
|
|
27
24
|
const model = getModel(name);
|
|
28
25
|
if (!model) {
|
|
@@ -41,18 +38,18 @@ export const addHooks = (models: Models[], getModel: ModelFetcher): void => {
|
|
|
41
38
|
model.addHook('beforeFind', 'sadot-beforeFind', beforeFind(scopeAttributes));
|
|
42
39
|
model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', beforeBulkCreate);
|
|
43
40
|
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', beforeBulkUpdate);
|
|
44
|
-
model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes
|
|
45
|
-
model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes
|
|
46
|
-
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind'
|
|
47
|
-
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes
|
|
48
|
-
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes
|
|
41
|
+
model.addHook('beforeCreate', 'sadot-beforeCreate', beforeCreate(scopeAttributes));
|
|
42
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', beforeUpdate(scopeAttributes));
|
|
43
|
+
model.addHook('afterFind', 'sadot-afterFind', enrichResults(name, scopeAttributes, 'afterFind'));
|
|
44
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', enrichResults(name, scopeAttributes));
|
|
45
|
+
model.addHook('afterCreate', 'sadot-afterCreate', enrichResults(name, scopeAttributes));
|
|
49
46
|
} catch (e) {
|
|
50
47
|
logger.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
51
48
|
}
|
|
52
49
|
});
|
|
53
50
|
};
|
|
54
51
|
|
|
55
|
-
export const removeHooks = (models:
|
|
52
|
+
export const removeHooks = (models: ModelOptions[], getModel: ModelFetcher): void => {
|
|
56
53
|
models.forEach(async ({ name }) => {
|
|
57
54
|
try {
|
|
58
55
|
const model = getModel(name);
|
|
@@ -84,7 +81,7 @@ const addAssociations = (model: ModelCtor, modelName: string): void => {
|
|
|
84
81
|
CustomFieldValue.belongsTo(model, { foreignKey: 'modelId', as: modelName });
|
|
85
82
|
};
|
|
86
83
|
|
|
87
|
-
export const addScopes = (models:
|
|
84
|
+
export const addScopes = (models: ModelOptions[], getModel: ModelFetcher): void => {
|
|
88
85
|
models.forEach(async ({ name, scopeAttributes }) => {
|
|
89
86
|
try {
|
|
90
87
|
const model = getModel(name);
|
|
@@ -104,9 +101,3 @@ export const addScopes = (models: Models[], getModel: ModelFetcher): void => {
|
|
|
104
101
|
}
|
|
105
102
|
});
|
|
106
103
|
};
|
|
107
|
-
|
|
108
|
-
export const applyCustomAssociation = (models: Models[]): void => {
|
|
109
|
-
models.forEach(({ modelOptions }) => {
|
|
110
|
-
modelOptions?.customAssociation?.(CustomFieldDefinition);
|
|
111
|
-
});
|
|
112
|
-
};
|
package/.env
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Model } from 'sequelize-typescript';
|
|
2
|
-
import ContextModel from './ContextModel';
|
|
3
|
-
declare class ContextAwareTestModel extends Model {
|
|
4
|
-
id: string;
|
|
5
|
-
contextId: string;
|
|
6
|
-
coolAttribute?: boolean;
|
|
7
|
-
customFields?: any;
|
|
8
|
-
context: ContextModel;
|
|
9
|
-
}
|
|
10
|
-
export default ContextAwareTestModel;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
16
|
-
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
17
|
-
const ContextModel_1 = __importDefault(require("./ContextModel"));
|
|
18
|
-
let ContextAwareTestModel = class ContextAwareTestModel extends sequelize_typescript_1.Model {
|
|
19
|
-
};
|
|
20
|
-
__decorate([
|
|
21
|
-
sequelize_typescript_1.PrimaryKey,
|
|
22
|
-
(0, sequelize_typescript_1.Column)({
|
|
23
|
-
type: sequelize_typescript_1.DataType.UUID,
|
|
24
|
-
defaultValue: sequelize_typescript_1.DataType.UUIDV4,
|
|
25
|
-
allowNull: false,
|
|
26
|
-
}),
|
|
27
|
-
__metadata("design:type", String)
|
|
28
|
-
], ContextAwareTestModel.prototype, "id", void 0);
|
|
29
|
-
__decorate([
|
|
30
|
-
(0, sequelize_typescript_1.ForeignKey)(() => ContextModel_1.default),
|
|
31
|
-
(0, sequelize_typescript_1.Column)({
|
|
32
|
-
type: sequelize_typescript_1.DataType.UUID,
|
|
33
|
-
}),
|
|
34
|
-
__metadata("design:type", String)
|
|
35
|
-
], ContextAwareTestModel.prototype, "contextId", void 0);
|
|
36
|
-
__decorate([
|
|
37
|
-
(0, sequelize_typescript_1.Column)({
|
|
38
|
-
type: sequelize_typescript_1.DataType.BOOLEAN,
|
|
39
|
-
}),
|
|
40
|
-
__metadata("design:type", Boolean)
|
|
41
|
-
], ContextAwareTestModel.prototype, "coolAttribute", void 0);
|
|
42
|
-
__decorate([
|
|
43
|
-
(0, sequelize_typescript_1.Column)({
|
|
44
|
-
type: sequelize_typescript_1.DataType.VIRTUAL,
|
|
45
|
-
}),
|
|
46
|
-
__metadata("design:type", Object)
|
|
47
|
-
], ContextAwareTestModel.prototype, "customFields", void 0);
|
|
48
|
-
__decorate([
|
|
49
|
-
(0, sequelize_typescript_1.BelongsTo)(() => ContextModel_1.default),
|
|
50
|
-
__metadata("design:type", ContextModel_1.default)
|
|
51
|
-
], ContextAwareTestModel.prototype, "context", void 0);
|
|
52
|
-
ContextAwareTestModel = __decorate([
|
|
53
|
-
(0, sequelize_typescript_1.Table)({ createdAt: false, updatedAt: false })
|
|
54
|
-
], ContextAwareTestModel);
|
|
55
|
-
exports.default = ContextAwareTestModel;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Model } from 'sequelize-typescript';
|
|
2
|
-
declare enum EEntityTypes {
|
|
3
|
-
BUSINESS_MODEL = "businessModel",
|
|
4
|
-
CONTEXT = "context",
|
|
5
|
-
DEMAND_SOURCE = "demandSource",
|
|
6
|
-
FLEET = "fleet"
|
|
7
|
-
}
|
|
8
|
-
declare class ContextModel extends Model {
|
|
9
|
-
id: string;
|
|
10
|
-
entityId: string;
|
|
11
|
-
entityType: EEntityTypes;
|
|
12
|
-
}
|
|
13
|
-
export default ContextModel;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
13
|
-
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
14
|
-
// eslint-disable-next-line no-shadow
|
|
15
|
-
var EEntityTypes;
|
|
16
|
-
(function (EEntityTypes) {
|
|
17
|
-
EEntityTypes["BUSINESS_MODEL"] = "businessModel";
|
|
18
|
-
EEntityTypes["CONTEXT"] = "context";
|
|
19
|
-
EEntityTypes["DEMAND_SOURCE"] = "demandSource";
|
|
20
|
-
EEntityTypes["FLEET"] = "fleet";
|
|
21
|
-
})(EEntityTypes || (EEntityTypes = {}));
|
|
22
|
-
let ContextModel = class ContextModel extends sequelize_typescript_1.Model {
|
|
23
|
-
};
|
|
24
|
-
__decorate([
|
|
25
|
-
sequelize_typescript_1.PrimaryKey,
|
|
26
|
-
(0, sequelize_typescript_1.Column)({
|
|
27
|
-
defaultValue: sequelize_typescript_1.DataType.UUIDV4,
|
|
28
|
-
type: sequelize_typescript_1.DataType.UUID,
|
|
29
|
-
}),
|
|
30
|
-
__metadata("design:type", String)
|
|
31
|
-
], ContextModel.prototype, "id", void 0);
|
|
32
|
-
__decorate([
|
|
33
|
-
(0, sequelize_typescript_1.Column)({
|
|
34
|
-
type: sequelize_typescript_1.DataType.UUID,
|
|
35
|
-
}),
|
|
36
|
-
__metadata("design:type", String)
|
|
37
|
-
], ContextModel.prototype, "entityId", void 0);
|
|
38
|
-
__decorate([
|
|
39
|
-
(0, sequelize_typescript_1.Column)({
|
|
40
|
-
type: sequelize_typescript_1.DataType.ENUM(...Object.values(EEntityTypes)),
|
|
41
|
-
}),
|
|
42
|
-
__metadata("design:type", String)
|
|
43
|
-
], ContextModel.prototype, "entityType", void 0);
|
|
44
|
-
ContextModel = __decorate([
|
|
45
|
-
(0, sequelize_typescript_1.Table)({ createdAt: false, updatedAt: false })
|
|
46
|
-
], ContextModel);
|
|
47
|
-
exports.default = ContextModel;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const mapAttributeToInstance = (scopeAttributes, instance) => scopeAttributes.map((attr) => instance[attr]);
|
|
4
|
-
const buildAttributesByInstance = (instance, scopeAttributes) => {
|
|
5
|
-
const uniqueAttributes = Array.from(new Set(scopeAttributes));
|
|
6
|
-
if (Array.isArray(instance)) {
|
|
7
|
-
return instance.flatMap((ins) => mapAttributeToInstance(uniqueAttributes, ins));
|
|
8
|
-
}
|
|
9
|
-
return mapAttributeToInstance(uniqueAttributes, instance);
|
|
10
|
-
};
|
|
11
|
-
exports.default = buildAttributesByInstance;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
import {
|
|
3
|
-
Table,
|
|
4
|
-
Column,
|
|
5
|
-
Model,
|
|
6
|
-
PrimaryKey,
|
|
7
|
-
DataType,
|
|
8
|
-
HasMany,
|
|
9
|
-
BelongsTo,
|
|
10
|
-
ForeignKey,
|
|
11
|
-
} from 'sequelize-typescript';
|
|
12
|
-
import AssociatedTestModel from '../AssociatedTestModel';
|
|
13
|
-
import ContextModel from './ContextModel';
|
|
14
|
-
|
|
15
|
-
@Table({ createdAt: false, updatedAt: false })
|
|
16
|
-
class ContextAwareTestModel extends Model {
|
|
17
|
-
@PrimaryKey
|
|
18
|
-
@Column({
|
|
19
|
-
type: DataType.UUID,
|
|
20
|
-
defaultValue: DataType.UUIDV4,
|
|
21
|
-
allowNull: false,
|
|
22
|
-
})
|
|
23
|
-
id!: string;
|
|
24
|
-
|
|
25
|
-
@ForeignKey(() => ContextModel)
|
|
26
|
-
@Column({
|
|
27
|
-
type: DataType.UUID,
|
|
28
|
-
})
|
|
29
|
-
contextId: string;
|
|
30
|
-
|
|
31
|
-
@Column({
|
|
32
|
-
type: DataType.BOOLEAN,
|
|
33
|
-
})
|
|
34
|
-
coolAttribute?: boolean;
|
|
35
|
-
|
|
36
|
-
@Column({
|
|
37
|
-
type: DataType.VIRTUAL,
|
|
38
|
-
})
|
|
39
|
-
customFields?: any;
|
|
40
|
-
|
|
41
|
-
@BelongsTo(() => ContextModel)
|
|
42
|
-
context: ContextModel;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export default ContextAwareTestModel;
|