ThisIsMissEm / annotations-service

An experimental Web Annotations server written in Adonis.js
GNU Affero General Public License v3.0
0 stars 1 forks source link

Use keyset pagination instead of offset #1

Open ThisIsMissEm opened 2 hours ago

ThisIsMissEm commented 2 hours ago

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 commented 2 hours ago

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