Registry / database / bookshelf-schema

bookshelf-schema

JSON →
library0.3.6jsnpmunverified

A Bookshelf ORM plugin that adds declarative schema definitions including fields, relations, scopes, and validations directly on model classes. Version 0.3.6 (stable, last release likely 2018) is compatible with Bookshelf >=0.8.2 <0.11. Unlike bookshelf-fields, this plugin integrates fields, relations, and scopes into a single schema definition. Documentation is hosted on ReadTheDocs. Note: Bookshelf itself is largely unmaintained, and this plugin has not seen updates in years, making it risky for new projects.

npm install bookshelf-schema
INSTALL
IMPORT
SIG · BOOKSHELF-SCHEMA
B
bookshelf-schema
databasejavascriptv0.3.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

plugin function (default)
import bookshelfSchema from 'bookshelf-schema'; bookshelf.plugin(bookshelfSchema());
const bookshelfSchema = require('bookshelf-schema'); bookshelf.plugin(bookshelfSchema);
The default export is a function that must be called (with no arguments typically) to get the plugin instance, not the plugin itself.
Field classes (EmailField, etc.)
import { EmailField, EncryptedStringField, BooleanField, HasMany, Scope } from 'bookshelf-schema';
const schema = require('bookshelf-schema'); const EmailField = schema.EmailField;
Named imports are available for schema definition components. Common mistake: trying to destructure from the default export function instead of using named imports.
bookshelf.plugin usage
bookshelf.plugin(require('bookshelf-schema')());
bookshelf.plugin(require('bookshelf-schema'));
Since the default export is a function, it must be invoked. Failure to call it results in a runtime error because the plugin expects a function, not an object.

Shows how to install the plugin, register it with Bookshelf, define a model with schema (fields, relation, scope), and perform a basic save.

// Install: npm install bookshelf-schema const knex = require('knex')({ client: 'sqlite3', connection: { filename: ':memory:' } }); const bookshelf = require('bookshelf')(knex); // Register plugin bookshelf.plugin(require('bookshelf-schema')()); const { EmailField, BooleanField, HasMany, Scope } = require('bookshelf-schema'); const User = bookshelf.Model.extend( { tableName: 'users' }, { schema: [ EmailField('email'), BooleanField('active'), HasMany('Photo'), Scope('isActive', function () { return this.where({ active: true }); }), ], } ); const Photo = bookshelf.Model.extend({ tableName: 'photos' }); new User({ email: 'test@test.com', active: true }) .save() .then(user => console.log('User created:', user.get('email')));
Debug
Known issues
breakingThe plugin modifies Bookshelf's Model prototype globally; after registration, all models will have schema validation applied.
fix
Ensure you only register the plugin once and be aware of side effects on existing models.
affects: >=0.1.0
deprecatedNo releases since 2018. Bookshelf itself is largely unmaintained and many users have migrated to Knex or TypeORM.
fix
Consider using Knex schema builder or modern ORM with active support.
affects: >=0.1.0
gotchaThe plugin's default export must be called as a function: require('bookshelf-schema')(). Failing to call it results in a runtime error: 'bookshelf.plugin expects a function'.
fix
Always invoke the exported function: bookshelf.plugin(require('bookshelf-schema')());
affects: >=0.1.0
gotchaField types like EmailField and EncryptedStringField may not actually validate or encrypt data; they are just labels for schema definition. Encryption must be implemented separately.
fix
Use actual validation/encryption libraries (e.g., bcrypt) and enforce in model lifecycle hooks.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: bookshelf.plugin is not a function
bookshelf.plugin is called on an object that isn't the bookshelf instance (e.g., wrong require).
fix
Ensure you have correctly required bookshelf: const bookshelf = require('bookshelf')(knex);
Error: Cannot find module 'bookshelf-schema'
Package not installed or typo in package name.
fix
Run: npm install bookshelf-schema
Uncaught TypeError: bookshelf_schema is not a function
Tried to call require('bookshelf-schema') and then pass result directly to bookshelf.plugin without invoking the returned function.
fix
Use: bookshelf.plugin(require('bookshelf-schema')());
Upgrade
Version history
0.3.6latest on npm
Audit
Dependencies
bookshelfrequiredpeer dependency: requires bookshelf >=0.8.2 <0.11
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
bookshelf-schema — npm install bookshelf-schema · libregistry