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