Cursor-Pagination
Page token generator for javascript using Protobufjs
Example Usage:
const CursorPagination = ; const pagination = ; // Returns a bufferconst tokenData = pagination; // Convert to base64 string to return to user.const token = tokenData; // Convert token back into buffer and parse itconst parsedToken = pagination;
Pass in an AES cipher Key to return an encrypted token
const CursorPagination = ; // Key should be a 16 character hex stringconst pagination = aesKey: '2cc3b35dd381ff3a' // CHANGE ME; // Encrypted with the cipher key passed inconst tokenData = pagination;
Notes
When working with SQL, the parsed token should be converted into a boolean logic statement.
A single column can easily be turned into the statement WHERE id > @id
, however dealing with multiple columns becomes a little trickier.
Pagination based on multiple columns would look something like this:
WHERE (column1 > @value1) OR (column1 = @value1 AND column2 > @value2) OR (column1 = @value1 AND column2 = @value2 AND column3 > @value3)
The combination of these columns must be unique so it is a good idea to always make the last column of the token the primary key for the table.
References
https://www.sitepoint.com/paginating-real-time-data-cursor-based-pagination/ https://stackoverflow.com/questions/38017054/mysql-cursor-based-pagination-with-multiple-columns