Ravel Sequelize Provider
ravel-sequelize-provider
is a DatabaseProvider
for Ravel, wrapping the powerful node sequelize library. It supports connection pooling as well as Ravel's transaction system (including rollbacks).
app.js
const app = new require('ravel')();
const SequelizeProvider = require('ravel-sequelize-provider');
new SequelizeProvider(app);
// load all model files
app.models('./models');
// ... other providers and parameters
app.modules('./modules');
app.resources('./resources');
// ... the rest of your Ravel app
app.init();
app.listen();
resources/posts_resource.js
const Ravel = require('ravel');
const Resource = Ravel.Resource;
const transaction = Resource.transaction;
class PostsResource extends Resource {
constructor() {
super('/posts');
}
/**
* Retrieve a single post
*/
get(ctx) {
// Best practice is to pass the transaction object through to a Module, where you handle the actual business logic.
return this.app.getModel('posts').findAll().then((posts) => {
ctx.body = posts;
});
}
}
Requiring the ravel-sequelize-provider
module will register a configuration parameter with Ravel which must be supplied via .ravelrc
or app.set()
:
.ravelrc
{
"sequelize options": {
"dialect": "postgres",
"host": "localhost",
"port": 5432,
"username": "",
"password": "",
"database": "ravel"
}
}