@autofleet/sadot 0.5.5-beta.28 → 0.5.5-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/index.d.ts CHANGED
@@ -4,7 +4,6 @@ import type { CustomFieldOptions, ModelFetcher } from './types';
4
4
  export * from './utils/validations/custom-fields';
5
5
  export * from './utils/constants';
6
6
  export * from './utils/helpers';
7
- export { CustomFieldDefinitionType } from './utils/validations/type';
8
7
  /**
9
8
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
10
9
  * @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 = exports.CustomFieldDefinitionType = void 0;
20
+ exports.disableCustomFields = 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"));
@@ -26,8 +26,6 @@ const init_1 = require("./utils/init");
26
26
  __exportStar(require("./utils/validations/custom-fields"), exports);
27
27
  __exportStar(require("./utils/constants"), exports);
28
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; } });
31
29
  /**
32
30
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
33
31
  * @see {@link 'custom-fields/config'} for configurations
@@ -23,6 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ const sequelize_1 = require("sequelize");
26
27
  const index_1 = require("../../../index");
27
28
  const DefinitionRepo = __importStar(require("../../../repository/definition"));
28
29
  const definition_mock_1 = require("../../mocks/definition.mock");
@@ -33,12 +34,13 @@ const customFieldsSearchTestFlow = async ({ fieldType, fieldValue, searchTerm, e
33
34
  await DefinitionRepo.create({ ...definition });
34
35
  const [testModel1] = await (0, testModel_1.createTestModels)(definition.entityId, 2);
35
36
  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),
37
+ const models = await models_1.TestModel.findAll({
38
+ where: { [sequelize_1.Op.or]: [(0, index_1.buildCustomFieldsSearchWhereClause)(searchTerm)] },
39
+ include: { model: models_1.CustomFieldValue, as: 'customFieldValue' },
38
40
  });
39
- expect(models.count).toBe(expectedNumberOfQueryResults);
41
+ expect(models.length).toBe(expectedNumberOfQueryResults);
40
42
  if (expectedNumberOfQueryResults > 0) {
41
- expect(models.rows[0].dataValues.id).toBe(testModel1.id);
43
+ expect(models[0].id).toBe(testModel1.id);
42
44
  }
43
45
  };
44
46
  exports.default = customFieldsSearchTestFlow;
@@ -1,25 +1,2 @@
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 {};
1
+ import { WhereOptions } from 'sequelize';
2
+ export declare const buildCustomFieldsSearchWhereClause: (searchTerm: string) => WhereOptions;
@@ -1,34 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateCustomFieldSearchQueryPayload = void 0;
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 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;
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.5.5-beta.28",
3
+ "version": "0.5.5-beta.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -14,8 +14,6 @@ export * from './utils/validations/custom-fields';
14
14
  export * from './utils/constants';
15
15
 
16
16
  export * from './utils/helpers';
17
-
18
- export { CustomFieldDefinitionType } from './utils/validations/type';
19
17
  /**
20
18
  * Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
21
19
  * @see {@link 'custom-fields/config'} for configurations
@@ -1,8 +1,9 @@
1
- import { generateCustomFieldSearchQueryPayload } from '../../../index';
1
+ import { Op } from 'sequelize';
2
+ import { buildCustomFieldsSearchWhereClause } from '../../../index';
2
3
  import * as DefinitionRepo from '../../../repository/definition';
3
4
  import { createDefinition } from '../../mocks/definition.mock';
4
5
  import { createTestModels } from '../../mocks/testModel';
5
- import { TestModel } from '../../../models';
6
+ import { CustomFieldValue, TestModel } from '../../../models';
6
7
 
7
8
  interface CustomFieldsSearchTestFlowInput {
8
9
  fieldType: string;
@@ -21,18 +22,15 @@ const customFieldsSearchTestFlow = async ({
21
22
  await DefinitionRepo.create({ ...definition });
22
23
  const [testModel1] = await createTestModels(definition.entityId, 2);
23
24
  await testModel1.update({ customFields: { [definition.name]: fieldValue } });
24
- const models = await TestModel.findAndCountAll(
25
+ const models = await TestModel.findAll(
25
26
  {
26
- ...generateCustomFieldSearchQueryPayload(
27
- searchTerm,
28
- TestModel,
29
- definition.entityId,
30
- ),
27
+ where: { [Op.or]: [buildCustomFieldsSearchWhereClause(searchTerm)] },
28
+ include: { model: CustomFieldValue, as: 'customFieldValue' },
31
29
  },
32
30
  );
33
- expect(models.count).toBe(expectedNumberOfQueryResults);
31
+ expect(models.length).toBe(expectedNumberOfQueryResults);
34
32
  if (expectedNumberOfQueryResults > 0) {
35
- expect(models.rows[0].dataValues.id).toBe(testModel1.id);
33
+ expect(models[0].id).toBe(testModel1.id);
36
34
  }
37
35
  };
38
36
 
@@ -1,60 +1,13 @@
1
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';
2
+ import { WhereOptions, Op } from 'sequelize';
3
+ import { Sequelize } from 'sequelize-typescript';
5
4
 
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 = (
5
+ export const buildCustomFieldsSearchWhereClause = (
29
6
  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
- };
7
+ ) : WhereOptions => ({
8
+ where: {
9
+ [Op.or]: [
10
+ Sequelize.where(Sequelize.cast(Sequelize.col('customFieldValue.value'), 'text'), { [Op.iLike]: `%${searchTerm}%` }),
11
+ ],
12
+ },
13
+ });