🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

cursor-pagination

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cursor-pagination

Page token generator for javascript

latest
Source
npmnpm
Version
1.0.7
Version published
Maintainers
1
Created
Source

Cursor-Pagination

Page token generator for javascript using Protobufjs

Example Usage:

const { CursorPagination } = require('cursor-pagination');

const pagination = new CursorPagination();

// Returns a buffer
const tokenData = pagination.createToken({
  entries: [
    { key: 'updatedAt', value: new Date() },
    { key: 'id', value: 'abcdefg', asc: false }
  ]
});

// Convert to base64 string to return to user.
const token = tokenData.toString('base64');

// Convert token back into buffer and parse it
const parsedToken = pagination.parseToken(Buffer.from(token, 'base64'));

Pass in an AES cipher Key to return an encrypted token

const { CursorPagination } = require('cursor-pagination');

// Key should be a 16 character hex string
const pagination = new CursorPagination({
  aesKey: '2cc3b35dd381ff3a' // CHANGE ME
});

// Encrypted with the cipher key passed in
const tokenData = pagination.createToken({
  entries: [
    { key: 'updatedAt', value: new Date() },
    { key: 'id', value: 'abcdefg', asc: false }
  ]
});

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

Keywords

cursor

FAQs

Package last updated on 03 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts