@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.
@@ -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: Number.MAX_SAFE_INTEGER }
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['input.name'] = filter.targetName;
226
+ appendJsonFieldLikeFilter(whereQuery, 'input.name', filter.targetName);
185
227
  }
186
228
 
187
229
  if (filter && filter.createdAt) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "3.0.0-alpha.13",
3
+ "version": "3.0.0-alpha.15",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {