@autofleet/sadot 0.5.2 → 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 +1 -0
- package/dist/index.js +3 -1
- package/dist/scopes/filter.js +3 -2
- package/dist/scopes/include.d.ts +9 -0
- package/dist/scopes/include.js +22 -0
- package/dist/utils/constants/index.d.ts +4 -1
- package/dist/utils/constants/index.js +3 -1
- package/dist/utils/init.js +5 -2
- package/package.json +2 -1
- package/src/index.ts +5 -0
- package/src/scopes/filter.ts +3 -1
- package/src/scopes/include.ts +30 -0
- package/src/utils/constants/index.ts +8 -1
- package/src/utils/init.ts +5 -1
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;
|
package/dist/scopes/filter.js
CHANGED
|
@@ -4,7 +4,8 @@ exports.scopeName = exports.customFieldsFilterScope = 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
|
|
7
|
+
const common_types_1 = require("@autofleet/common-types");
|
|
8
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
8
9
|
/**
|
|
9
10
|
* A Sequelize scope for filtering models by custom fields.
|
|
10
11
|
* This scope builds a WHERE clause to be applied on the main query.
|
|
@@ -51,4 +52,4 @@ const customFieldsFilterScope = (name) => (conditions) => {
|
|
|
51
52
|
};
|
|
52
53
|
};
|
|
53
54
|
exports.customFieldsFilterScope = customFieldsFilterScope;
|
|
54
|
-
exports.scopeName =
|
|
55
|
+
exports.scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
@@ -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';
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
+
declare const CUSTOM_FIELDS_FILTER_SCOPE: string;
|
|
1
2
|
export declare const supportedEntities: string[];
|
|
2
|
-
export
|
|
3
|
+
export {
|
|
4
|
+
/** @deprecated Use the value from `@autofleet/common-types` instead */
|
|
5
|
+
CUSTOM_FIELDS_FILTER_SCOPE, };
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CUSTOM_FIELDS_FILTER_SCOPE = exports.supportedEntities = void 0;
|
|
4
|
+
const common_types_1 = require("@autofleet/common-types");
|
|
5
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
6
|
+
exports.CUSTOM_FIELDS_FILTER_SCOPE = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
4
7
|
// eslint-disable-next-line import/prefer-default-export
|
|
5
8
|
exports.supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
|
|
6
|
-
exports.CUSTOM_FIELDS_FILTER_SCOPE = 'filterByCustomFields';
|
package/dist/utils/init.js
CHANGED
|
@@ -5,11 +5,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.addScopes = exports.removeHooks = exports.addHooks = void 0;
|
|
7
7
|
const sequelize_1 = require("sequelize");
|
|
8
|
+
const common_types_1 = require("@autofleet/common-types");
|
|
8
9
|
const models_1 = require("../models");
|
|
9
10
|
const hooks_1 = require("../hooks");
|
|
10
11
|
const scopes_1 = require("../scopes");
|
|
11
12
|
const logger_1 = __importDefault(require("./logger"));
|
|
12
|
-
const
|
|
13
|
+
const include_1 = require("../scopes/include");
|
|
14
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
13
15
|
const addHooks = (models, getModel) => {
|
|
14
16
|
models.forEach(async ({ name, scopeAttributes }) => {
|
|
15
17
|
try {
|
|
@@ -89,7 +91,8 @@ const addScopes = (models, getModel) => {
|
|
|
89
91
|
// Necessary associations for the filtering scope
|
|
90
92
|
addAssociations(model, name);
|
|
91
93
|
// Add filter scope
|
|
92
|
-
model.addScope(
|
|
94
|
+
model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
|
|
95
|
+
model.addScope('searchByCustomFields', (0, include_1.customFieldsSearchScope)(name));
|
|
93
96
|
}
|
|
94
97
|
catch (e) {
|
|
95
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
|
+
"version": "0.5.4-beta.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"dev": "nodemon"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
+
"@autofleet/common-types": "^1.7.29",
|
|
15
16
|
"@autofleet/errors": "^1.0.10",
|
|
16
17
|
"@autofleet/events": "^2.0.0",
|
|
17
18
|
"@autofleet/logger": "^2.0.5",
|
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}%')`;
|
package/src/scopes/filter.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
2
|
import { Op, WhereOptions } from 'sequelize';
|
|
3
3
|
import { Sequelize } from 'sequelize-typescript';
|
|
4
|
-
import {
|
|
4
|
+
import { customFields } from '@autofleet/common-types';
|
|
5
|
+
|
|
6
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Type representing possible condition values.
|
|
@@ -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';
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
+
import { customFields } from '@autofleet/common-types';
|
|
2
|
+
|
|
3
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
4
|
+
|
|
1
5
|
// eslint-disable-next-line import/prefer-default-export
|
|
2
6
|
export const supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
|
|
3
7
|
|
|
4
|
-
export
|
|
8
|
+
export {
|
|
9
|
+
/** @deprecated Use the value from `@autofleet/common-types` instead */
|
|
10
|
+
CUSTOM_FIELDS_FILTER_SCOPE,
|
|
11
|
+
};
|
package/src/utils/init.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { DataTypes } from 'sequelize';
|
|
2
2
|
import { ModelCtor } from 'sequelize-typescript';
|
|
3
|
+
import { customFields } from '@autofleet/common-types';
|
|
3
4
|
import {
|
|
4
5
|
CustomFieldValue,
|
|
5
6
|
} from '../models';
|
|
@@ -14,7 +15,9 @@ import {
|
|
|
14
15
|
import { customFieldsFilterScope } from '../scopes';
|
|
15
16
|
import logger from './logger';
|
|
16
17
|
import type { ModelFetcher, ModelOptions } from '../types';
|
|
17
|
-
import {
|
|
18
|
+
import { customFieldsSearchScope } from '../scopes/include';
|
|
19
|
+
|
|
20
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
18
21
|
|
|
19
22
|
export const addHooks = (models: ModelOptions[], getModel: ModelFetcher): void => {
|
|
20
23
|
models.forEach(async ({ name, scopeAttributes }) => {
|
|
@@ -94,6 +97,7 @@ export const addScopes = (models: ModelOptions[], getModel: ModelFetcher): void
|
|
|
94
97
|
addAssociations(model, name);
|
|
95
98
|
// Add filter scope
|
|
96
99
|
model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, customFieldsFilterScope(name));
|
|
100
|
+
model.addScope('searchByCustomFields', customFieldsSearchScope(name));
|
|
97
101
|
} catch (e) {
|
|
98
102
|
logger.error(`Could not add custom fields scopes to model ${name}. `, e);
|
|
99
103
|
}
|