Skip to content
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

Use keyset pagination instead of offset #1

Open
ThisIsMissEm opened this issue Nov 1, 2024 · 1 comment
Open

Use keyset pagination instead of offset #1

ThisIsMissEm opened this issue Nov 1, 2024 · 1 comment

Comments

@ThisIsMissEm
Copy link
Owner

ThisIsMissEm commented Nov 1, 2024

Currently Adonis's built in paginate method relies on offset pagination, which means pagination cursors aren't stable. This can cause issues in annotation collections which are frequently updated, as you'll miss items.

Keyset pagination is the solution.

Essentially instead of:

SELECT * FROM "annotations" WHERE collection_id = decode(?, 'hex') LIMIT ? OFFSET ?

We do:

SELECT * FROM "annotations" WHERE collection_id = decode(?, 'hex') AND id > ? ORDER BY id DESC LIMIT ?

It does change the pagination URLs too, from:

/annotations/781bc04ec9bd049cdfacaec4ae2026102118d13c79413246f0c88eeef6ebec4e?page=1

to:

/annotations/781bc04ec9bd049cdfacaec4ae2026102118d13c79413246f0c88eeef6ebec4e?min_id=0192e517-1e93-742d-a8bf-e8f491c570da

or

/annotations/781bc04ec9bd049cdfacaec4ae2026102118d13c79413246f0c88eeef6ebec4e?max_id=0192e517-1e93-742d-a8bf-e8f491c570da
@ThisIsMissEm
Copy link
Owner Author

We can also rely on sorting with the ID column, since that's a UUID v7 which is time-orderable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant