@kne/fastify-task 3.0.0-alpha.13 → 3.0.0-alpha.14

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,6 +4,7 @@ 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']);
8
9
 
9
10
  const cancel = async ({ id, targetId, targetType, type }) => {
@@ -170,6 +171,46 @@ module.exports = fp(async (fastify, options) => {
170
171
  return normalized;
171
172
  };
172
173
 
174
+ const quoteIdentifier = identifier => `"${String(identifier).replaceAll('"', '""')}"`;
175
+
176
+ const getJsonTextExpression = fieldPath => {
177
+ if (typeof Sequelize.literal !== 'function') {
178
+ return null;
179
+ }
180
+
181
+ const [field, key] = String(fieldPath).split('.');
182
+ const column = quoteIdentifier(models.task.rawAttributes?.[field]?.field || field);
183
+ const dialect = models.task.sequelize?.getDialect?.();
184
+
185
+ if (dialect === 'postgres') {
186
+ return Sequelize.literal(`${column}->>'${key}'`);
187
+ }
188
+
189
+ if (dialect === 'sqlite') {
190
+ return Sequelize.literal(`json_extract(${column}, '$.${key}')`);
191
+ }
192
+
193
+ return typeof Sequelize.json === 'function' ? Sequelize.json(fieldPath) : null;
194
+ };
195
+
196
+ const appendJsonFieldLikeFilter = (whereQuery, fieldPath, value) => {
197
+ const keyword = String(value).toLowerCase();
198
+ const condition = {
199
+ [Op.like]: `%${keyword}%`
200
+ };
201
+ const fallbackCondition = {
202
+ [Op.like]: `%${value}%`
203
+ };
204
+ const jsonTextExpression = getJsonTextExpression(fieldPath);
205
+
206
+ if (typeof Sequelize.where === 'function' && typeof Sequelize.fn === 'function' && jsonTextExpression && Op.and) {
207
+ whereQuery[Op.and] = [...(whereQuery[Op.and] || []), Sequelize.where(Sequelize.fn('lower', jsonTextExpression), condition)];
208
+ return;
209
+ }
210
+
211
+ whereQuery[fieldPath] = fallbackCondition;
212
+ };
213
+
173
214
  const list = async query => {
174
215
  const { filter, perPage = 20, currentPage = 1, sort } = normalizeListQuery(query);
175
216
  const whereQuery = {};
@@ -181,7 +222,7 @@ module.exports = fp(async (fastify, options) => {
181
222
  });
182
223
 
183
224
  if (filter && filter.targetName) {
184
- whereQuery['input.name'] = filter.targetName;
225
+ appendJsonFieldLikeFilter(whereQuery, 'input.name', filter.targetName);
185
226
  }
186
227
 
187
228
  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.14",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {