diff --git a/README.md b/README.md index b221b58..d771ee4 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ server.register([ }, plugins: ['registry'], // Required models: '../path/to/models/directory', - base: require('../path/to/model/base') + base: require('../path/to/model/base') // optional } } ], function (err) { @@ -42,15 +42,10 @@ server.register([ - ```knex``` [Knex Confuration Object](http://knexjs.org/#Installation-client) - ```plugins``` [Bookshelf.js Plugins](http://bookshelfjs.org/#Plugins) the _registry_ plugin is required - ```models``` directory where you Bookshelf.js models are defined -- ```base```options that will be passed to the Bookshelf.js [extend method](http://bookshelfjs.org/#Model-extend), example below. +- ```base``` (optional) function that applies the Bookshelf.js [extend method](http://bookshelfjs.org/#Model-extend) and returns the extended model, example below. ### Example ```base``` ```javascript -// Basic, No Extension -base: function (bookshelf) { - return bookshelf.Model.extend({}); -} - // Add timestamps to all models base: function (bookshelf) { return bookshelf.Model.extend({ diff --git a/lib/index.js b/lib/index.js index 6bcd6a2..27f11c5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,7 +8,7 @@ exports.register = function (server, options, next) { knex: Joi.object().required(), plugins: Joi.array().includes(Joi.string()).default([]), models: Joi.string().required(), - base: Joi.func().required() + base: Joi.func().optional() }; try { @@ -28,7 +28,12 @@ exports.register = function (server, options, next) { bookshelf.plugin(plugin); }); - var baseModel = options.base(bookshelf); + var baseModel; + if (options.base) { + baseModel = options.base(bookshelf); + } else { + baseModel = bookshelf.Model.extend({}); + } fs.readdirSync(options.models).forEach(function (model) { var modelName = path.basename(model).replace(path.extname(model), ''); diff --git a/test/index.test.js b/test/index.test.js index 9e9b5da..0759d3f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -63,13 +63,41 @@ describe('bookshelf plugin', function () { plugins: ['registry'], models: path.join(__dirname + '/models'), base: function (bookshelf) { - return bookshelf.Model.extend({}); + return bookshelf.Model.extend({ + test: 'test' + }); } } } ], function (err) { expect(err).to.be.undefined; expect(server.plugins.bookshelf.model('User')).to.be.a('function'); + var User = server.plugins.bookshelf.model('User').forge({ id: 1 }); + expect(User.test).to.eql('test'); }); }); + + it('should load a good configuration without base', function () { + var server = new Hapi.Server(); + + server.register([ + { + register: require('../lib/'), + options: { + knex: { + client: 'sqlite3', + connection: { + filename: './database.sqlite' + } + }, + plugins: ['registry'], + models: path.join(__dirname + '/models') + } + } + ], function (err) { + expect(err).to.be.undefined; + expect(server.plugins.bookshelf.model('User')).to.be.a('function'); + }); + }); + });