acl-sequelize-backend
v0.0.7
Published
A Sequelize.js backend for node_acl
Downloads
4
Maintainers
Readme
ACL Sequelize
node_acl implementation for Sequelize Currently working only with Postgres.
Installation
npm install sequelize
npm install pg
npm install acl
npm install acl-sequelize-backend
Setup
var NodeAcl = require('acl');
var Sequelize = require('Sequelize');
var SequelizeBackend = require('acl-sequelize-backend');
var db = new Sequelize('postgres://postgres:[email protected]:5432/myawesomedb');
var aclOptions = {};
var tablePrefix = 'acl_';
var acl = new NodeAcl(new SequelizeBackend(db, tablePrefix, aclOptions));
API
new SequelizeBackend(db, prefix, options)
Arguments
db {Function} Sequelize db instance.
prefix {String} Prefix for generated tables
options {Object} Options provided to the backend
Options can contain these fields:
Note: These are also the defaults if nothing is provided.
var options = {
meta: 'meta', // Table name for meta bucket
parents: 'parents', // Table name for parents bucket
permissions: 'permissions', // Table name for permissions bucket
resources: 'resources', // Table name for resources bucket
roles: 'roles', // Table name for roles bucket
users: 'users', // Table name for users bucket
sync: true, // Should tables be automatically created using sequelize sync method
debug: false // Enable debug (shows sql on sync if enabled)
};
Migrations
If you want to use migrations to generate tables instead of automatic sync, You can pull them from SequelizeBackend like this:
First, make sure you disable automatic sync when initializing acl:
var acl = new NodeAcl(new SequelizeBackend(db, 'acl_', {
sync: false
}));
Then generate migration file and add this to it:
// migrations/add_acl.js
var SequelizeBackend = require('acl-sequelize-backend');
module.exports = {
up: function (queryInterface, Sequelize) {
var options = { prefix: 'acl_' };
return SequelizeBackend.migration.up(queryInterface, Sequelize, options);
},
down: function (queryInterface, Sequelize) {
var options = { prefix: 'acl_' };
return SequelizeBackend.migration.down(queryInterface, Sequelize, options);
}
};
Both up
and down
methods accept 3rd parameter options
which can contain table names for each bucket
var options = {
prefix: '', // Prefix for table names
meta: 'meta', // Table name for meta bucket
parents: 'parents', // Table name for parents bucket
permissions: 'permissions', // Table name for permissions bucket
resources: 'resources', // Table name for resources bucket
roles: 'roles', // Table name for roles bucket
users: 'users' // Table name for users bucket
};
NOTE: Make sure that table names and prefix provided here match with table names and prefix provided to the backend.
It is recommended to just use defaults in order to avoid any issues with naming.
Tests
npm test