Skip to content

mlaccetti/ravel-sequelize-provider

Repository files navigation

ravel-sequelize-provider

Ravel Sequelize Provider

GitHub license npm version Dependency Status npm Build Status Code Climate Test Coverage

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).

Example usage:

Step 1: Import and instantiate the SequelizeProvider

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();

Step 2: Access models via app.getModel

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;
    });
  }
}

Step 3: Configuration

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"
  }
}