chiselstrike icon indicating copy to clipboard operation
chiselstrike copied to clipboard

CRUD-like paging for custom endpoints

Open BearLemma opened this issue 3 years ago • 3 comments

@luisfvieirasilva and @ricardodalarme asked for an easy way of generating paging for custom endpoints.

The usage might look as follows:

export default new RouteMap()
    .getPaginated('/get_person', (q: QueryParams) => {
        return Person.cursor().filter({name: q.name}).map(p => p.age);
    }, 50 /*page size*/)

Calling /get_person would generate paginated response listing people's ages with a name given by the QueryParams. next/previous URLs would have to point to a different internal auto-generated endpoint __internal/get_person which would be created automatically. cc @honzasp

BearLemma avatar Oct 17 '22 08:10 BearLemma

Rather than trying to do this on the level of RouteMap, it should perhaps be a helper function that you call from your route, something like

async function getPerson(req: ChiselRequest, query: QueryParams) {
    const cursor = Person.cursor().filter({name: query.name}).map(p => p.age);
    return paginate(cursor, req);
}

honzasp avatar Oct 17 '22 09:10 honzasp

I was considering that but in any case, we will need the auto-generated internal endpoints and when we use explicit methods in the route map, I thought it would be easier to infer. But you are right, what you propose is cleaner.

What's not clear though is how to convert cursor to something we can put to the next_page URL. For CRUD it's easy, but cursor can contain arbitrary lambdas :thinking: . We will need a way to somehow capture the cursor transformation chain.

BearLemma avatar Oct 17 '22 11:10 BearLemma

What do you mean by "auto-generated internal endpoints"? It seems to me that we could implement the hypothetical paginate() function today, without any extensive modifications to our code :thinking:

I think that we can assume that the definition of cursor will stay the same between requests, so one option is to simply use LIMIT {page_size} OFFSET {page_idx*page_size} to implement poor-man's paging.

honzasp avatar Oct 18 '22 06:10 honzasp