@autofleet/sadot 0.5.5-beta.18 → 0.5.5-beta.2
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/utils/helpers/index.d.ts +2 -22
- package/dist/utils/helpers/index.js +9 -24
- package/package.json +1 -1
- package/src/utils/helpers/index.ts +10 -51
- package/dist/tests/functional/searching/index.d.ts +0 -8
- package/dist/tests/functional/searching/index.js +0 -44
- package/src/tests/functional/searching/index.ts +0 -40
|
@@ -1,22 +1,2 @@
|
|
|
1
|
-
import { WhereOptions
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
5
|
-
*
|
|
6
|
-
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
7
|
-
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
8
|
-
* The WHERE clause and replacements are designed to be added to the main query.
|
|
9
|
-
*
|
|
10
|
-
* @param {string} searchTerm - The term to search for within custom fields.
|
|
11
|
-
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
12
|
-
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
13
|
-
*
|
|
14
|
-
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
15
|
-
* for Sequelize.
|
|
16
|
-
*/
|
|
17
|
-
interface CustomFieldsSearchPayload {
|
|
18
|
-
where: WhereOptions;
|
|
19
|
-
replacements: BindOrReplacements;
|
|
20
|
-
}
|
|
21
|
-
export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, excludedCustomFieldsTypes?: SequelizeDataTypes[]) => CustomFieldsSearchPayload;
|
|
22
|
-
export {};
|
|
1
|
+
import { WhereOptions } from 'sequelize';
|
|
2
|
+
export declare const buildCustomFieldsSearchWhereClause: (searchTerm: string) => WhereOptions;
|
|
@@ -1,29 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.buildCustomFieldsSearchWhereClause = void 0;
|
|
4
4
|
/* eslint-disable import/prefer-default-export */
|
|
5
5
|
const sequelize_1 = require("sequelize");
|
|
6
6
|
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
+ ' WHERE'
|
|
16
|
-
+ ' "cv"."deleted_at" IS NULL'
|
|
17
|
-
+ ' AND "cd"."field_type" NOT IN (\'datetime\')'
|
|
18
|
-
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
19
|
-
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
20
|
-
return {
|
|
21
|
-
where: {
|
|
22
|
-
[sequelize_1.Op.or]: [
|
|
23
|
-
sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
|
|
24
|
-
],
|
|
25
|
-
},
|
|
26
|
-
replacements: { searchTerm: `%${searchTerm}%` },
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
|
|
7
|
+
const buildCustomFieldsSearchWhereClause = (searchTerm) => ({
|
|
8
|
+
where: {
|
|
9
|
+
[sequelize_1.Op.or]: [
|
|
10
|
+
sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.cast(sequelize_typescript_1.Sequelize.col('customFieldValue.value'), 'text'), { [sequelize_1.Op.iLike]: `%${searchTerm}%` }),
|
|
11
|
+
],
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
exports.buildCustomFieldsSearchWhereClause = buildCustomFieldsSearchWhereClause;
|
package/package.json
CHANGED
|
@@ -1,54 +1,13 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
} from 'sequelize';
|
|
5
|
-
import { DataType, ModelStatic, Sequelize } from 'sequelize-typescript';
|
|
2
|
+
import { WhereOptions, Op } from 'sequelize';
|
|
3
|
+
import { Sequelize } from 'sequelize-typescript';
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
9
|
-
*
|
|
10
|
-
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
11
|
-
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
12
|
-
* The WHERE clause and replacements are designed to be added to the main query.
|
|
13
|
-
*
|
|
14
|
-
* @param {string} searchTerm - The term to search for within custom fields.
|
|
15
|
-
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
16
|
-
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
17
|
-
*
|
|
18
|
-
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
19
|
-
* for Sequelize.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
interface CustomFieldsSearchPayload {
|
|
23
|
-
where: WhereOptions;
|
|
24
|
-
replacements: BindOrReplacements;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const generateCustomFieldSearchQueryPayload = (
|
|
5
|
+
export const buildCustomFieldsSearchWhereClause = (
|
|
28
6
|
searchTerm: string,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
+ ' INNER JOIN custom_field_definitions AS cd '
|
|
37
|
-
+ ` ON cd.entity_id = '${entityId}'`
|
|
38
|
-
+ ' AND cv.custom_field_definition_id = cd.id '
|
|
39
|
-
+ ` AND cd.model_type = '${model.name}' `
|
|
40
|
-
+ ' WHERE'
|
|
41
|
-
+ ' "cv"."deleted_at" IS NULL'
|
|
42
|
-
+ ' AND "cd"."field_type" NOT IN (\'datetime\')'
|
|
43
|
-
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
44
|
-
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
where: {
|
|
48
|
-
[Op.or]: [
|
|
49
|
-
Sequelize.where(Sequelize.literal(subQuery), true),
|
|
50
|
-
],
|
|
51
|
-
},
|
|
52
|
-
replacements: { searchTerm: `%${searchTerm}%` },
|
|
53
|
-
};
|
|
54
|
-
};
|
|
7
|
+
) : WhereOptions => ({
|
|
8
|
+
where: {
|
|
9
|
+
[Op.or]: [
|
|
10
|
+
Sequelize.where(Sequelize.cast(Sequelize.col('customFieldValue.value'), 'text'), { [Op.iLike]: `%${searchTerm}%` }),
|
|
11
|
+
],
|
|
12
|
+
},
|
|
13
|
+
});
|
|
@@ -1,8 +0,0 @@
|
|
|
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;
|
|
@@ -1,44 +0,0 @@
|
|
|
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;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { DataType } from 'sequelize-typescript';
|
|
2
|
-
import { generateCustomFieldSearchQueryPayload } from '../../../index';
|
|
3
|
-
import * as DefinitionRepo from '../../../repository/definition';
|
|
4
|
-
import { createDefinition } from '../../mocks/definition.mock';
|
|
5
|
-
import { createTestModels } from '../../mocks/testModel';
|
|
6
|
-
import { TestModel } from '../../../models';
|
|
7
|
-
|
|
8
|
-
interface CustomFieldsSearchTestFlowInput {
|
|
9
|
-
fieldType: string;
|
|
10
|
-
fieldValue: string | number;
|
|
11
|
-
searchTerm: string;
|
|
12
|
-
expectedNumberOfQueryResults: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const customFieldsSearchTestFlow = async ({
|
|
16
|
-
fieldType,
|
|
17
|
-
fieldValue,
|
|
18
|
-
searchTerm,
|
|
19
|
-
expectedNumberOfQueryResults,
|
|
20
|
-
}: CustomFieldsSearchTestFlowInput) : Promise<void> => {
|
|
21
|
-
const definition = createDefinition({ fieldType, name: 'coolDefinition' });
|
|
22
|
-
await DefinitionRepo.create({ ...definition });
|
|
23
|
-
const [testModel1] = await createTestModels(definition.entityId, 2);
|
|
24
|
-
await testModel1.update({ customFields: { [definition.name]: fieldValue } });
|
|
25
|
-
const models = await TestModel.findAndCountAll(
|
|
26
|
-
{
|
|
27
|
-
...generateCustomFieldSearchQueryPayload(
|
|
28
|
-
searchTerm,
|
|
29
|
-
TestModel,
|
|
30
|
-
definition.entityId,
|
|
31
|
-
),
|
|
32
|
-
},
|
|
33
|
-
);
|
|
34
|
-
expect(models.count).toBe(expectedNumberOfQueryResults);
|
|
35
|
-
if (expectedNumberOfQueryResults > 0) {
|
|
36
|
-
expect(models.rows[0].dataValues.id).toBe(testModel1.id);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export default customFieldsSearchTestFlow;
|