perf(core): Add filtering and pagination to GET /workflows (#6845)
* Initial setup * Specify max paginated items * Simplify * Add tests * Add more tests * Add migrations * Add top-level property * Add field selection * Cleanup * Rename `total` to `count` * More cleanup * Move query logic into `WorkflowRepository` * Create `AbstractRepository` * Cleanup * Fix name * Remove leftover comments * Replace reference * Add default for `rawSkip` * Remove unneeded typing * Switch to `class-validator` * Simplify * Simplify * Type as optional * Make typing more accurate * Fix lint * Use `getOwnPropertyNames` * Use DSL * Set schema at repo level * Cleanup * Remove comment * Refactor repository methods to middleware * Add middleware tests * Remove old test files * Remove generic experiment * Reuse `reportError` * Remove unused type * Cleanup * Improve wording * Reduce diff * Add missing mw * Use `Container.get` * Adjust lint rule * Reorganize into subdir * Remove unused directive * Remove nodes * Silly mistake * Validate take * refactor(core): Adjust index handling in new migrations DSL (no-changelog) (#6876) * refactor(core): Adjust index handling in new migrations DSL (no-changelog) * Account for custom index name * Also for dropping * Fix `select` issue with `relations` * Tighten validation * Ensure `ownerId` is not added when specifying `select`
This commit is contained in:
46
packages/cli/src/middlewares/listQuery/select.ts
Normal file
46
packages/cli/src/middlewares/listQuery/select.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { handleListQueryError } from './error';
|
||||
import { jsonParse } from 'n8n-workflow';
|
||||
import { WorkflowSchema } from './workflow.schema';
|
||||
import * as utils from '@/utils';
|
||||
import type { ListQueryRequest } from '@/requests';
|
||||
import type { RequestHandler } from 'express';
|
||||
import type { Schema } from '@/middlewares/listQuery/schema';
|
||||
|
||||
function toQuerySelect(rawSelect: string, schema: typeof Schema) {
|
||||
const asArr = jsonParse(rawSelect, { errorMessage: 'Failed to parse select JSON' });
|
||||
|
||||
if (!utils.isStringArray(asArr)) {
|
||||
throw new Error('Parsed select is not a string array');
|
||||
}
|
||||
|
||||
return asArr.reduce<Record<string, true>>((acc, field) => {
|
||||
if (!schema.fieldNames.includes(field)) return acc;
|
||||
return (acc[field] = true), acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export const selectListQueryMiddleware: RequestHandler = (req: ListQueryRequest, res, next) => {
|
||||
const { select: rawSelect } = req.query;
|
||||
|
||||
if (!rawSelect) return next();
|
||||
|
||||
let schema;
|
||||
|
||||
if (req.baseUrl.endsWith('workflows')) {
|
||||
schema = WorkflowSchema;
|
||||
} else {
|
||||
return next();
|
||||
}
|
||||
|
||||
try {
|
||||
const select = toQuerySelect(rawSelect, schema);
|
||||
|
||||
if (Object.keys(select).length === 0) return next();
|
||||
|
||||
req.listQueryOptions = { ...req.listQueryOptions, select };
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
handleListQueryError('select', rawSelect, error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user