A hapi plugin integrating Waterline ORM
var Hapi = require('hapi');
var Dogwater = require('dogwater');
var Memory = require('sails-memory');
var server = new Hapi.Server();
server.connection({ port: 3000 });
server.route({
method: 'GET',
path: '/dogs/{id}',
handler: function (request, reply) {
var Dogs = request.collections.dogs;
// reply with promise
reply(Dogs.findOne(request.params.id));
}
});
server.register({
register: Dogwater,
options: {
adapters: {
memory: Memory
},
connections: {
simple: { adapter: 'memory' },
},
models: [
{
identity: 'dogs',
connection: 'simple',
attributes: { name: 'string' }
}
],
fixtures: [
{
model: 'dogs',
items: [
{ name: 'Guinness' },
{ name: 'Sully' },
{ name: 'Ren' }
]
}
],
}
}, function (err) {
if (err) {
throw err;
}
server.start(function () {
console.log('Server running at:', server.info.uri);
});
});
server.waterline
- the raw Waterline ORM object.
request.collections
- an object containing all Waterline collections indexed by identity.
server.plugins.dogwater.collections
- an object containing all Waterline collections indexed by identity.server.plugins.dogwater.connections
- an object containing all Waterline connections indexed by name.server.plugins.dogwater.schema
- an object containing the normalized Waterline schema.server.plugins.dogwater.teardown(cb)
- a method to teardown all Waterline-managed connections,cb
taking a single error argument.
adapters
- An object whose keys are adapter names (to be referenced in theconnections
option), and whose values are Waterline adapter modules or string names of adapter modules to berequire
d.connections
- A object containing a Waterline connections configuration. Each key should be a connection name, and each value should be an object specifying the relevant adapter's name plus any adapter connection configurations.models
- Either a relative (to the current working directory) or absolute path to berequire
d that will return an array of unextended Waterline collections, or an array of unextended Waterline collections. If a function is an element of this array, it will be called with the raw Waterline ORM object as an argument. It is expected to return an unextended Waterline collection. This allows one to easily reference Waterline in lifecycle callbacks.fixtures
- An optional object containing the configuration used by waterline-fixtures to load data fixtures, with the exception of itscollections
option, which is automatically set by dogwater. Alternately, this option may specify an array of fixture data in the format prescribed by waterline-fixtures (which can be seen in the usage example above).
Dogwater's options aim to be friendly with rejoice manifests.