hapi-waterline
v1.0.0
Published
Waterline (an adapter-based ORM for Node.js) as plugin for Hapi
Downloads
4
Readme
hapi-waterline
Waterline (an adapter-based ORM for Node.js) as a plugin for Hapi More details about waterline in original repository
Please consider (if required)
waterline-table for working with SQL and sails-postgres adapter
.
Usage
Install with npm (Hapi > 17.x):
npm i hapi-waterline --save
or for old version
npm i [email protected] --save
Register plugin in the hapi
server
var Hapi = require('hapi');
var server = new Hapi.Server();
var pluginOptions = {
adapters: { // adapters declaration
'mongo-adapter': require('sails-mongo')
},
datastores: {
mongoCon: { // datastores declaration
adapter: 'mongo-adapter',
user: '',
password: '',
host: 'localhost',
port: '27017',
database: 'TuiDev'
}
},
models: { // common models parameters, not override exist declaration inside models
datatstore: 'mongoCon',
migrate: 'alter'
},
decorateServer: true, // decorate server by method - getModel
path: ['../api/models', './common/models'] // string or array of strings with paths to folders with models declarations
};
server.register({
plugin: require('hapi-waterline'),
options: pluginOptions })
.then(() => {/*...do some stuff*/})
.catch((err) => console.log('error', 'Failed loading plugin: hapi-kea-config'));
});
// Usage in the code
var orm = server.plugins['hapi-waterline'];
var models = orm.models; // all registered types of models
var databases = orm.databases; // registered connections to databeses
var pet = models.pet;
// or
var pet = server.getModel('pet'); // applied by flag - decorateServer
Model example
// file loccated in ../api/models folder
module.exports = {
identity: 'pet',
attributes: {
primaryKey:'id',
identity: 'pet',
datastore: 'flat',
attributes: {
id: { type: 'number', autoMigrations: { autoIncrement: true } },
name: {type: 'string'},
breed: {type: 'string'}
}
}
};
##API
server.plugins['hapi-waterline']
Return object exposed by plugin
{
orm: {...}, // current exemplar waterline orm
models: {...}, // registered models
databases: {...} // registered database connections
}
server.getModel
Exten server
if decorateServer
flag is true
in plugin config
Parameters
model-indentity: string
, indentity value of model
Thanks
Big thank you for Mikael Lindahl for support Hapi 17.x