Skip to content

Instantly share code, notes, and snippets.

@madorb
Last active February 21, 2018 17:34
Show Gist options
  • Save madorb/3d0f25be240826ae54673c12d16127d4 to your computer and use it in GitHub Desktop.
Save madorb/3d0f25be240826ae54673c12d16127d4 to your computer and use it in GitHub Desktop.
// @flow
import { IndexablePage, Pageable, Sort, } from '@panderalabs/koa-pageable';
import type { QueryBuilder } from 'objection';
function getData(pageable: Pageable): IndexablePage<number, Person> {
const pageNumber = pageable.page;
const pageSize = pageable.size;
const sort: Sort = pageable.sort;
const queryBuilder: QueryBuilder = Person.query().where('age', '>=', 21).page(pageNumber, pageSize);
//If there is a sort, add each order element to the query's `orderBy`
if (sort) {
sort.forEach((property, direction) => queryBuilder.orderBy(property, direction));
}
const result = await query.execute();
return new IndexablePage(result.results, result.total, pageable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment