Add support for search query param

This commit is contained in:
rijkvanzanten
2020-07-08 13:15:44 -04:00
parent 4d33b9a7ab
commit a60785d563
3 changed files with 24 additions and 1 deletions

View File

@@ -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) {

View File

@@ -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();
};

View File

@@ -9,6 +9,7 @@ export type Query = {
page?: number;
single?: boolean;
meta?: Meta[];
search?: string;
};
export type Sort = {