Bookshelf.js Example

06 Apr 2014

So getting up and running with Bookshelf.js was not as easy as I was expecting. I knew that it sits on top of Knex.js so I kept going thru the docs looking for the Bookshelf method that abstracts away table creation from Knex. When I didn't find it, I assumed they must be doing some next level stuff where they just fully abstract away table creation/alteration on the fly. Nope. The answer is you just use knex for that:

Connect to your database - Bookshelf.initialize

var Bookshelf = require('bookshelf')
Bookshelf.conn = Bookshelf.initialize({
    client: 'sqlite3',
    connection: {
        filename: './mydb.sqlite'

Create your table (with knex) - Bookshelf.knex / knex.schema.createTable

Bookshelf.conn.knex.schema.createTable('customers', function (customer) {
}).then(function () {

Create your model - Bookshelf.Model.extend

    var Customer = Bookshelf.conn.Model.extend({
      tableName: 'customers'

Create your instance/row - Bookshelf.Model.forge

    Customer.forge({name: 'Reid Ransom'}).save().then(function() {

        // Say "hi"

Note: It may look a little wierd the way the docs refer to Bookshelf.Something when in your code it's more like Bookshelf.conn.Something but they explain that under the Bookshelf.initialize docs.