Paging and sorting

Paging

When reading the quick start guide, you may have asked yourself why the readAll method of the generated client did not just return an array of TodoItems, but an object where the actual TodoItem were in a field called items. This is because readAll supports paging and also returns paging metadata. Next to the items field, you can also access an object of the following type:

interface PageData {
    size: number; // The size of the current page
    totalElements: number; // How many entities there are in total
    totalPages: number; // How many pages you can query (So totalElements / size, rounded up)
    number: number; // The page for which items have been returned, starting at 0
}

The corresponding Kotlin data class has the same fields and can be accessed in the same way. If you inspect the response you get when loading the TodoItems in the quick start guide example, you will notice that by default, a maximum of 20 entities will be retrieved.

If you have more than 20 entities, you can pass an integer as the first parameter. Take note that this number is an index starting at 0, so if you wish to retrieve items 21 up to 40, you have to use 1 as the parameter. If you do not specify the first parameter or use undefined, the default page will be 0.

You are not restricted to pages of size 0, but you can use the second parameter to pass the page size.

Sorting

The third parameter of readAll is used for returning entities in a specific order. A corresponding call may look like this:

const comments = await apiClient.commentClient.readAll(undefined, undefined, "createdAt,DESC");

In this example, a list of comments is retrieved so that the most recent comment is the first one in the list. Only the 20 most recent comments are returned, since the default values for page and size are 0 and 20 respectively. The sort parameter must be string consisting of a property to sort by and a sort direction of either ASC (ascending) or DESC (descending) separated by a comma.

Other methods

Paging and sorting is neither restricted to Typescript nor to the readAll method. You can use both when fetching projections and repository searches. For custom searches, at least sorting is currently available. Of course the parameters mentioned here do not only exist in Typescript, but also in Kotlin code generated by FeGen.