UNPKG

drizzle-pagination

Version:

Easily add cursor pagination to your Drizzle ORM queries.

76 lines (74 loc) 3.44 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc2) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc2 = __getOwnPropDesc(from, key)) || desc2.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { withCursorPagination: () => withCursorPagination }); module.exports = __toCommonJS(src_exports); var import_drizzle_orm = require("drizzle-orm"); function withCursorPagination({ cursors, limit, where: inputWhere }) { const primaryColumn = cursors[0][0]; const primaryOrder = cursors[0][1] === "asc" ? import_drizzle_orm.asc : import_drizzle_orm.desc; const primaryOperator = cursors[0][1] === "asc" ? import_drizzle_orm.gt : import_drizzle_orm.lt; const primaryCursor = cursors[0][2]; const secondaryColumn = cursors[1] ? cursors[1][0] : null; const secondaryOrder = cursors[1] ? cursors[1][1] === "asc" ? import_drizzle_orm.asc : import_drizzle_orm.desc : null; const secondaryOperator = cursors[1] ? cursors[1][1] === "asc" ? import_drizzle_orm.gt : import_drizzle_orm.lt : null; const secondaryCursor = cursors[1] ? cursors[1][2] : void 0; const singleColumnPaginationWhere = typeof primaryCursor !== "undefined" ? primaryOperator(primaryColumn, primaryCursor) : void 0; const doubleColumnPaginationWhere = secondaryColumn && secondaryOperator && typeof primaryCursor !== "undefined" && typeof secondaryCursor !== "undefined" ? (0, import_drizzle_orm.or)( primaryOperator(primaryColumn, primaryCursor), (0, import_drizzle_orm.and)( (0, import_drizzle_orm.eq)(primaryColumn, primaryCursor), secondaryOperator(secondaryColumn, secondaryCursor) ) ) : void 0; const paginationWhere = secondaryColumn ? doubleColumnPaginationWhere : singleColumnPaginationWhere; const where = inputWhere ? paginationWhere ? (0, import_drizzle_orm.and)(inputWhere, paginationWhere) : inputWhere : paginationWhere; return __spreadValues({ orderBy: [ primaryOrder(primaryColumn), ...secondaryColumn && secondaryOrder ? [secondaryOrder(secondaryColumn)] : [] ], limit }, where ? { where } : {}); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { withCursorPagination });