@kne/fastify-task 3.0.0-alpha.13 → 3.0.0-alpha.15
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/libs/services/task.js +44 -2
- package/package.json +1 -1
package/libs/services/task.js
CHANGED
|
@@ -4,7 +4,9 @@ const getTaskServiceContext = require('../helpers/task-service-context');
|
|
|
4
4
|
module.exports = fp(async (fastify, options) => {
|
|
5
5
|
const context = getTaskServiceContext(fastify, options);
|
|
6
6
|
const { models, Op } = context;
|
|
7
|
+
const Sequelize = fastify.sequelize.Sequelize;
|
|
7
8
|
const SORTABLE_FIELDS = new Set(['id', 'targetId', 'targetType', 'type', 'status', 'runnerType', 'priority', 'createdAt', 'updatedAt', 'startTime', 'startedAt', 'completedAt']);
|
|
9
|
+
const INTEGER_PRIORITY_MAX = 2147483647;
|
|
8
10
|
|
|
9
11
|
const cancel = async ({ id, targetId, targetType, type }) => {
|
|
10
12
|
if (!id && !(targetId && targetType && type)) {
|
|
@@ -73,7 +75,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
73
75
|
await context.updateTaskByStatus({
|
|
74
76
|
task,
|
|
75
77
|
allowedStatuses: ['pending'],
|
|
76
|
-
updateData: { priority:
|
|
78
|
+
updateData: { priority: INTEGER_PRIORITY_MAX }
|
|
77
79
|
});
|
|
78
80
|
const claimedTask = await fastify[options.name].services.claimTask(task);
|
|
79
81
|
if (claimedTask) {
|
|
@@ -170,6 +172,46 @@ module.exports = fp(async (fastify, options) => {
|
|
|
170
172
|
return normalized;
|
|
171
173
|
};
|
|
172
174
|
|
|
175
|
+
const quoteIdentifier = identifier => `"${String(identifier).replaceAll('"', '""')}"`;
|
|
176
|
+
|
|
177
|
+
const getJsonTextExpression = fieldPath => {
|
|
178
|
+
if (typeof Sequelize.literal !== 'function') {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const [field, key] = String(fieldPath).split('.');
|
|
183
|
+
const column = quoteIdentifier(models.task.rawAttributes?.[field]?.field || field);
|
|
184
|
+
const dialect = models.task.sequelize?.getDialect?.();
|
|
185
|
+
|
|
186
|
+
if (dialect === 'postgres') {
|
|
187
|
+
return Sequelize.literal(`${column}->>'${key}'`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (dialect === 'sqlite') {
|
|
191
|
+
return Sequelize.literal(`json_extract(${column}, '$.${key}')`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return typeof Sequelize.json === 'function' ? Sequelize.json(fieldPath) : null;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const appendJsonFieldLikeFilter = (whereQuery, fieldPath, value) => {
|
|
198
|
+
const keyword = String(value).toLowerCase();
|
|
199
|
+
const condition = {
|
|
200
|
+
[Op.like]: `%${keyword}%`
|
|
201
|
+
};
|
|
202
|
+
const fallbackCondition = {
|
|
203
|
+
[Op.like]: `%${value}%`
|
|
204
|
+
};
|
|
205
|
+
const jsonTextExpression = getJsonTextExpression(fieldPath);
|
|
206
|
+
|
|
207
|
+
if (typeof Sequelize.where === 'function' && typeof Sequelize.fn === 'function' && jsonTextExpression && Op.and) {
|
|
208
|
+
whereQuery[Op.and] = [...(whereQuery[Op.and] || []), Sequelize.where(Sequelize.fn('lower', jsonTextExpression), condition)];
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
whereQuery[fieldPath] = fallbackCondition;
|
|
213
|
+
};
|
|
214
|
+
|
|
173
215
|
const list = async query => {
|
|
174
216
|
const { filter, perPage = 20, currentPage = 1, sort } = normalizeListQuery(query);
|
|
175
217
|
const whereQuery = {};
|
|
@@ -181,7 +223,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
181
223
|
});
|
|
182
224
|
|
|
183
225
|
if (filter && filter.targetName) {
|
|
184
|
-
whereQuery
|
|
226
|
+
appendJsonFieldLikeFilter(whereQuery, 'input.name', filter.targetName);
|
|
185
227
|
}
|
|
186
228
|
|
|
187
229
|
if (filter && filter.createdAt) {
|