@autofleet/sadot 0.5.4-beta.26 → 0.5.4-beta.4
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 +4 -17
- package/dist/hooks/update.d.ts +1 -2
- package/dist/hooks/update.js +3 -4
- package/dist/index.js +1 -2
- 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 -27
- package/dist/repository/value.d.ts +1 -5
- package/dist/repository/value.js +3 -13
- 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 +4 -20
- package/dist/utils/init.d.ts +4 -5
- package/dist/utils/init.js +7 -13
- package/package.json +2 -2
- package/src/api/v1/definition/index.ts +4 -2
- package/src/hooks/create.ts +5 -13
- package/src/hooks/enrich.ts +4 -20
- package/src/hooks/update.ts +3 -6
- package/src/index.ts +2 -5
- package/src/models/index.ts +1 -7
- package/src/repository/definition.ts +19 -38
- package/src/repository/value.ts +7 -18
- 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 +4 -21
- package/src/utils/init.ts +10 -19
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +0 -10
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +0 -55
- package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +0 -13
- package/dist/models/tests/contextAwareModels/ContextTestModel.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/ContextTestModel.ts +0 -38
- package/src/utils/scopeAttributes.ts +0 -12
package/src/hooks/enrich.ts
CHANGED
|
@@ -4,8 +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 { ModelOptions } from '../types';
|
|
8
|
-
import applyScopeToInstance from '../utils/scopeAttributes';
|
|
9
7
|
|
|
10
8
|
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
|
11
9
|
|
|
@@ -32,7 +30,6 @@ const enrichResults = (
|
|
|
32
30
|
modelType: string,
|
|
33
31
|
scopeAttributes: string[],
|
|
34
32
|
hookType?: SupportedHookTypes,
|
|
35
|
-
modelOptions: ModelOptions = {},
|
|
36
33
|
) => async (
|
|
37
34
|
instancesOrInstance: any | any[],
|
|
38
35
|
options,
|
|
@@ -51,33 +48,21 @@ const enrichResults = (
|
|
|
51
48
|
|
|
52
49
|
instances = instances.filter(Boolean);
|
|
53
50
|
|
|
54
|
-
const identifiers =
|
|
51
|
+
const identifiers = instances.map((instance) =>
|
|
52
|
+
scopeAttributes
|
|
53
|
+
.map((attr) => instance[attr])).flat();
|
|
55
54
|
|
|
56
55
|
const uniqueIdentifiers = [...new Set(identifiers)].filter(Boolean);
|
|
57
|
-
|
|
58
56
|
const identifierCustomFieldDefinitionsMapping = uniqueIdentifiers.reduce((map, identifier) => ({
|
|
59
57
|
...map,
|
|
60
58
|
[identifier]: [],
|
|
61
|
-
|
|
62
59
|
}), {});
|
|
63
|
-
|
|
64
60
|
const customFieldDefinitions = await DefinitionRepo.findByEntityIds(
|
|
65
61
|
modelType,
|
|
66
62
|
uniqueIdentifiers,
|
|
67
|
-
{ transaction: options.transaction
|
|
63
|
+
{ transaction: options.transaction },
|
|
68
64
|
);
|
|
69
65
|
|
|
70
|
-
if (modelOptions?.include && modelOptions.useEntityIdFromInclude) {
|
|
71
|
-
// if we pass useEntityIdFromInclude,
|
|
72
|
-
// map the entity from the options to the identifierCustomFieldDefinitionsMapping
|
|
73
|
-
modelOptions.include(identifiers).forEach(({ model }) => {
|
|
74
|
-
customFieldDefinitions.forEach((cfd) => {
|
|
75
|
-
const entityId = cfd[`${model.name}.entityId`];
|
|
76
|
-
identifierCustomFieldDefinitionsMapping[entityId] = [];
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
66
|
const definitionsMap = customFieldDefinitions.reduce((map, definition) => ({
|
|
82
67
|
...map,
|
|
83
68
|
[definition.id]: definition,
|
|
@@ -119,7 +104,6 @@ const enrichResults = (
|
|
|
119
104
|
|
|
120
105
|
scopeAttributes.forEach((attribute) => {
|
|
121
106
|
const identifier = instance[attribute];
|
|
122
|
-
|
|
123
107
|
const entityCustomFieldDefinitions = identifierCustomFieldDefinitionsMapping[identifier];
|
|
124
108
|
if (entityCustomFieldDefinitions?.length > 0) {
|
|
125
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 applyScopeToInstance 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
|
|
|
@@ -26,7 +24,7 @@ const useCustomFields = async (
|
|
|
26
24
|
): Promise<Sequelize> => {
|
|
27
25
|
const { models } = options;
|
|
28
26
|
if (app) {
|
|
29
|
-
app.use('/api', api);
|
|
27
|
+
app.use('/api', options.middleware, api);
|
|
30
28
|
}
|
|
31
29
|
const sequelize = initDB(options.databaseConfig);
|
|
32
30
|
if (process.env.NODE_ENV === 'test') {
|
|
@@ -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 ContextTestModel from './tests/contextAwareModels/ContextTestModel';
|
|
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 ContextTestModel.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
|
-
ContextTestModel,
|
|
105
99
|
initTables,
|
|
106
100
|
initTestModels,
|
|
107
101
|
};
|
|
@@ -1,27 +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
19
|
});
|
|
26
20
|
};
|
|
27
21
|
|
|
@@ -41,26 +35,26 @@ export const findById = (
|
|
|
41
35
|
return CustomFieldDefinition.scope('userScope').findByPk(id);
|
|
42
36
|
};
|
|
43
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
|
+
|
|
44
46
|
export const findByEntityIds = async (
|
|
45
47
|
modelType: string,
|
|
46
48
|
entityIds: string[],
|
|
47
|
-
options:
|
|
48
|
-
): Promise<CustomFieldDefinition[]> => {
|
|
49
|
-
|
|
50
|
-
const where: WhereOptions = {
|
|
49
|
+
options: any = {},
|
|
50
|
+
): Promise<CustomFieldDefinition[]> => CustomFieldDefinition.findAll({
|
|
51
|
+
where: {
|
|
51
52
|
modelType,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return CustomFieldDefinition.findAll({
|
|
58
|
-
where,
|
|
59
|
-
transaction: options.transaction,
|
|
60
|
-
include: include?.(entityIds),
|
|
61
|
-
raw: true,
|
|
62
|
-
});
|
|
63
|
-
};
|
|
53
|
+
entityId: entityIds,
|
|
54
|
+
},
|
|
55
|
+
transaction: options.transaction,
|
|
56
|
+
raw: true,
|
|
57
|
+
});
|
|
64
58
|
|
|
65
59
|
export const findByWhere = (where): Promise<CustomFieldDefinition | null> =>
|
|
66
60
|
CustomFieldDefinition.scope('userScope').findOne({
|
|
@@ -106,23 +100,10 @@ export const getRequiredFields = async (
|
|
|
106
100
|
modelType: string,
|
|
107
101
|
modelId: string | string[],
|
|
108
102
|
entityId: string | string[],
|
|
109
|
-
modelOptions: ModelOptions = {},
|
|
110
103
|
): Promise<string[]> => {
|
|
111
104
|
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
112
|
-
const { include, useEntityIdFromInclude } = modelOptions;
|
|
113
|
-
|
|
114
|
-
const where: WhereOptions = {
|
|
115
|
-
modelType,
|
|
116
|
-
required: true,
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
if (!useEntityIdFromInclude) {
|
|
120
|
-
where.entityId = entityIds;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
105
|
const requiredFields = await CustomFieldDefinition.findAll({
|
|
124
|
-
where,
|
|
125
|
-
include: include?.(entityIds),
|
|
106
|
+
where: { required: true, modelType, entityId: { [Op.in]: entityIds } },
|
|
126
107
|
logging: true,
|
|
127
108
|
});
|
|
128
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 type { 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,27 +50,18 @@ 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
|
-
|
|
58
|
-
logger.info(`custom-fields: updating values for ${modelType} ${modelId}`, {
|
|
59
|
-
names, options, valuesToUpdate, identifiers,
|
|
60
|
-
});
|
|
61
|
-
const { modelOptions, transaction } = options;
|
|
55
|
+
logger.info(`custom-fields: updating values for ${modelType} ${modelId}`, { valuesToUpdate });
|
|
62
56
|
|
|
63
|
-
const
|
|
64
|
-
|
|
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) }) || [];
|
|
57
|
+
const names = Object.keys(valuesToUpdate);
|
|
58
|
+
const fieldDefinitions = await DefinitionRepo.findAll(
|
|
59
|
+
{ modelType, name: names, entityId: identifiers },
|
|
60
|
+
{ withDisabled: true, transaction: options.transaction },
|
|
61
|
+
) || [];
|
|
72
62
|
|
|
73
63
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
74
64
|
if (fieldDefinitions.length !== names.length) {
|
|
75
|
-
logger.info(`custom-fields: missing definitions for ${modelType} ${modelId}`, { names, fieldDefinitions });
|
|
76
65
|
const missingDefinitions = names.filter((name) => !fieldDefinitions.some((def) => def.name === name));
|
|
77
66
|
throw new MissingDefinitionError(missingDefinitions);
|
|
78
67
|
}
|
|
@@ -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, ContextTestModel, 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 ContextTestModel.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,35 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ModelCtor } from 'sequelize-typescript';
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
3
2
|
|
|
4
3
|
export type ModelFetcher = (name: string) => any;
|
|
5
4
|
|
|
6
5
|
export type ModelOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* Include options for the model
|
|
9
|
-
*/
|
|
10
|
-
include?: (entityIds: string[]) => IncludeOptions[];
|
|
11
|
-
/**
|
|
12
|
-
* Custom association for the model
|
|
13
|
-
* @param model - The model to associate with
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
customAssociation?: (model: ModelCtor) => void;
|
|
17
|
-
/**
|
|
18
|
-
* Whether to use the entity id from the instance per scope attribute
|
|
19
|
-
*/
|
|
20
|
-
useEntityIdFromInclude?: boolean
|
|
21
|
-
}
|
|
22
|
-
export type Models = {
|
|
23
6
|
name: string;
|
|
24
|
-
scopeAttributes:
|
|
25
|
-
modelOptions?: ModelOptions;
|
|
7
|
+
scopeAttributes: string[];
|
|
26
8
|
creationWebhookHandler?: (instance: any) => any;
|
|
27
9
|
updateWebhookHandler?: (instance: any) => any;
|
|
28
10
|
deletionWebhookHandler?: (instance: any) => any;
|
|
29
11
|
}
|
|
30
12
|
|
|
31
13
|
export type CustomFieldOptions = {
|
|
32
|
-
models:
|
|
14
|
+
models: ModelOptions[];
|
|
33
15
|
databaseConfig: any;
|
|
34
16
|
getUser: () => any;
|
|
17
|
+
middleware?: RequestHandler
|
|
35
18
|
};
|
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
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Model } from 'sequelize-typescript';
|
|
2
|
-
import ContextTestModel from './ContextTestModel';
|
|
3
|
-
declare class ContextAwareTestModel extends Model {
|
|
4
|
-
id: string;
|
|
5
|
-
contextId: string;
|
|
6
|
-
coolAttribute?: boolean;
|
|
7
|
-
customFields?: any;
|
|
8
|
-
context: ContextTestModel;
|
|
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 ContextTestModel_1 = __importDefault(require("./ContextTestModel"));
|
|
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)(() => ContextTestModel_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)(() => ContextTestModel_1.default),
|
|
50
|
-
__metadata("design:type", ContextTestModel_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 ContextTestModel extends Model {
|
|
9
|
-
id: string;
|
|
10
|
-
entityId: string;
|
|
11
|
-
entityType: EEntityTypes;
|
|
12
|
-
}
|
|
13
|
-
export default ContextTestModel;
|
|
@@ -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 ContextTestModel = class ContextTestModel 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
|
-
], ContextTestModel.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
|
-
], ContextTestModel.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
|
-
], ContextTestModel.prototype, "entityType", void 0);
|
|
44
|
-
ContextTestModel = __decorate([
|
|
45
|
-
(0, sequelize_typescript_1.Table)({ createdAt: false, updatedAt: false })
|
|
46
|
-
], ContextTestModel);
|
|
47
|
-
exports.default = ContextTestModel;
|
|
@@ -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 applyScopeToInstance = (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 = applyScopeToInstance;
|
|
@@ -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 ContextTestModel from './ContextTestModel';
|
|
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(() => ContextTestModel)
|
|
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(() => ContextTestModel)
|
|
42
|
-
context: ContextTestModel;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export default ContextAwareTestModel;
|