bookshelf-consul-pilot

Installation
Your database connection file might look something like this:
const BookshelfConsulPilot = require('bookshelf-consul-pilot');
const knexfile = require('../../knexfile').development;
const path = require('path');
module.exports = new BookshelfConsulPilot(knexfile, 'database', path.join(__dirname, '/../models'), (bookshelf) => {
bookshelf.plugin('pagination');
bookshelf.plugin(require('bookshelf-signals')());
});
Usage
Defining Models
Ensure that your model files are wrapped in a function that accepts bookshelf as an argument:
module.exports = (bookshelf) => {
return bookshelf.Model.extend({
tableName: 'books',
});
};
Querying
const db = require('database');
function getBooks() {
db.model('book').fetchPage()
.then((books) => {
console.log(books);
});
}
Registering Events
Anything that must modify the Bookshelf instance or its models must be wrapped in a register. This allows bookshelf-consul-pilot
to completely reset the Bookshelf instance if a new connection were to be reported. An example is events using the bookshelf-signals
plugin:
const db = require('database');
db.register((bookshelf) => {
bookshelf.on('created', db.model('book'), (model) => {
console.log('created fired!');
});
bookshelf.on('updated', db.model('book'), (model) => {
console.log('updated fired!');
});
});