@autofleet/sadot 0.13.5-beta.0 → 0.13.5-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.
@@ -1,4 +1,4 @@
1
- import { type WhereOptions, type IncludeOptions } from 'sequelize';
1
+ import { type WhereOptions, type BindOrReplacements } from 'sequelize';
2
2
  import { type ModelStatic } from 'sequelize-typescript';
3
3
  import { CustomFieldDefinitionType } from '../constants';
4
4
  /**
@@ -14,11 +14,12 @@ import { CustomFieldDefinitionType } from '../constants';
14
14
  * @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
15
15
  * to exclude from the search
16
16
  *
17
- * @returns {CustomFieldsSearchPayload} - An object containing the INCLUDE clause and WHERE clause to add to query payload
17
+ * @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
18
+ * for Sequelize.
18
19
  */
19
20
  interface CustomFieldsSearchPayload {
20
21
  where: WhereOptions;
21
- include: IncludeOptions[];
22
+ replacements: BindOrReplacements;
22
23
  }
23
24
  export declare const generateRandomString: (length?: number) => string;
24
25
  export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, customFieldsTypesToExclude?: CustomFieldDefinitionType[]) => CustomFieldsSearchPayload;
@@ -6,7 +6,6 @@ const sequelize_1 = require("sequelize");
6
6
  const sequelize_typescript_1 = require("sequelize-typescript");
7
7
  const node_crypto_1 = require("node:crypto");
8
8
  const constants_1 = require("../constants");
9
- const models_1 = require("../../models");
10
9
  const generateRandomString = (length = 5) => {
11
10
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
12
11
  return Array.from({ length }, () => characters.charAt((0, node_crypto_1.randomInt)(characters.length))).join('');
@@ -15,34 +14,27 @@ exports.generateRandomString = generateRandomString;
15
14
  const generateCustomFieldSearchQueryPayload = (searchTerm, model, entityId, customFieldsTypesToExclude = [
16
15
  constants_1.CustomFieldDefinitionType.DATETIME,
17
16
  constants_1.CustomFieldDefinitionType.DATE,
18
- ]) => ({
19
- include: [{
20
- attributes: ['value', 'model_id'],
21
- model: models_1.CustomFieldValue,
22
- as: 'customFieldValue',
23
- required: false,
24
- include: [
25
- {
26
- model: models_1.CustomFieldDefinition,
27
- as: 'customFieldDefinition',
28
- attributes: ['entity_id', 'field_type', 'model_type'],
29
- required: true,
30
- on: {
31
- id: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.col('id'), { [sequelize_1.Op.eq]: sequelize_typescript_1.Sequelize.col('customFieldValue.custom_field_definition_id') }),
32
- entityId: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.col('entity_id'), { [sequelize_1.Op.eq]: `${entityId}` }),
33
- modelType: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.col('model_type'), { [sequelize_1.Op.eq]: `${model.name}` }),
34
- fieldType: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.col('field_type'), { [sequelize_1.Op.notIn]: customFieldsTypesToExclude }),
35
- },
36
- where: {
37
- deleted_at: null,
38
- },
39
- },
17
+ ]) => {
18
+ const excludedTypesString = customFieldsTypesToExclude.map((type) => `'${type}'`).join(',');
19
+ const subQuery = 'EXISTS ('
20
+ + ' SELECT 1'
21
+ + ' FROM "custom_field_values" AS "cv"'
22
+ + ' INNER JOIN custom_field_definitions AS cd '
23
+ + ` ON cd.entity_id = '${entityId}'`
24
+ + ' AND cv.custom_field_definition_id = cd.id'
25
+ + ` AND cd.model_type = '${model.name}'`
26
+ + ` ${excludedTypesString ? `AND cd.field_type NOT IN (${excludedTypesString})` : ''}`
27
+ + ' WHERE'
28
+ + ' "cv"."deleted_at" IS NULL'
29
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
30
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
31
+ return {
32
+ where: {
33
+ [sequelize_1.Op.or]: [
34
+ sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
40
35
  ],
41
- on: {
42
- value: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.cast(sequelize_typescript_1.Sequelize.col('value'), 'text'), { [sequelize_1.Op.iLike]: `%${searchTerm}%` }),
43
- model_id: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.col('model_id'), { [sequelize_1.Op.eq]: sequelize_typescript_1.Sequelize.col(`${model.name}.id`) }),
44
- },
45
- }],
46
- where: sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.col('customFieldValue.model_id'), { [sequelize_1.Op.not]: null }),
47
- });
36
+ },
37
+ replacements: { searchTerm: `%${searchTerm}%` },
38
+ };
39
+ };
48
40
  exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.13.5-beta.0",
3
+ "version": "0.13.5-beta.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -60,7 +60,7 @@
60
60
  "typescript": "^5.3.3"
61
61
  },
62
62
  "peerDependencies": {
63
- "@autofleet/errors": "^3",
63
+ "@autofleet/errors": "^3.0.0-0",
64
64
  "@autofleet/logger": "^4",
65
65
  "@autofleet/node-common": "^4",
66
66
  "@autofleet/sheilta": "^2",
@@ -1,11 +1,8 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
- import {
3
- type WhereOptions, Op, type IncludeOptions,
4
- } from 'sequelize';
2
+ import { type WhereOptions, Op, type BindOrReplacements } from 'sequelize';
5
3
  import { type ModelStatic, Sequelize } from 'sequelize-typescript';
6
4
  import { randomInt } from 'node:crypto';
7
5
  import { CustomFieldDefinitionType } from '../constants';
8
- import { CustomFieldDefinition, CustomFieldValue } from '../../models';
9
6
 
10
7
  /**
11
8
  * Builds a WHERE clause and replacements for free-text search by custom fields.
@@ -20,12 +17,13 @@ import { CustomFieldDefinition, CustomFieldValue } from '../../models';
20
17
  * @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
21
18
  * to exclude from the search
22
19
  *
23
- * @returns {CustomFieldsSearchPayload} - An object containing the INCLUDE clause and WHERE clause to add to query payload
20
+ * @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
21
+ * for Sequelize.
24
22
  */
25
23
 
26
24
  interface CustomFieldsSearchPayload {
27
25
  where: WhereOptions;
28
- include: IncludeOptions[];
26
+ replacements: BindOrReplacements;
29
27
  }
30
28
 
31
29
  export const generateRandomString = (length = 5): string => {
@@ -41,33 +39,28 @@ export const generateCustomFieldSearchQueryPayload = (
41
39
  CustomFieldDefinitionType.DATETIME,
42
40
  CustomFieldDefinitionType.DATE,
43
41
  ],
44
- ): CustomFieldsSearchPayload => ({
45
- include: [{
46
- attributes: ['value', 'model_id'],
47
- model: CustomFieldValue,
48
- as: 'customFieldValue',
49
- required: false,
50
- include: [
51
- {
52
- model: CustomFieldDefinition,
53
- as: 'customFieldDefinition',
54
- attributes: ['entity_id', 'field_type', 'model_type'],
55
- required: true,
56
- on: {
57
- id: Sequelize.where(Sequelize.col('id'), { [Op.eq]: Sequelize.col('customFieldValue.custom_field_definition_id') }),
58
- entityId: Sequelize.where(Sequelize.col('entity_id'), { [Op.eq]: `${entityId}` }),
59
- modelType: Sequelize.where(Sequelize.col('model_type'), { [Op.eq]: `${model.name}` }),
60
- fieldType: Sequelize.where(Sequelize.col('field_type'), { [Op.notIn]: customFieldsTypesToExclude }),
61
- },
62
- where: {
63
- deleted_at: null,
64
- },
65
- },
66
- ],
67
- on: {
68
- value: Sequelize.where(Sequelize.cast(Sequelize.col('value'), 'text'), { [Op.iLike]: `%${searchTerm}%` }),
69
- model_id: Sequelize.where(Sequelize.col('model_id'), { [Op.eq]: Sequelize.col(`${model.name}.id`) }),
42
+ ): CustomFieldsSearchPayload => {
43
+ const excludedTypesString = customFieldsTypesToExclude.map((type) => `'${type}'`).join(',');
44
+
45
+ const subQuery = 'EXISTS ('
46
+ + ' SELECT 1'
47
+ + ' FROM "custom_field_values" AS "cv"'
48
+ + ' INNER JOIN custom_field_definitions AS cd '
49
+ + ` ON cd.entity_id = '${entityId}'`
50
+ + ' AND cv.custom_field_definition_id = cd.id'
51
+ + ` AND cd.model_type = '${model.name}'`
52
+ + ` ${excludedTypesString ? `AND cd.field_type NOT IN (${excludedTypesString})` : ''}`
53
+ + ' WHERE'
54
+ + ' "cv"."deleted_at" IS NULL'
55
+ + ` AND "cv"."model_id" = "${model.name}"."id"`
56
+ + ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
57
+
58
+ return {
59
+ where: {
60
+ [Op.or]: [
61
+ Sequelize.where(Sequelize.literal(subQuery), true),
62
+ ],
70
63
  },
71
- }],
72
- where: Sequelize.where(Sequelize.col('customFieldValue.model_id'), { [Op.not]: null }),
73
- });
64
+ replacements: { searchTerm: `%${searchTerm}%` },
65
+ };
66
+ };