@autofleet/sadot 0.13.2 → 0.13.3-beta-c0a9f0d7.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.
- package/.env +4 -0
- package/dist/scopes/filter.d.ts +1 -2
- package/dist/scopes/filter.js +7 -8
- package/dist/scopes/helpers/filter.helpers.js +9 -11
- package/dist/utils/db/index.js +2 -2
- package/package.json +1 -1
- package/src/scopes/filter.ts +8 -7
- package/src/scopes/helpers/filter.helpers.ts +9 -11
- package/src/utils/db/index.ts +2 -2
package/.env
ADDED
package/dist/scopes/filter.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ type customFieldsFilterScopeParams = {
|
|
|
12
12
|
* @returns A function that takes conditions and returns the Sequelize options object.
|
|
13
13
|
*/
|
|
14
14
|
export declare const customFieldsFilterScope: (name: string, options?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => ({ replacementsMap: replacements, scopeValue: conditions }: customFieldsFilterScopeParams) => CustomFieldFilterOptions;
|
|
15
|
-
export declare const scopeName: "filterByCustomFields";
|
|
16
15
|
export declare const customFieldsSortScope: (name: string, options?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => ({ replacementsMap, scopeValue: sort }: {
|
|
17
16
|
replacementsMap: any;
|
|
18
17
|
scopeValue: any;
|
|
@@ -24,7 +23,7 @@ export declare const customFieldsSortScope: (name: string, options?: Pick<Custom
|
|
|
24
23
|
attributes: {
|
|
25
24
|
include: (string | import("sequelize/types/utils").Literal)[][];
|
|
26
25
|
};
|
|
27
|
-
order:
|
|
26
|
+
order: any[][];
|
|
28
27
|
replacements: any;
|
|
29
28
|
};
|
|
30
29
|
export {};
|
package/dist/scopes/filter.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.customFieldsSortScope = exports.
|
|
3
|
+
exports.customFieldsSortScope = 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
7
|
const common_types_1 = require("@autofleet/common-types");
|
|
8
|
-
const helpers_1 = require("../utils/helpers");
|
|
9
8
|
const filter_helpers_1 = require("./helpers/filter.helpers");
|
|
10
9
|
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
11
10
|
/**
|
|
@@ -46,23 +45,23 @@ const customFieldsFilterScope = (name, options) => ({ replacementsMap: replaceme
|
|
|
46
45
|
};
|
|
47
46
|
};
|
|
48
47
|
exports.customFieldsFilterScope = customFieldsFilterScope;
|
|
49
|
-
exports.scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
50
48
|
const customFieldsSortScope = (name, options) => ({ replacementsMap, scopeValue: sort }) => {
|
|
51
49
|
if (!sort || sort.length === 0) {
|
|
52
50
|
return {};
|
|
53
51
|
}
|
|
54
52
|
const queryType = options?.useCustomFieldsEntries ? filter_helpers_1.SubQueryType.ENTRIES : filter_helpers_1.SubQueryType.VALUES;
|
|
55
|
-
const randomStr = (0, helpers_1.generateRandomString)();
|
|
56
53
|
const includes = Object.entries(sort).map(([key]) => {
|
|
57
54
|
const replacementKey = Object.keys(replacementsMap).find((randomString) => replacementsMap[randomString] === key);
|
|
55
|
+
const alias = `cf_sort_${key}`; // Explicit unique alias per field
|
|
58
56
|
return ([
|
|
59
|
-
sequelize_typescript_1.Sequelize.literal((0, filter_helpers_1.getSortCustomFieldsSubQuery)(queryType, name, replacementKey)),
|
|
60
|
-
|
|
57
|
+
sequelize_typescript_1.Sequelize.literal(`(${(0, filter_helpers_1.getSortCustomFieldsSubQuery)(queryType, name, replacementKey)})`),
|
|
58
|
+
alias,
|
|
61
59
|
]);
|
|
62
60
|
});
|
|
63
|
-
const orders = Object.entries(sort).map(([, sortObject]) => {
|
|
61
|
+
const orders = Object.entries(sort).map(([key, sortObject]) => {
|
|
64
62
|
const direction = typeof sortObject === 'string' ? sortObject : Object.values(sortObject)[0];
|
|
65
|
-
|
|
63
|
+
const alias = `cf_sort_${key}`;
|
|
64
|
+
return [sequelize_typescript_1.Sequelize.literal(`"${alias}"`), direction || 'ASC'];
|
|
66
65
|
});
|
|
67
66
|
return {
|
|
68
67
|
attributes: {
|
|
@@ -117,17 +117,15 @@ const getSortCustomFieldsSubQuery = (queryType, modelType, replacementKey) => {
|
|
|
117
117
|
`;
|
|
118
118
|
case SubQueryType.ENTRIES:
|
|
119
119
|
return `(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
)
|
|
130
|
-
`;
|
|
120
|
+
SELECT customFields.value::numeric -- <-- Add numeric casting if sorting numeric values
|
|
121
|
+
FROM custom_field_entries AS ${exports.CE_TABLE_ALIAS},
|
|
122
|
+
jsonb_each_text(custom_fields) AS customFields
|
|
123
|
+
WHERE
|
|
124
|
+
customFields.key = :${replacementKey}${exports.AND_DELIMITER}
|
|
125
|
+
${exports.CE_TABLE_ALIAS}.model_type = '${modelType}'${exports.AND_DELIMITER}
|
|
126
|
+
${exports.CE_TABLE_ALIAS}.model_id = "${modelType}"."id"
|
|
127
|
+
LIMIT 1
|
|
128
|
+
)`;
|
|
131
129
|
default:
|
|
132
130
|
throw new Error('Invalid query type');
|
|
133
131
|
}
|
package/dist/utils/db/index.js
CHANGED
|
@@ -9,10 +9,10 @@ exports.default = (databaseConfig) => {
|
|
|
9
9
|
const config = databaseConfig[env];
|
|
10
10
|
let sequelize;
|
|
11
11
|
if (config.use_env_variable) {
|
|
12
|
-
sequelize = new sequelize_typescript_1.Sequelize(process.env[config.use_env_variable], config);
|
|
12
|
+
sequelize = new sequelize_typescript_1.Sequelize(process.env[config.use_env_variable], { ...config, logging: console.log });
|
|
13
13
|
}
|
|
14
14
|
else {
|
|
15
|
-
sequelize = new sequelize_typescript_1.Sequelize(config.database, config.username, config.password, config);
|
|
15
|
+
sequelize = new sequelize_typescript_1.Sequelize(config.database, config.username, config.password, { ...config, logging: console.log });
|
|
16
16
|
}
|
|
17
17
|
return sequelize;
|
|
18
18
|
};
|
package/package.json
CHANGED
package/src/scopes/filter.ts
CHANGED
|
@@ -64,8 +64,6 @@ export const customFieldsFilterScope = (
|
|
|
64
64
|
};
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
export const scopeName = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
68
|
-
|
|
69
67
|
export const customFieldsSortScope = (
|
|
70
68
|
name: string,
|
|
71
69
|
options?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>,
|
|
@@ -75,21 +73,24 @@ export const customFieldsSortScope = (
|
|
|
75
73
|
}
|
|
76
74
|
|
|
77
75
|
const queryType = options?.useCustomFieldsEntries ? SubQueryType.ENTRIES : SubQueryType.VALUES;
|
|
78
|
-
|
|
76
|
+
|
|
79
77
|
const includes = Object.entries(sort).map(([key]) => {
|
|
80
78
|
const replacementKey = Object.keys(replacementsMap).find(
|
|
81
79
|
(randomString) => replacementsMap[randomString] === key,
|
|
82
80
|
);
|
|
81
|
+
const alias = `cf_sort_${key}`; // Explicit unique alias per field
|
|
83
82
|
return ([
|
|
84
|
-
Sequelize.literal(getSortCustomFieldsSubQuery(queryType, name, replacementKey)),
|
|
85
|
-
|
|
83
|
+
Sequelize.literal(`(${getSortCustomFieldsSubQuery(queryType, name, replacementKey)})`),
|
|
84
|
+
alias,
|
|
86
85
|
]);
|
|
87
86
|
});
|
|
88
87
|
|
|
89
|
-
const orders = Object.entries(sort).map(([, sortObject]) => {
|
|
88
|
+
const orders = Object.entries(sort).map(([key, sortObject]) => {
|
|
90
89
|
const direction = typeof sortObject === 'string' ? sortObject : Object.values(sortObject)[0];
|
|
91
|
-
|
|
90
|
+
const alias = `cf_sort_${key}`;
|
|
91
|
+
return [Sequelize.literal(`"${alias}"`), direction || 'ASC'];
|
|
92
92
|
});
|
|
93
|
+
|
|
93
94
|
return {
|
|
94
95
|
attributes: {
|
|
95
96
|
include: includes,
|
|
@@ -134,17 +134,15 @@ export const getSortCustomFieldsSubQuery = (queryType: SubQueryType, modelType:
|
|
|
134
134
|
`;
|
|
135
135
|
case SubQueryType.ENTRIES:
|
|
136
136
|
return `(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
)
|
|
147
|
-
`;
|
|
137
|
+
SELECT customFields.value::numeric -- <-- Add numeric casting if sorting numeric values
|
|
138
|
+
FROM custom_field_entries AS ${CE_TABLE_ALIAS},
|
|
139
|
+
jsonb_each_text(custom_fields) AS customFields
|
|
140
|
+
WHERE
|
|
141
|
+
customFields.key = :${replacementKey}${AND_DELIMITER}
|
|
142
|
+
${CE_TABLE_ALIAS}.model_type = '${modelType}'${AND_DELIMITER}
|
|
143
|
+
${CE_TABLE_ALIAS}.model_id = "${modelType}"."id"
|
|
144
|
+
LIMIT 1
|
|
145
|
+
)`;
|
|
148
146
|
default:
|
|
149
147
|
throw new Error('Invalid query type');
|
|
150
148
|
}
|
package/src/utils/db/index.ts
CHANGED
|
@@ -7,9 +7,9 @@ export default (databaseConfig: any): Sequelize => {
|
|
|
7
7
|
const config = databaseConfig[env];
|
|
8
8
|
let sequelize: Sequelize;
|
|
9
9
|
if (config.use_env_variable) {
|
|
10
|
-
sequelize = new Sequelize(process.env[config.use_env_variable], config);
|
|
10
|
+
sequelize = new Sequelize(process.env[config.use_env_variable], { ...config, logging: console.log });
|
|
11
11
|
} else {
|
|
12
|
-
sequelize = new Sequelize(config.database, config.username, config.password, config);
|
|
12
|
+
sequelize = new Sequelize(config.database, config.username, config.password, { ...config, logging: console.log });
|
|
13
13
|
}
|
|
14
14
|
return sequelize;
|
|
15
15
|
};
|