Functions

paginate

Koa Middleware function that reads pagination parameters from the query string, and populate ctx.state.pageable with a Pageable instance.

paginate(ctx: Context, next: Function): Promise
Parameters
ctx (Context) Context associated with the Koa middleware function
next (Function) Middleware function called after Pageable property is set in state
Returns
Promise:

Classes

Pageable

Represents the configuration for a page of elements. Created by the middleware based on the request query parameters

new Pageable(pageNumber: number, pageSize: number, indexed: boolean, sort: (string? | Array<string>? | Sort))
Parameters
pageNumber (number) The page to be returned
pageSize (number) The number of elements to be returned
indexed (boolean) The format to return the results in. If true and the type of the content being returned supports it (has an id property), the result will contain a (potentially ordered, based on sort ) list of ids and a corresponding map of {id: content item} , else content returned as a simple array. Default value is false.
sort ((string? | Array<string>? | Sort)) Optional. The order to return the results in, ordered list of property, Direction .
Instance Members
page
size
sort
indexed

Sort

Sort options that should be applied to returned data set. Represents an iterable list of ordered properties.

new Sort(orders: Array<Order>)
Parameters
orders (Array<Order>) Array of Order instances
Instance Members
orders
iterator()
forEach(iteratee)
toJSON()

Direction

Enumeration of sort directions

Direction
Static Members
asc
desc

Order

Pairing of a property and a Direction. Represents a single property that should be ordered as part of a Sort

new Order(property: string, direction: DirectionType)
Parameters
property (string) The property to be ordered
direction (DirectionType) The direction of the ordering, defaults to Direction.asc
Instance Members
direction
property

Page

"Base class" for container for content being returned.

new Page(totalElements: number, pageable: Pageable)
Parameters
totalElements (number) The total number of elements in the data set
pageable (Pageable) The Pageable containing the paging information
Instance Members
number
size
numberOfElements
totalElements
totalPages
sort
first
last

ArrayPage

Represents a sublist of a list of objects. Provides details about the total list, including whether there is more data available.

new ArrayPage(content: Array<T>, totalElements: number, pageable: Pageable)

Extends Page

Parameters
content (Array<T>) The content to be returned
totalElements (number) The total number of elements in the data set
pageable (Pageable) The Pageable containing the paging information
Instance Members
content
map(iteratee)

IndexedPage

A page that has its content normalized into an array of ids and a corresponding map of {id : content item }

new IndexedPage(ids: Array<I>, index: {}, totalElements: number, pageable: Pageable)

Extends Page

Parameters
ids (Array<I>) Array of content ids
index ({}) Map of Id to Content Items
totalElements (number) The total number of elements in the data set
pageable (Pageable) The Pageable containing the paging information
Instance Members
ids
index
map(iteratee)

IndexablePage

Page type that can be serialized to json as either an {@ArrayPage} or {@IndexedPage}.

In order to achieve this, all elements in the content array must have an id property.

Then, upon serialization, if the indexed value is true, the content is grouped by id to obtain the map of {id: content item} and written as an IndexedPage, else it is written as an ArrayPage

new IndexablePage(content: Array<T>, totalElements: number, pageable: Pageable)

Extends Page

Parameters
content (Array<T>) The content to be returned
totalElements (number) The total number of elements in the data set
pageable (Pageable) The Pageable containing the paging information
Instance Members
content
indexed
map(iteratee)
toJSON()

Errors

KoaPageableError

Base Class for error types thrown by Koa Paginate.

new KoaPageableError(message: string)

Extends Error

Parameters
message (string) The human-readable message describing the error
Static Members
status
Instance Members
stack
message
name

NumberFormatError

Error type thrown when parsing Pagination parameters fails

new NumberFormatError(message: string)

Extends KoaPageableError

Parameters
message (string) The human-readable message describing the error
Static Members
status

InvalidSortError

Error type thrown when an invalid Sort Direction is provided on the request.

new InvalidSortError()

Extends KoaPageableError

Static Members
status

Flow Types

DirectionType

Flow type for Direction enum

DirectionType

Type: $Keys<any>