mirror of
https://github.com/directus/directus.git
synced 2026-01-29 13:38:05 -05:00
Add support for search query param
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { AST, NestedCollectionAST } from '../types/ast';
|
||||
import { uniq } from 'lodash';
|
||||
import database from './index';
|
||||
import database, { schemaInspector } from './index';
|
||||
import { Query } from '../types/query';
|
||||
|
||||
export default async function runAST(ast: AST, query = ast.query) {
|
||||
@@ -69,6 +69,24 @@ export default async function runAST(ast: AST, query = ast.query) {
|
||||
dbQuery.limit(1).first();
|
||||
}
|
||||
|
||||
if (query.search && ast.type === 'collection') {
|
||||
const columns = await schemaInspector.columnInfo(ast.name);
|
||||
|
||||
columns
|
||||
/** @todo Check if this scales between SQL vendors */
|
||||
.filter(
|
||||
(column) =>
|
||||
column.type.toLowerCase().includes('text') ||
|
||||
column.type.toLowerCase().includes('char')
|
||||
)
|
||||
.forEach((column) => {
|
||||
dbQuery.orWhereRaw(
|
||||
`LOWER(${column.name}) LIKE '%' || LOWER(?) || '%'`,
|
||||
query.search
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
let results = await dbQuery;
|
||||
|
||||
for (const batch of nestedCollections) {
|
||||
|
||||
@@ -51,6 +51,10 @@ const sanitizeQuery: RequestHandler = (req, res, next) => {
|
||||
query.meta = sanitizeMeta(req.query.meta);
|
||||
}
|
||||
|
||||
if (req.query.search && typeof req.query.search === 'string') {
|
||||
query.search = req.query.search;
|
||||
}
|
||||
|
||||
req.sanitizedQuery = query;
|
||||
return next();
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@ export type Query = {
|
||||
page?: number;
|
||||
single?: boolean;
|
||||
meta?: Meta[];
|
||||
search?: string;
|
||||
};
|
||||
|
||||
export type Sort = {
|
||||
|
||||
Reference in New Issue
Block a user