@autofleet/sadot 0.5.3 → 0.5.4-beta.0

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
@@ -10,3 +10,4 @@ export * from './utils/constants';
10
10
  declare const useCustomFields: (app: Application | null, getModel: ModelFetcher, options: CustomFieldOptions) => Promise<Sequelize>;
11
11
  export default useCustomFields;
12
12
  export declare const disableCustomFields: (models: any, getModel: any) => void;
13
+ export declare const getCustomFieldsSearchLiteral: (modelName: string, searchTerm: string) => string;
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.getCustomFieldsSearchLiteral = 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"));
@@ -50,3 +50,5 @@ const disableCustomFields = (models, getModel) => {
50
50
  (0, init_1.removeHooks)(models, getModel);
51
51
  };
52
52
  exports.disableCustomFields = disableCustomFields;
53
+ const getCustomFieldsSearchLiteral = (modelName, searchTerm) => `EXISTS (SELECT 1 FROM "custom_field_values" AS "cv" WHERE "cv"."deleted_at" IS NULL AND "cv"."model_id" = "${modelName}"."id" AND CAST("cv"."value" AS TEXT) ILIKE '%${searchTerm}%')`;
54
+ exports.getCustomFieldsSearchLiteral = getCustomFieldsSearchLiteral;
@@ -0,0 +1,9 @@
1
+ import { WhereOptions } from 'sequelize';
2
+ export type CustomFieldSearchOptions = {
3
+ where?: WhereOptions;
4
+ };
5
+ export declare const customFieldsSearchScope: (name: string) => ({ searchTerm, queryWithSearchTerm }: {
6
+ searchTerm: any;
7
+ queryWithSearchTerm: any;
8
+ }) => CustomFieldSearchOptions;
9
+ export declare const scopeName = "searchByCustomFields";
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scopeName = exports.customFieldsSearchScope = void 0;
4
+ const sequelize_typescript_1 = require("sequelize-typescript");
5
+ const customFieldsSearchScope = (name) => ({ searchTerm, queryWithSearchTerm }) => {
6
+ const subQuery = `${'EXISTS (SELECT 1 '
7
+ + 'FROM "custom_field_values" AS "cv" '
8
+ + 'WHERE '
9
+ + '"cv"."deleted_at" IS NULL AND '
10
+ + `"cv"."model_id" = "${name}"."id" AND `
11
+ + `CAST("cv"."value" AS TEXT) ILIKE '%${searchTerm}%')`}`;
12
+ return {
13
+ where: {
14
+ $or: [
15
+ sequelize_typescript_1.Sequelize.literal(`(${subQuery})`),
16
+ ...queryWithSearchTerm.$and[0].$or,
17
+ ],
18
+ },
19
+ };
20
+ };
21
+ exports.customFieldsSearchScope = customFieldsSearchScope;
22
+ exports.scopeName = 'searchByCustomFields';
@@ -10,6 +10,7 @@ const models_1 = require("../models");
10
10
  const hooks_1 = require("../hooks");
11
11
  const scopes_1 = require("../scopes");
12
12
  const logger_1 = __importDefault(require("./logger"));
13
+ const include_1 = require("../scopes/include");
13
14
  const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
14
15
  const addHooks = (models, getModel) => {
15
16
  models.forEach(async ({ name, scopeAttributes }) => {
@@ -91,6 +92,7 @@ const addScopes = (models, getModel) => {
91
92
  addAssociations(model, name);
92
93
  // Add filter scope
93
94
  model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
95
+ model.addScope('searchByCustomFields', (0, include_1.customFieldsSearchScope)(name));
94
96
  }
95
97
  catch (e) {
96
98
  logger_1.default.error(`Could not add custom fields scopes to model ${name}. `, e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.5.3",
3
+ "version": "0.5.4-beta.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -43,3 +43,8 @@ export default useCustomFields;
43
43
  export const disableCustomFields = (models, getModel): void => {
44
44
  removeHooks(models, getModel);
45
45
  };
46
+
47
+ export const getCustomFieldsSearchLiteral = (
48
+ modelName: string,
49
+ searchTerm: string,
50
+ ) : string => `EXISTS (SELECT 1 FROM "custom_field_values" AS "cv" WHERE "cv"."deleted_at" IS NULL AND "cv"."model_id" = "${modelName}"."id" AND CAST("cv"."value" AS TEXT) ILIKE '%${searchTerm}%')`;
@@ -0,0 +1,30 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import { WhereOptions } from 'sequelize';
3
+ import { Sequelize } from 'sequelize-typescript';
4
+
5
+ export type CustomFieldSearchOptions = {
6
+ where?: WhereOptions;
7
+ }
8
+
9
+ export const customFieldsSearchScope = (
10
+ name: string,
11
+ ) => ({ searchTerm, queryWithSearchTerm }) : CustomFieldSearchOptions => {
12
+ const subQuery = `${
13
+ 'EXISTS (SELECT 1 '
14
+ + 'FROM "custom_field_values" AS "cv" '
15
+ + 'WHERE '
16
+ + '"cv"."deleted_at" IS NULL AND '
17
+ + `"cv"."model_id" = "${name}"."id" AND `
18
+ + `CAST("cv"."value" AS TEXT) ILIKE '%${searchTerm}%')`}`;
19
+
20
+ return {
21
+ where: {
22
+ $or: [
23
+ Sequelize.literal(`(${subQuery})`),
24
+ ...queryWithSearchTerm.$and[0].$or,
25
+ ],
26
+ },
27
+ };
28
+ };
29
+
30
+ export const scopeName = 'searchByCustomFields';
package/src/utils/init.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  import { customFieldsFilterScope } from '../scopes';
16
16
  import logger from './logger';
17
17
  import type { ModelFetcher, ModelOptions } from '../types';
18
+ import { customFieldsSearchScope } from '../scopes/include';
18
19
 
19
20
  const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
20
21
 
@@ -96,6 +97,7 @@ export const addScopes = (models: ModelOptions[], getModel: ModelFetcher): void
96
97
  addAssociations(model, name);
97
98
  // Add filter scope
98
99
  model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, customFieldsFilterScope(name));
100
+ model.addScope('searchByCustomFields', customFieldsSearchScope(name));
99
101
  } catch (e) {
100
102
  logger.error(`Could not add custom fields scopes to model ${name}. `, e);
101
103
  }