drizzle-more
v0.1.3
Published
A collection of utilities for Drizzle ORM.
Maintainers
Readme
drizzle-more
A collection of utilities for Drizzle ORM for non-trivial but still common uses.
Installation
npm install drizzle-moreor
pnpm add drizzle-moreAPI
sqliteEnum
Creates a SQLite enum-like column with runtime validation. Since SQLite lacks native enum support, this uses a TEXT column that validates values on insert/update.
import { sqliteEnum } from 'drizzle-more';
const users = sqliteTable('users', {
id: integer('id').primaryKey(),
status: sqliteEnum(['active', 'inactive', 'pending'] as const, 'status').notNull(),
});Throws an error if an invalid value is provided.
paginateQuery
Applies pagination to a Drizzle query. Supports page-based or offset-based pagination.
import { paginateQuery } from 'drizzle-more';
// Page-based (page 2, 10 items per page)
const results = await paginateQuery(db.select().from(users), { page: 2, limit: 10 });
// Offset-based (skip 20, take 10)
const results = await paginateQuery(db.select().from(users), { offset: 20, limit: 10 });Page-based: { page: number, limit: number } — page is 1-indexed, offset is calculated automatically.
Offset-based: { offset: number, limit: number } — specify the exact number of records to skip.
paginateWithTotal
Paginates a query and returns both the data and total count—useful for building paginated UIs.
import { paginateWithTotal } from 'drizzle-more';
const { total, data } = await paginateWithTotal(db, db.select().from(users), {
page: 1,
limit: 10,
});
const totalPages = Math.ceil(total / 10);Returns: { total: number, data: T[] }
⚠️ Note: This function executes 2 queries sequentially (one for count, one for data). Use with caution in performance-critical scenarios.
stackedWhereQuery
Drizzle ORM's .where() replaces any previous condition. To combine conditions, you must wrap them in and(...):
// Without stackedWhereQuery - must use and() explicitly
const results = await db.select().from(users)
.where(and(eq(users.status, 'active'), gt(users.age, 18)));This becomes unwieldy when building queries conditionally. stackedWhereQuery lets you call .where() multiple times—conditions are automatically combined with AND:
import { stackedWhereQuery } from 'drizzle-more';
import { eq, gt } from 'drizzle-orm';
// Basic usage - chain multiple where calls
const results = await stackedWhereQuery(db.select().from(users).$dynamic())
.where(eq(users.status, 'active'))
.where(gt(users.age, 18));// Conditional filtering
const query = stackedWhereQuery(db.select().from(users).$dynamic());
if (filterByStatus) query.where(eq(users.status, 'active'));
if (filterByAge) query.where(gt(users.age, 18));
const results = await query;Combining Query Utilities
Use stackedWhereQuery with paginateWithTotal for filtered, paginated results:
import { stackedWhereQuery, paginateWithTotal } from 'drizzle-more';
import { eq, gt } from 'drizzle-orm';
const query = stackedWhereQuery(db.select().from(users).$dynamic())
.where(eq(users.status, 'active'))
.where(gt(users.age, 18));
const { total, data } = await paginateWithTotal(db, query, { page: 1, limit: 10 });License
MIT
