Skip to content

szy0syz/js-sequelize-adapter

Repository files navigation

Sequelize Adapter Build Status Coverage Status Godoc

Sequelize Adapter is the Sequelize adapter for Casbin. With this library, Casbin can load policy from Sequelize supported database or save policy to it.

Based on Officially Supported Databases, The current supported databases are:

  • MySQL
  • PostgreSQL
  • Sqlite3
  • Mssql

You may find other 3rd-party supported DBs in Sequelize website or other places.

Installation

npm install sequelize-adapter

Simple Example

const Adapter = require('sequelize-adapter')
const { Enforcer } = require('casbin')

module.exports = () => {
	// Initialize a Sequelize adapter and use it in a Casbin enforcer:
	// The adapter will use the MySQL database named "casbin".
	// If it doesn't exist, the adapter will create it automatically.
	
	const adapter = new Adapter('database', 'username', 'password', {host: 'localhost', dialect: 'mysql', port: 3306}) // Your driver and data source. 
	await adapter.init() // init your db connection and table
	const enforcer = await Enforcer.newEnforcer('rbac_model.conf', adapter)
	
	// Or you can use an existing DB "abc" like this:
	// The adapter will use the table named "casbin_rule".
	// If it doesn't exist, the adapter will create it automatically.
	// adapter = sequelizeadapter.newAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/abc", true)

	// Load the policy from DB.
	enforcer.loadPolicy()
	
	// Check the permission.
	enforcer.enforce('alice', 'data1', 'read')
	
	// Modify the policy.
	// e.addPolicy(...)
	// e.removePolicy(...)
	
	// Save the policy back to DB.
	enforcer.savePolicy()
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Releases

No releases published

Packages

No packages published