@autofleet/sadot 0.5.5-beta.7 → 0.5.5-beta.8
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.
|
@@ -1,10 +1,22 @@
|
|
|
1
|
-
import { WhereOptions } from 'sequelize';
|
|
1
|
+
import { WhereOptions, BindOrReplacements } from 'sequelize';
|
|
2
2
|
import { ModelStatic } from 'sequelize-typescript';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* The WHERE clause enable free term search by custom fields.
|
|
4
|
+
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
6
5
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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.
|
|
9
16
|
*/
|
|
10
|
-
|
|
17
|
+
interface CustomFieldsSearchPayload {
|
|
18
|
+
where: WhereOptions;
|
|
19
|
+
replacements: BindOrReplacements;
|
|
20
|
+
}
|
|
21
|
+
export declare const buildCustomFieldsSearchPayload: (searchTerm: string, model: ModelStatic, entityId: string) => CustomFieldsSearchPayload;
|
|
22
|
+
export {};
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.buildCustomFieldsSearchPayload = 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
|
-
|
|
8
|
-
* This function builds a WHERE clause to be added to the main query.
|
|
9
|
-
* The WHERE clause enable free term search by custom fields.
|
|
10
|
-
*
|
|
11
|
-
* @param {string} name - The model type name to be search for.
|
|
12
|
-
* @returns {WhereOptions} - A where clause to be added to the main query.
|
|
13
|
-
*/
|
|
14
|
-
const buildCustomFieldsSearchWhereClause = (model, entityId) => {
|
|
7
|
+
const buildCustomFieldsSearchPayload = (searchTerm, model, entityId) => {
|
|
15
8
|
const subQuery = 'EXISTS ('
|
|
16
9
|
+ ' SELECT 1'
|
|
17
10
|
+ ' FROM "custom_field_values" AS "cv"'
|
|
@@ -24,9 +17,12 @@ const buildCustomFieldsSearchWhereClause = (model, entityId) => {
|
|
|
24
17
|
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
25
18
|
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
26
19
|
return {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
where: {
|
|
21
|
+
[sequelize_1.Op.or]: [
|
|
22
|
+
sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
replacements: { searchTerm: `%${searchTerm}%` },
|
|
30
26
|
};
|
|
31
27
|
};
|
|
32
|
-
exports.
|
|
28
|
+
exports.buildCustomFieldsSearchPayload = buildCustomFieldsSearchPayload;
|
package/package.json
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { buildCustomFieldsSearchWhereClause } from '../../../index';
|
|
1
|
+
import { generateCustomFieldSearchQueryPayload } from '../../../index';
|
|
3
2
|
import * as DefinitionRepo from '../../../repository/definition';
|
|
4
3
|
import { createDefinition } from '../../mocks/definition.mock';
|
|
5
4
|
import { createTestModels } from '../../mocks/testModel';
|
|
@@ -24,8 +23,11 @@ const customFieldsSearchTestFlow = async ({
|
|
|
24
23
|
await testModel1.update({ customFields: { [definition.name]: fieldValue } });
|
|
25
24
|
const models = await TestModel.findAndCountAll(
|
|
26
25
|
{
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
...generateCustomFieldSearchQueryPayload(
|
|
27
|
+
searchTerm,
|
|
28
|
+
TestModel,
|
|
29
|
+
definition.entityId,
|
|
30
|
+
),
|
|
29
31
|
},
|
|
30
32
|
);
|
|
31
33
|
expect(models.count).toBe(expectedNumberOfQueryResults);
|
|
@@ -1,19 +1,32 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import { WhereOptions, Op } from 'sequelize';
|
|
2
|
+
import { WhereOptions, Op, BindOrReplacements } from 'sequelize';
|
|
3
3
|
import { ModelStatic, Sequelize } from 'sequelize-typescript';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* The WHERE clause enable free term search by custom fields.
|
|
6
|
+
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
9
|
+
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
10
|
+
* The WHERE clause and replacements are designed to be added to the main query.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} searchTerm - The term to search for within custom fields.
|
|
13
|
+
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
14
|
+
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
15
|
+
*
|
|
16
|
+
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
17
|
+
* for Sequelize.
|
|
11
18
|
*/
|
|
12
19
|
|
|
13
|
-
|
|
20
|
+
interface CustomFieldsSearchPayload {
|
|
21
|
+
where: WhereOptions;
|
|
22
|
+
replacements: BindOrReplacements;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const generateCustomFieldSearchQueryPayload = (
|
|
26
|
+
searchTerm: string,
|
|
14
27
|
model: ModelStatic,
|
|
15
28
|
entityId : string,
|
|
16
|
-
):
|
|
29
|
+
): CustomFieldsSearchPayload => {
|
|
17
30
|
const subQuery = 'EXISTS ('
|
|
18
31
|
+ ' SELECT 1'
|
|
19
32
|
+ ' FROM "custom_field_values" AS "cv"'
|
|
@@ -25,9 +38,13 @@ export const buildCustomFieldsSearchWhereClause = (
|
|
|
25
38
|
+ ' "cv"."deleted_at" IS NULL'
|
|
26
39
|
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
27
40
|
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
41
|
+
|
|
28
42
|
return {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
43
|
+
where: {
|
|
44
|
+
[Op.or]: [
|
|
45
|
+
Sequelize.where(Sequelize.literal(subQuery), true),
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
replacements: { searchTerm: `%${searchTerm}%` },
|
|
32
49
|
};
|
|
33
50
|
};
|