or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bookshelf-instance.mdcollections.mdevents.mdindex.mdmodels.mdquery-building.mdrelationships.md
tile.json

bookshelf-instance.mddocs/

Bookshelf Instance

Core Bookshelf instance functionality including initialization, model registry, transactions, and plugin system.

Capabilities

Bookshelf Constructor

Creates a new Bookshelf instance initialized with a Knex client.

/**
 * Creates a new Bookshelf instance
 * @param {Knex} knex - Initialized Knex client instance
 * @returns {Bookshelf} Bookshelf instance
 * @throws {Error} If knex is not a valid Knex instance
 */
function Bookshelf(knex);

Usage Example:

const knex = require('knex')({
  client: 'mysql',
  connection: {
    host: '127.0.0.1',
    user: 'your_db_user', 
    password: 'your_password',
    database: 'myapp'
  }
});

const bookshelf = require('bookshelf')(knex);

Instance Properties

Core properties available on Bookshelf instances.

interface BookshelfProperties {
  /** Current Bookshelf version string */
  VERSION: string;
  
  /** Reference to the underlying Knex instance */
  knex: Knex;
  
  /** Base Model constructor for extending */
  Model: ModelConstructor;
  
  /** Base Collection constructor for extending */
  Collection: CollectionConstructor;
}

Model Registry

Register and retrieve models by name for avoiding circular dependencies.

/**
 * Register or retrieve a model by name
 * @param {string} name - Model name
 * @returns {Model} Previously registered model
 */
model(name: string): Model;

/**
 * Register a new model
 * @param {string} name - Model name for registry
 * @param {Model|object} Model - Model constructor or plain object to extend
 * @param {object} [staticProperties] - Static properties for the model
 * @returns {Model} Registered model constructor
 */
model(name: string, Model: ModelConstructor | object, staticProperties?: object): Model;

Usage Examples:

// Register a model
const User = bookshelf.model('User', {
  tableName: 'users',
  posts() {
    return this.hasMany('Post'); // Can reference by string name
  }
});

// Retrieve a registered model
const User = bookshelf.model('User');

// Register with static properties
const Post = bookshelf.model('Post', {
  tableName: 'posts'
}, {
  findByTitle(title) {
    return this.where('title', title).fetch();
  }
});

Collection Registry

Register and retrieve collections by name.

/**
 * Register or retrieve a collection by name
 * @param {string} name - Collection name
 * @returns {Collection} Previously registered collection
 */
collection(name: string): Collection;

/**
 * Register a new collection
 * @param {string} name - Collection name for registry
 * @param {Collection|object} Collection - Collection constructor or plain object to extend
 * @param {object} [staticProperties] - Static properties for the collection
 * @returns {Collection} Registered collection constructor
 */
collection(name: string, Collection: CollectionConstructor | object, staticProperties?: object): Collection;

Usage Example:

const Users = bookshelf.collection('Users', {
  model: bookshelf.model('User')
});

const users = bookshelf.collection('Users');

Model Resolution

Override default model resolution behavior for custom loading strategies.

/**
 * Custom resolver function for model/collection names
 * Override this to define custom resolution logic
 * @param {string} name - The model/collection name to resolve
 * @returns {*} The resolved model/collection or any custom value
 */
resolve(name: string): any;

Usage Example:

// Custom resolver
bookshelf.resolve = (name) => {
  if (name === 'SpecialUser') {
    return bookshelf.model('User');
  }
  // Return undefined for default behavior
};

Transaction Support

Database transaction wrapper using Knex transaction system.

/**
 * Execute operations within a database transaction
 * @param {Function} transactionCallback - Callback containing transaction logic
 * @returns {Promise} Promise resolving to callback return value
 */
transaction(transactionCallback: (transaction: Transaction) => Promise<any>): Promise<any>;

Usage Example:

const result = await bookshelf.transaction(async (t) => {
  const user = await new User({name: 'Alice'}).save(null, {transacting: t});
  const post = await new Post({
    title: 'Hello World',
    user_id: user.id
  }).save(null, {transacting: t});
  
  return {user, post};
});

Plugin System

Load plugins to extend Bookshelf functionality.

/**
 * Load a plugin into the Bookshelf instance
 * @param {string|Function|Array} plugin - Plugin to load (module name, function, or array)
 * @param {*} [options] - Options to pass to the plugin
 * @returns {Bookshelf} The bookshelf instance for chaining
 */
plugin(plugin: string | Function | Array, options?: any): BookshelfInstance;

Usage Examples:

// Load plugin by module name
bookshelf.plugin('bookshelf-uuid');

// Load plugin function
bookshelf.plugin(function(bookshelf, options) {
  // Plugin implementation
});

// Load multiple plugins
bookshelf.plugin(['plugin1', 'plugin2'], {option1: 'value'});

// Plugin with options
bookshelf.plugin('some-plugin', {
  option1: 'value1',
  option2: 'value2'
});

Built-in Plugin Messages

Bookshelf displays warnings for deprecated built-in plugins that have been moved or removed:

  • pagination - Moved to core, use fetchPage() directly
  • visibility - Moved to core, use hidden/visible properties directly
  • registry - Moved to core, use model()/collection() methods directly
  • processor - Removed, migrate to standalone package
  • case-converter - Removed, migrate to standalone package
  • virtuals - Removed, migrate to standalone package

Types

interface Transaction {
  // Knex transaction object - see Knex documentation
}

interface ModelConstructor {
  new(attributes?: object, options?: object): Model;
  forge(attributes?: object, options?: object): Model;
  // Additional static methods...
}

interface CollectionConstructor {
  new(models?: any[], options?: object): Collection;
  forge(models?: any[], options?: object): Collection;
  // Additional static methods...
}