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')));
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).
fixEnsure you have correctly required bookshelf: const bookshelf = require('bookshelf')(knex); Error: Cannot find module 'bookshelf-schema'
Package not installed or typo in package name.
fixRun: 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.
fixUse: bookshelf.plugin(require('bookshelf-schema')()); Audit
Dependencies
bookshelfrequiredpeer dependency: requires bookshelf >=0.8.2 <0.11