Skip to content

Instantly share code, notes, and snippets.

@pirey
pirey / knex-pagination.js
Last active April 6, 2024 14:40 — forked from andremsantos/knex-pagination.js
Adding pagination to knex.js
const config = require('./config')
const knex = require('knex')(config.db)
const QueryBuilder = require('knex/lib/query/builder')
QueryBuilder.prototype.paginate = function ({ limit = 10, page = 1 }) {
const offset = (page - 1) * limit
return Promise.all([
this.clone().count('* as count').first(),
this.offset(offset).limit(limit)