-
Notifications
You must be signed in to change notification settings - Fork 48
LimitOffset implementation #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,6 +226,7 @@ Return the first `count` rows. N.B. you can only use this method if you have fir | |
| ```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. | ||
| export async function paginatedEmails(nextPageToken?: string) { | ||
| const records = await users(db) | ||
| .find({ | ||
|
|
@@ -253,6 +254,27 @@ export async function printAllEmails() { | |
| } | ||
| ``` | ||
|
|
||
| ### limitOffset(count, offset) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need updating for the new |
||
|
|
||
| Return the first `count` rows offset by `offset` number of rows. N.B. you can only use this method if you have first called `orderByAsc` or `orderByDesc` at least once. | ||
|
|
||
| If you have a large number of rows (more than some thousands), using an offset is inefficient as it will scan through the results. For larger sets, see the endless pagination example above. | ||
|
|
||
| ```typescript | ||
| 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`) | ||
| .limitOffset(10, offset); | ||
| return { | ||
| records: records.map((record) => record.email), | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| ### first() | ||
|
|
||
| Return the first record. If there are no records, `null` is returned. N.B. you can only use this method if you have first called `orderByAsc` or `orderByDesc` at least once. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -484,7 +484,8 @@ Return the first `count` rows. N.B. you can only use this method if you have fir | |
| ```typescript | ||
| import db, {users} from './database'; | ||
|
|
||
| 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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". |
||
| const records = await users(db) | ||
| .find({ | ||
| ...(nextPageToken ? {email: gt(nextPageToken)} : {}), | ||
|
|
@@ -498,19 +499,40 @@ export async function paginatedEmails(nextPageToken?: string) { | |
| } | ||
|
|
||
| export async function printAllEmails() { | ||
| let page = await paginatedEmails(); | ||
| let page = await endlessPaginatedEmails(); | ||
| while (page.records.length) { | ||
| for (const email of page.records) { | ||
| console.log(email); | ||
| } | ||
| if (!page.nextPageToken) { | ||
| break; | ||
| } | ||
| page = await paginatedEmails(page.nextPageToken); | ||
| page = await endlessPaginatedEmails(page.nextPageToken); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### limitOffset(count, offset) | ||
|
|
||
| Return the first `count` rows offset by `offset` number of rows. N.B. you can only use this method if you have first called `orderByAsc` or `orderByDesc` at least once. | ||
|
|
||
| If you have a large number of rows (more than some thousands), using an offset is inefficient as it will scan through the results. For larger sets, see the endless pagination example above. | ||
|
|
||
| ```typescript | ||
| 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`) | ||
| .limitOffset(10, offset); | ||
| return { | ||
| records: records.map((record) => record.email), | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| ### first() | ||
|
|
||
| Return the first record. If there are no records, `null` is returned. N.B. you can only use this method if you have first called `orderByAsc` or `orderByDesc` at least once. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this comment makes sense. There's nothing inherently "endless" about this pagination. It's commonly known as "token based pagination"