@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.
- package/libs/controllers/main.js +4 -0
- package/libs/services/task.js +75 -1
- package/package.json +1 -1
package/libs/controllers/main.js
CHANGED
package/libs/services/task.js
CHANGED
|
@@ -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
|
|
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
|
}
|