mirror of
https://github.com/directus/directus.git
synced 2026-01-26 00:38:34 -05:00
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { RequestHandler } from 'express';
|
||||
import { Query, Sort } from '../types/query';
|
||||
import { Query, Sort, Filter, FilterOperator } from '../types/query';
|
||||
|
||||
const sanitizeQuery: RequestHandler = (req, res, next) => {
|
||||
if (!req.query) return;
|
||||
@@ -19,6 +19,10 @@ const sanitizeQuery: RequestHandler = (req, res, next) => {
|
||||
query.sort = sanitizeSort(req.query.sort);
|
||||
}
|
||||
|
||||
if (req.query.filter) {
|
||||
query.filter = sanitizeFilter(req.query.filter);
|
||||
}
|
||||
|
||||
res.locals.query = query;
|
||||
return next();
|
||||
};
|
||||
@@ -46,3 +50,16 @@ function sanitizeSort(rawSort: any) {
|
||||
return { column, order } as Sort;
|
||||
});
|
||||
}
|
||||
|
||||
function sanitizeFilter(rawFilter: any) {
|
||||
const filters: Filter[] = [];
|
||||
|
||||
Object.keys(rawFilter).forEach((column) => {
|
||||
Object.keys(rawFilter[column]).forEach((operator: FilterOperator) => {
|
||||
const value = rawFilter[column][operator];
|
||||
filters.push({ column, operator, value });
|
||||
});
|
||||
});
|
||||
|
||||
return filters;
|
||||
}
|
||||
|
||||
@@ -16,16 +16,62 @@ export const readItems = async (collection: string, query: Query = {}) => {
|
||||
dbQuery.orderBy(query.sort);
|
||||
}
|
||||
|
||||
if (query.filter) {
|
||||
query.filter.forEach((filter) => {
|
||||
if (filter.operator === 'eq') {
|
||||
dbQuery.where({ [filter.column]: filter.value });
|
||||
}
|
||||
|
||||
if (filter.operator === 'neq') {
|
||||
dbQuery.whereNot({ [filter.column]: filter.value });
|
||||
}
|
||||
|
||||
if (filter.operator === 'null') {
|
||||
dbQuery.whereNull(filter.column);
|
||||
}
|
||||
|
||||
if (filter.operator === 'nnull') {
|
||||
dbQuery.whereNotNull(filter.column);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return await dbQuery;
|
||||
};
|
||||
|
||||
export const readItem = async (collection: string, pk: number | string, query: Query = {}) => {
|
||||
const dbQuery = database.select('*').from(collection).where({ id: pk });
|
||||
|
||||
/**
|
||||
* @TODO
|
||||
* Merge query building between items / item. It should be the same for both, with the exception
|
||||
* of limit / page etc
|
||||
*/
|
||||
|
||||
if (query.sort) {
|
||||
dbQuery.orderBy(query.sort);
|
||||
}
|
||||
|
||||
if (query.filter) {
|
||||
query.filter.forEach((filter) => {
|
||||
if (filter.operator === 'eq') {
|
||||
dbQuery.where({ [filter.column]: filter.value });
|
||||
}
|
||||
|
||||
if (filter.operator === 'neq') {
|
||||
dbQuery.whereNot({ [filter.column]: filter.value });
|
||||
}
|
||||
|
||||
if (filter.operator === 'null') {
|
||||
dbQuery.whereNull(filter.column);
|
||||
}
|
||||
|
||||
if (filter.operator === 'nnull') {
|
||||
dbQuery.whereNotNull(filter.column);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const records = await dbQuery;
|
||||
|
||||
return records[0];
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
export type Query = {
|
||||
fields?: string[];
|
||||
sort?: Sort[];
|
||||
filter?: Filter[];
|
||||
};
|
||||
|
||||
export type Sort = {
|
||||
column: string;
|
||||
order: 'asc' | 'desc';
|
||||
};
|
||||
|
||||
export type Filter = {
|
||||
column: string;
|
||||
operator: FilterOperator;
|
||||
value: null | string | number;
|
||||
};
|
||||
|
||||
export type FilterOperator = 'eq' | 'neq' | 'in' | 'nin' | 'null' | 'nnull';
|
||||
|
||||
Reference in New Issue
Block a user