adobnikar / bookshelf-eloquent

Bookshelf extension that adds functionality of eloquent.
MIT License
30 stars 8 forks source link

bookshelf-eloquent

This is a plugin for Bookshelf.js that adds some functionality from the Laravel's eloquent ORM. Most notably it improves nested eager loading (with function) and adds the withCount and whereHas functions while supporting existing Bookshelf plugins like registry, visibility, bookshelf-paranoia and others. All the functions documented here are accessible on both the static Bookshelf models and their instances.

About Bookshelf: Bookshelf is a JavaScript ORM for Node.js, built on the Knex SQL query builder. Featuring both promise based and traditional callback interfaces, providing transaction support, eager/nested-eager relation loading, polymorphic associations, and support for one-to-one, one-to-many, and many-to-many relations. It is designed to work well with PostgreSQL, MySQL, and SQLite3.

Requirements

Installation

Run the npm install command:

npm i --save bookshelf-eloquent

After installing bookshelf-eloquent, all you need to do is add it as a bookshelf plugin to enable it on your models.

let knex = require('knex')(require('./knexfile.js').development);
let bookshelf = require('bookshelf')(knex);

// Add the plugin
bookshelf.plugin(require('bookshelf-eloquent'));

Example

If you are new to bookshelf this example might help you out. The example contains migrations, models and a config .env file. The main.js file contains code that creates a user and a group and then fetches the data from the database and prints it out.

Steps:

For more examples you can check out the test files in this repository.

List of supported relations

List of all functions

Model

Collection

Get, First and Select functions

Where statements

Knex has a lot of useful where methods that are not directly accessible from the Bookshelf Model. Now all of the Knex where methods are directly attached to the Bookshelf Model. For the detailed documentation you can checkout the Knex documentation. All the where methods are chainable. The full list of methods:

Examples:

Require account and user models.

const User = require('../models/user');
const Account = require('../models/account');

Other Knex functions

For the detailed documentation you can checkout the Knex documentation. All these functions are chainable.

Examples:

With (Eager loading)

Examples:

Require the user model.

const User = require('../models/user');

WithCount

If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {camelCaseRelation}Count column on your resulting models.

Examples:

Require the user model.

const User = require('../models/user');

Has and WhereHas

When accessing the records for a model, you may wish to limit your results based on the existence of a relationship. For example, imagine you want to retrieve all blog posts that have at least one comment. To do so, you may pass the name of the relationship to the has method:

If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries. These methods allow you to add customized constraints to a relationship constraint, such as checking the content of a comment:

Destroy / Delete All

WithDeleted / WithTrashed (bookshelf-paranoia)

Complete list of function synonyms

Miscellaneous

Bulk insert