Can't save Bookshelf model instance

Problem

I'm experimenting with Bookshelf, and made a small program to learn how it works.

Unfortunately it seems it doesn't really work, as Knex complains that it haven't been initialized.

I'm using Bookshelf version 0.3.1, and Knex version 0.2.6.

When I run my simple test program, I get the following error:

/home/joachimp/tmp/ks/db/node_modules/knex/knex.js:20
      throw new Error('The Knex instance has not been initialized yet.');
            ^
Error: The Knex instance has not been initialized yet.
    at Knex (/home/joachimp/tmp/ks/db/node_modules/knex/knex.js:20:13)
    at _.extend.builder (/home/joachimp/tmp/ks/db/node_modules/bookshelf/bookshelf.js:384:14)
    at query (/home/joachimp/tmp/ks/db/node_modules/bookshelf/bookshelf.js:1294:35)
    at _.extend.query (/home/joachimp/tmp/ks/db/node_modules/bookshelf/bookshelf.js:379:14)
    at new Bookshelf.Sync (/home/joachimp/tmp/ks/db/node_modules/bookshelf/bookshelf.js:823:26)
    at _.extend.sync (/home/joachimp/tmp/ks/db/node_modules/bookshelf/bookshelf.js:389:14)
    at _.extend.save (/home/joachimp/tmp/ks/db/node_modules/bookshelf/bookshelf.js:263:24)
    at Object. (/home/joachimp/tmp/ks/db/dbtest.js:20:6)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

And the program is simply this:

var Bookshelf = require('bookshelf');

Bookshelf.Initialize('sqlite3', {
    client: 'sqlite3',
    connection: {
        filename : './dbtest.sqlite3'
    }
});

var TestModel = Bookshelf.Model.extend({
    tableName: 'TestModel',

    initialize: function() {
    },

    name: 'foo'
});

var test = new TestModel;
test.save();    // <- Line 20

console.log('All done');

The documentation is scarce, and examples even more so, or I might have figured it out already.

I have also tried creating collections and putting model instances in them, and using sync object with the insert method. All with the same result of Knex not being initialized.

What am I missing? Do I have to initialize Knex separately? And (yes I know it's off-topic) are there any simple examples or tutorials to learn from?

Problem courtesy of: Some programmer dude

Solution

So this was sort of a bad design decision, there is a try/catch block in "Knex" wrapping the client initialize code, so there's an unrelated error with the client other than using the wrong name, it gets silenced.

I'm guessing there's something wrong with the sqlite3 client you're using, this has been fixed in the latest version. Try it with the latest Bookshelf 0.5.1 and Knex 0.4.3 with this code:

var Bookshelf = require('bookshelf');

var bookshelf = Bookshelf.initialize({
    client: 'sqlite3',
    connection: {
        filename : './dbtest.sqlite3'
    }
});

var TestModel = bookshelf.Model.extend({
    tableName: 'TestModel',

    initialize: function() {
    },

    name: 'foo'
});

var test = new TestModel;
test.save();    // <- Line 20

console.log('All done');

As for examples, I'm hoping to get one together soon... otherwise, looking at the code in the integration tests would be your best bet.

Solution courtesy of: tgriesser

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.