Last active
February 21, 2018 17:34
-
-
Save madorb/3d0f25be240826ae54673c12d16127d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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