mirror of
https://github.com/directus/directus.git
synced 2026-01-25 22:18:25 -05:00
Merge pull request #10 from directus/limit-offset-page
Add limit/offset
This commit is contained in:
@@ -23,6 +23,14 @@ const sanitizeQuery: RequestHandler = (req, res, next) => {
|
||||
query.filter = sanitizeFilter(req.query.filter);
|
||||
}
|
||||
|
||||
if (req.query.limit) {
|
||||
query.limit = sanitizeLimit(req.query.limit);
|
||||
}
|
||||
|
||||
if (req.query.offset) {
|
||||
query.offset = sanitizeOffset(req.query.offset);
|
||||
}
|
||||
|
||||
res.locals.query = query;
|
||||
return next();
|
||||
};
|
||||
@@ -63,3 +71,11 @@ function sanitizeFilter(rawFilter: any) {
|
||||
|
||||
return filters;
|
||||
}
|
||||
|
||||
function sanitizeLimit(rawLimit: any) {
|
||||
return Number(rawLimit);
|
||||
}
|
||||
|
||||
function sanitizeOffset(rawOffset: any) {
|
||||
return Number(rawOffset);
|
||||
}
|
||||
|
||||
@@ -36,42 +36,20 @@ export const readItems = async (collection: string, query: Query = {}) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (query.limit) {
|
||||
dbQuery.limit(query.limit);
|
||||
}
|
||||
|
||||
if (query.offset) {
|
||||
dbQuery.offset(query.offset);
|
||||
}
|
||||
|
||||
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];
|
||||
|
||||
@@ -2,6 +2,9 @@ export type Query = {
|
||||
fields?: string[];
|
||||
sort?: Sort[];
|
||||
filter?: Filter[];
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
page?: number;
|
||||
};
|
||||
|
||||
export type Sort = {
|
||||
|
||||
Reference in New Issue
Block a user