API Reference

Bookshelf.js

Promise-based ORM for Node.js that wraps Knex with models, collections, and relation helpers for older codebases.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

npmnpm install bookshelf knex
pnpmpnpm add bookshelf knex

Methods

Overview

Bookshelf.js is a promise-based ORM that wraps Knex with models, collections, and relation helpers for legacy Node.js codebases. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • Model.forge — Creates an unsaved model instance with the given attributes so you can fetch or save it.
  • Model.fetch — Fetches a single row matching the model attributes and resolves with the model.
  • Model.save — Inserts or updates the row represented by the model depending on its id.

Example

const knex = require('knex')({ client: 'pg' });
const bookshelf = require('bookshelf')(knex);

const User = bookshelf.model('User', { tableName: 'users' });
const user = User.forge({ id: 1 });

Notes

By default fetch rejects when no row matches, so use require: false for optional lookups. Transactions are passed through the transacting option.

Verification

Fetch a non-existent id with require: false and confirm it resolves to null rather than throwing.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://bookshelfjs.org/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:30.087Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • Add more helpful error messages on bad or insufficient morphTo data: #1824 . (https://bookshelfjs.org/)
  • Fixes Bluebird warning when a Promise was internally rejected with a non-error. (https://bookshelfjs.org/)
  • Fix error in virtuals plugin - #936 Correct error updating parsed/formatted Model#idAttribute after successful insert operation. (https://bookshelfjs.org/)

FAQ

How do I load related records?
Define relations with hasMany, belongsTo, or belongsToMany and call load() or fetch with withRelated.
How does fetch handle missing rows?
By default fetch rejects with a NotFoundError; pass { require: false } to resolve with null instead.
Does Bookshelf support transactions?
Yes. Pass a Knex transaction via the transacting option on fetch and save, or use bookshelf.transaction(callback).