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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ server.register([
- ```plugins``` [Bookshelf.js Plugins](http://bookshelfjs.org/#Plugins) the _registry_ plugin is required
- ```models``` directory where you Bookshelf.js models are defined
- ```base``` (optional) function that applies the Bookshelf.js [extend method](http://bookshelfjs.org/#Model-extend) and returns the extended model, example below.
- ```namespace``` (optional) string that will control how models are exposed in the plugin. The intent is to support models coming from multiple knex connections or to logically segment models. Models can be accessed via `server.plugins.bookshelf.{namespace}.model('ModelName')`

### Example ```base```
```javascript
Expand Down Expand Up @@ -80,3 +81,42 @@ After loading these models you can access them via ```server.plugins.bookshelf.m

### Notes:
- Models will be registered and made available under the file name with the first character capitalized. For example ```user.js``` becomes ```User``` and ```blogPost.js``` becomes ```BlogPost```

# Registering Multiple Namespaces
Modeling namespaces is a great way to expose models from multiple databases using the same interface. Below is an example of how you can do this.

```javascript
var Hapi = require('hapi');

var server = new Hapi.Server();

server.register([
{
register: require('hapi-bookshelf-models'),
options: {
knex: {
// connection one details
},
plugins: ['registry'],
models: '../path/to/namespaceone/models/directory',
namespace: 'namespaceone'
}
},
{
register: require('hapi-bookshelf-models'),
options: {
knex: {
//connection two details
},
plugins: ['registry'],
models: '../path/to/namespacetwo/models/directory',
namespace: 'namespacetwo'
}
}
], function (err) {
// An error will be available here if anything goes wrong
});

// You can access the namespaceone models via server.plugins.bookshelf.namespaceone.model('ModelName')
// You can access the namespacetwo models via server.plugins.bookshelf.namespacetwo.model('ModelName')
```
12 changes: 9 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ exports.register = function (server, options, next) {
knex: Joi.object().required(),
plugins: Joi.array().includes(Joi.string()).default([]),
models: Joi.string().required(),
base: Joi.func().optional()
base: Joi.func().optional(),
namespace: Joi.string().optional()
};

try {
Expand Down Expand Up @@ -42,11 +43,16 @@ exports.register = function (server, options, next) {
require(path.join(options.models, model))(baseModel));
});

server.expose(bookshelf);
if (options.namespace) {
server.expose(options.namespace, bookshelf);
} else {
server.expose(bookshelf);
}
next();
};

exports.register.attributes = {
name: 'bookshelf',
version: '1.0.0'
version: '1.0.0',
multiple: true
};
8 changes: 6 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ describe('bookshelf plugin', function () {
}
},
plugins: ['registry'],
models: path.join(__dirname + '/models')
models: path.join(__dirname + '/models'),
namespace: 'test'
}
}
], function (err) {
expect(err).to.be.undefined;
expect(server.plugins.bookshelf.model('User')).to.be.a('function');
expect(server.plugins.bookshelf.test.model('User')).to.be.a('function');
});
});

it('should allow namespacing for multiple registrations', function () {

});
});