@kne/fastify-task 3.0.0-alpha.12 → 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.
@@ -94,6 +94,10 @@ module.exports = fp(async (fastify, options) => {
94
94
  targetId: {
95
95
  type: 'string'
96
96
  },
97
+ targetName: {
98
+ type: 'string',
99
+ description: '目标对象名称,对应任务 input.name'
100
+ },
97
101
  type: {
98
102
  type: 'string'
99
103
  },
@@ -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 }) => {
@@ -142,7 +143,76 @@ module.exports = fp(async (fastify, options) => {
142
143
  }
143
144
  };
144
145
 
145
- const list = async ({ filter, perPage = 20, currentPage = 1, sort }) => {
146
+ const normalizeListQuery = (query = {}) => {
147
+ const normalized = Object.assign({}, query);
148
+
149
+ Object.entries(query || {}).forEach(([rawKey, value]) => {
150
+ const match = rawKey.match(/^(filter|sort)\[([^\]]+)\](?:\[([^\]]+)\])?$/);
151
+ if (!match) {
152
+ return;
153
+ }
154
+
155
+ const [, root, key, childKey] = match;
156
+ if (!normalized[root] || typeof normalized[root] !== 'object' || Array.isArray(normalized[root])) {
157
+ normalized[root] = {};
158
+ }
159
+
160
+ if (childKey) {
161
+ if (!normalized[root][key] || typeof normalized[root][key] !== 'object' || Array.isArray(normalized[root][key])) {
162
+ normalized[root][key] = {};
163
+ }
164
+ normalized[root][key][childKey] = value;
165
+ } else {
166
+ normalized[root][key] = value;
167
+ }
168
+ delete normalized[rawKey];
169
+ });
170
+
171
+ return normalized;
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
+
214
+ const list = async query => {
215
+ const { filter, perPage = 20, currentPage = 1, sort } = normalizeListQuery(query);
146
216
  const whereQuery = {};
147
217
 
148
218
  ['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
@@ -151,6 +221,10 @@ module.exports = fp(async (fastify, options) => {
151
221
  }
152
222
  });
153
223
 
224
+ if (filter && filter.targetName) {
225
+ appendJsonFieldLikeFilter(whereQuery, 'input.name', filter.targetName);
226
+ }
227
+
154
228
  if (filter && filter.createdAt) {
155
229
  whereQuery.createdAt = getTimeQuery(filter.createdAt);
156
230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "3.0.0-alpha.12",
3
+ "version": "3.0.0-alpha.14",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {