🗄️
Database50K+/wkMIT

bookshelf

Bookshelf is a JavaScript ORM for Node.js built on top of the Knex.js query builder, providing an Active Record-like interface for working with relational datab

Installation

npm
npm install bookshelf knex
yarn
yarn add bookshelf knex
pnpm
pnpm add bookshelf knex

Import

ESM
import Bookshelf from 'bookshelf';

Quick Example

usage
import Knex from 'knex';
import Bookshelf from 'bookshelf';

const knex = Knex({ client: 'pg', connection: process.env.DATABASE_URL });
const bookshelf = Bookshelf(knex);

const User = bookshelf.model('User', { tableName: 'users' });
const user = await User.forge({ name: 'Alice' }).save();

About bookshelf

Bookshelf is a JavaScript ORM for Node.js built on top of the Knex.js query builder, providing an Active Record-like interface for working with relational databases. Bookshelf supports PostgreSQL, MySQL, MariaDB, and SQLite through Knex's multi-dialect support. The library provides model definitions with relations (hasOne, hasMany, belongsTo, belongsToMany through junction tables), eager loading to prevent N+1 query problems, virtual attributes for computed properties, and lifecycle events (creating, saving, updating, destroying) for model hooks. Bookshelf's API is inspired by Backbone.js models, providing fetch(), save(), destroy(), and forge() methods on model instances. The library supports pagination through the bookshelf-page plugin, soft deletes through bookshelf-paranoia, and case conversion through bookshelf-case-converter. While newer ORMs like Prisma and Drizzle offer better TypeScript support and developer experience, Bookshelf remains in use in existing codebases where its Knex foundation provides a familiar and well-understood query building layer. Bookshelf is particularly suitable for applications that need a simple Active Record pattern with the flexibility to drop down to Knex's query builder for complex queries.

Quick Facts

Packagebookshelf
CategoryDatabase
Weekly Downloads50K+
LicenseMIT
Installnpm install bookshelf knex

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.