Conversation
|
There is no change log for this pull request yet. |
ForbesLindesay
left a comment
There was a problem hiding this comment.
I'm very open to a feature like this. Offset is often the easiest way to handle pagination. I think rather than a combined limitOffset function that does both the offset and limit, I'd prefer that we add an .offset(count: number) method that you can then chain a .limit(count: number) onto:
export interface OrderedSelectQueryWithOffset<TRecord> extends SelectQuery<TRecord> {
first(): Promise<TRecord | null>;
limit(count: number): Promise<TRecord[]>;
}
export interface OrderedSelectQuery<TRecord> extends OrderedSelectQueryWithOffset<TRecord> {
offset(count: number): Promise<OrderedSelectQueryWithOffset<TRecord>>;
}Splitting out these two interfaces ensures you cannot call .offset(...) multiple times.
Usage would be:
import db, {users} from './database';
// Example for simple offset-based pagination
export async function offsetPaginatedEmails(offset?: number = 0) {
const records = await users(db)
.find()
.orderByAsc(`email`)
.offset(offset)
.limit(10);
return {
records: records.map((record) => record.email),
};
}|
P.S. it would save me time when merging if you also fill in the change log on Rolling Versions (see automated comment) for pg-typed and mysql-typed. |
|
OK I'm moving house right now but will take a look soon. |
|
@ForbesLindesay I gave it a shot but I'm not sure how the interface composing should turn out based on your example. I've ended up with tests not recognising |
There was a problem hiding this comment.
I can't see any attempt here to make the suggested change. I can offer feedback if you have a go and it doesn't work, but all this code just uses limitOffset as a single method. Please push what you have, even if it's not perfect, so I can see/review it.
| ```typescript | ||
| import db, {users} from './database'; | ||
|
|
||
| // Example for an endless pagination. Expects an email to be passed in, from where it returns 10 more rows. |
There was a problem hiding this comment.
I don't think this comment makes sense. There's nothing inherently "endless" about this pagination. It's commonly known as "token based pagination"
| } | ||
| ``` | ||
|
|
||
| ### limitOffset(count, offset) |
There was a problem hiding this comment.
This will need updating for the new .offset(count).limit(count) API.
|
|
||
| export async function paginatedEmails(nextPageToken?: string) { | ||
| // Example for an endless pagination. Expects an email to be passed in, from where it returns 10 more rows. | ||
| export async function endlessPaginatedEmails(nextPageToken?: string) { |
There was a problem hiding this comment.
Again, I don't think "endless" adds clarity here. I think this example can be left more or less as is. If you want to give it a name, the two types of pagination are "offset based pagination" and "token based pagination".
|
i agree to match chaining it should be |
Seems pretty simple to implement, should I start a new PR or did you want to do it or what would you prefer? |
|
@ForbesLindesay I took my first stab at this, can you please take a look and let me know of any changes required or any input on handling this? Thanks!! |
To address #234
I've added a skeleton for a
limitOffsetnext tolimitthat would pass in an offset value forpg-typedandmysql-typed.This probably still needs some work but I just wanted to ask if you're open for such a feature.
I also added some tests that seem to run.