@autofleet/sadot 0.6.0 → 0.6.1-beta.1
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/index.d.ts +2 -0
- package/dist/index.js +4 -1
- package/dist/tests/functional/searching/index.d.ts +8 -0
- package/dist/tests/functional/searching/index.js +44 -0
- package/dist/utils/helpers/index.d.ts +25 -0
- package/dist/utils/helpers/index.js +34 -0
- package/package.json +1 -1
- package/src/index.ts +3 -0
- package/src/tests/functional/searching/index.ts +39 -0
- package/src/utils/helpers/index.ts +60 -0
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { Sequelize } from 'sequelize-typescript';
|
|
|
3
3
|
import type { CustomFieldOptions, ModelFetcher } from './types';
|
|
4
4
|
export * from './utils/validations/custom-fields';
|
|
5
5
|
export * from './utils/constants';
|
|
6
|
+
export * from './utils/helpers';
|
|
7
|
+
export { CustomFieldDefinitionType } from './utils/validations/type';
|
|
6
8
|
/**
|
|
7
9
|
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
8
10
|
* @see {@link 'custom-fields/config'} for configurations
|
package/dist/index.js
CHANGED
|
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
17
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.disableCustomFields = void 0;
|
|
20
|
+
exports.disableCustomFields = exports.CustomFieldDefinitionType = void 0;
|
|
21
21
|
const models_1 = require("./models");
|
|
22
22
|
const api_1 = __importDefault(require("./api"));
|
|
23
23
|
const db_1 = __importDefault(require("./utils/db"));
|
|
@@ -25,6 +25,9 @@ const logger_1 = __importDefault(require("./utils/logger"));
|
|
|
25
25
|
const init_1 = require("./utils/init");
|
|
26
26
|
__exportStar(require("./utils/validations/custom-fields"), exports);
|
|
27
27
|
__exportStar(require("./utils/constants"), exports);
|
|
28
|
+
__exportStar(require("./utils/helpers"), exports);
|
|
29
|
+
var type_1 = require("./utils/validations/type");
|
|
30
|
+
Object.defineProperty(exports, "CustomFieldDefinitionType", { enumerable: true, get: function () { return type_1.CustomFieldDefinitionType; } });
|
|
28
31
|
/**
|
|
29
32
|
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
30
33
|
* @see {@link 'custom-fields/config'} for configurations
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
interface CustomFieldsSearchTestFlowInput {
|
|
2
|
+
fieldType: string;
|
|
3
|
+
fieldValue: string | number;
|
|
4
|
+
searchTerm: string;
|
|
5
|
+
expectedNumberOfQueryResults: number;
|
|
6
|
+
}
|
|
7
|
+
declare const customFieldsSearchTestFlow: ({ fieldType, fieldValue, searchTerm, expectedNumberOfQueryResults, }: CustomFieldsSearchTestFlowInput) => Promise<void>;
|
|
8
|
+
export default customFieldsSearchTestFlow;
|
|
@@ -0,0 +1,44 @@
|
|
|
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 index_1 = require("../../../index");
|
|
27
|
+
const DefinitionRepo = __importStar(require("../../../repository/definition"));
|
|
28
|
+
const definition_mock_1 = require("../../mocks/definition.mock");
|
|
29
|
+
const testModel_1 = require("../../mocks/testModel");
|
|
30
|
+
const models_1 = require("../../../models");
|
|
31
|
+
const customFieldsSearchTestFlow = async ({ fieldType, fieldValue, searchTerm, expectedNumberOfQueryResults, }) => {
|
|
32
|
+
const definition = (0, definition_mock_1.createDefinition)({ fieldType, name: 'coolDefinition' });
|
|
33
|
+
await DefinitionRepo.create({ ...definition });
|
|
34
|
+
const [testModel1] = await (0, testModel_1.createTestModels)(definition.entityId, 2);
|
|
35
|
+
await testModel1.update({ customFields: { [definition.name]: fieldValue } });
|
|
36
|
+
const models = await models_1.TestModel.findAndCountAll({
|
|
37
|
+
...(0, index_1.generateCustomFieldSearchQueryPayload)(searchTerm, models_1.TestModel, definition.entityId),
|
|
38
|
+
});
|
|
39
|
+
expect(models.count).toBe(expectedNumberOfQueryResults);
|
|
40
|
+
if (expectedNumberOfQueryResults > 0) {
|
|
41
|
+
expect(models.rows[0].dataValues.id).toBe(testModel1.id);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
exports.default = customFieldsSearchTestFlow;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { WhereOptions, BindOrReplacements } from 'sequelize';
|
|
2
|
+
import { ModelStatic } from 'sequelize-typescript';
|
|
3
|
+
import { CustomFieldDefinitionType } from '../validations/type';
|
|
4
|
+
/**
|
|
5
|
+
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
6
|
+
*
|
|
7
|
+
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
8
|
+
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
9
|
+
* The WHERE clause and replacements are designed to be added to the main query.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} searchTerm - The term to search for within custom fields.
|
|
12
|
+
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
13
|
+
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
14
|
+
* @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
|
|
15
|
+
* to exclude from the search
|
|
16
|
+
*
|
|
17
|
+
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
18
|
+
* for Sequelize.
|
|
19
|
+
*/
|
|
20
|
+
interface CustomFieldsSearchPayload {
|
|
21
|
+
where: WhereOptions;
|
|
22
|
+
replacements: BindOrReplacements;
|
|
23
|
+
}
|
|
24
|
+
export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, customFieldsTypesToExclude?: CustomFieldDefinitionType[]) => CustomFieldsSearchPayload;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCustomFieldSearchQueryPayload = void 0;
|
|
4
|
+
/* eslint-disable import/prefer-default-export */
|
|
5
|
+
const sequelize_1 = require("sequelize");
|
|
6
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
7
|
+
const type_1 = require("../validations/type");
|
|
8
|
+
const generateCustomFieldSearchQueryPayload = (searchTerm, model, entityId, customFieldsTypesToExclude = [
|
|
9
|
+
type_1.CustomFieldDefinitionType.DATETIME,
|
|
10
|
+
type_1.CustomFieldDefinitionType.DATE,
|
|
11
|
+
]) => {
|
|
12
|
+
const excludedTypesString = customFieldsTypesToExclude.map((type) => `'${type}'`).join(',');
|
|
13
|
+
const subQuery = 'EXISTS ('
|
|
14
|
+
+ ' SELECT 1'
|
|
15
|
+
+ ' FROM "custom_field_values" AS "cv"'
|
|
16
|
+
+ ' INNER JOIN custom_field_definitions AS cd '
|
|
17
|
+
+ ` ON cd.entity_id = '${entityId}'`
|
|
18
|
+
+ ' AND cv.custom_field_definition_id = cd.id'
|
|
19
|
+
+ ` AND cd.model_type = '${model.name}'`
|
|
20
|
+
+ ` ${excludedTypesString ? `AND cd.field_type NOT IN (${excludedTypesString})` : ''}`
|
|
21
|
+
+ ' WHERE'
|
|
22
|
+
+ ' "cv"."deleted_at" IS NULL'
|
|
23
|
+
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
24
|
+
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
25
|
+
return {
|
|
26
|
+
where: {
|
|
27
|
+
[sequelize_1.Op.or]: [
|
|
28
|
+
sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
replacements: { searchTerm: `%${searchTerm}%` },
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -15,6 +15,9 @@ export * from './utils/validations/custom-fields';
|
|
|
15
15
|
|
|
16
16
|
export * from './utils/constants';
|
|
17
17
|
|
|
18
|
+
export * from './utils/helpers';
|
|
19
|
+
|
|
20
|
+
export { CustomFieldDefinitionType } from './utils/validations/type';
|
|
18
21
|
/**
|
|
19
22
|
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
20
23
|
* @see {@link 'custom-fields/config'} for configurations
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { generateCustomFieldSearchQueryPayload } from '../../../index';
|
|
2
|
+
import * as DefinitionRepo from '../../../repository/definition';
|
|
3
|
+
import { createDefinition } from '../../mocks/definition.mock';
|
|
4
|
+
import { createTestModels } from '../../mocks/testModel';
|
|
5
|
+
import { TestModel } from '../../../models';
|
|
6
|
+
|
|
7
|
+
interface CustomFieldsSearchTestFlowInput {
|
|
8
|
+
fieldType: string;
|
|
9
|
+
fieldValue: string | number;
|
|
10
|
+
searchTerm: string;
|
|
11
|
+
expectedNumberOfQueryResults: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const customFieldsSearchTestFlow = async ({
|
|
15
|
+
fieldType,
|
|
16
|
+
fieldValue,
|
|
17
|
+
searchTerm,
|
|
18
|
+
expectedNumberOfQueryResults,
|
|
19
|
+
}: CustomFieldsSearchTestFlowInput) : Promise<void> => {
|
|
20
|
+
const definition = createDefinition({ fieldType, name: 'coolDefinition' });
|
|
21
|
+
await DefinitionRepo.create({ ...definition });
|
|
22
|
+
const [testModel1] = await createTestModels(definition.entityId, 2);
|
|
23
|
+
await testModel1.update({ customFields: { [definition.name]: fieldValue } });
|
|
24
|
+
const models = await TestModel.findAndCountAll(
|
|
25
|
+
{
|
|
26
|
+
...generateCustomFieldSearchQueryPayload(
|
|
27
|
+
searchTerm,
|
|
28
|
+
TestModel,
|
|
29
|
+
definition.entityId,
|
|
30
|
+
),
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
expect(models.count).toBe(expectedNumberOfQueryResults);
|
|
34
|
+
if (expectedNumberOfQueryResults > 0) {
|
|
35
|
+
expect(models.rows[0].dataValues.id).toBe(testModel1.id);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default customFieldsSearchTestFlow;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
import { WhereOptions, Op, BindOrReplacements } from 'sequelize';
|
|
3
|
+
import { ModelStatic, Sequelize } from 'sequelize-typescript';
|
|
4
|
+
import { CustomFieldDefinitionType } from '../validations/type';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
8
|
+
*
|
|
9
|
+
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
10
|
+
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
11
|
+
* The WHERE clause and replacements are designed to be added to the main query.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} searchTerm - The term to search for within custom fields.
|
|
14
|
+
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
15
|
+
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
16
|
+
* @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
|
|
17
|
+
* to exclude from the search
|
|
18
|
+
*
|
|
19
|
+
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
20
|
+
* for Sequelize.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
interface CustomFieldsSearchPayload {
|
|
24
|
+
where: WhereOptions;
|
|
25
|
+
replacements: BindOrReplacements;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const generateCustomFieldSearchQueryPayload = (
|
|
29
|
+
searchTerm: string,
|
|
30
|
+
model: ModelStatic,
|
|
31
|
+
entityId : string,
|
|
32
|
+
customFieldsTypesToExclude : CustomFieldDefinitionType[] = [
|
|
33
|
+
CustomFieldDefinitionType.DATETIME,
|
|
34
|
+
CustomFieldDefinitionType.DATE,
|
|
35
|
+
],
|
|
36
|
+
): CustomFieldsSearchPayload => {
|
|
37
|
+
const excludedTypesString = customFieldsTypesToExclude.map((type) => `'${type}'`).join(',');
|
|
38
|
+
|
|
39
|
+
const subQuery = 'EXISTS ('
|
|
40
|
+
+ ' SELECT 1'
|
|
41
|
+
+ ' FROM "custom_field_values" AS "cv"'
|
|
42
|
+
+ ' INNER JOIN custom_field_definitions AS cd '
|
|
43
|
+
+ ` ON cd.entity_id = '${entityId}'`
|
|
44
|
+
+ ' AND cv.custom_field_definition_id = cd.id'
|
|
45
|
+
+ ` AND cd.model_type = '${model.name}'`
|
|
46
|
+
+ ` ${excludedTypesString ? `AND cd.field_type NOT IN (${excludedTypesString})` : ''}`
|
|
47
|
+
+ ' WHERE'
|
|
48
|
+
+ ' "cv"."deleted_at" IS NULL'
|
|
49
|
+
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
50
|
+
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
where: {
|
|
54
|
+
[Op.or]: [
|
|
55
|
+
Sequelize.where(Sequelize.literal(subQuery), true),
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
replacements: { searchTerm: `%${searchTerm}%` },
|
|
59
|
+
};
|
|
60
|
+
};
|