Skip to content
This repository was archived by the owner on Oct 9, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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({
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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), '');
Expand Down
30 changes: 29 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

});