Skip to content

knex/casbin-knex-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f6c60a9 · Sep 17, 2021

History

34 Commits
Sep 17, 2021
Jul 13, 2021
Jan 16, 2021
Jul 13, 2021
Jan 16, 2021
Jan 17, 2021
Jan 17, 2021
Apr 14, 2019
Apr 14, 2019
Feb 10, 2021
Feb 10, 2021
Apr 4, 2021
Sep 17, 2021
Feb 10, 2021

Repository files navigation

Knex Adapter

NPM Version Coverage Status

Knex Adapter for Node-Casbin. Use this library for policy storage in Casbin.

For full database support list, go to the Knex documentation.

Installation

npm install casbin-knex-adapter --save

or

yarn add casbin-knex-adapter

Example

const Knex = require('knex')
const casbin = require('casbin');
const KnexAdapter = require('casbin-knex-adapter');

(async function() {
  // Instantiate DB connection
  const knex = Knex(knexOptions)
  // Create adapter
  const adapter = await KnexAdapter.newAdapter({ knex });

  // Create casbin enforcer
  const enforcer = await casbin.newEnforcer('model.conf', adapter);

  // Load policy from DB
  await enforcer.loadPolicy();

  // Check permission
  if (await enforcer.enforce('user', 'resource', 'read')) {
    // Do something if user is authorized
  }

  // Modify policy
  // await enforcer.addPolicy(...)
  // await enforcer.removePolicy(...)
  // await adapter.removePolicyWhere({ 'v0': '00001' }) // needs to be reloaded from enforcer afterwards

  // Rewrite entire policy in DB
  await enforcer.savePolicy();
})();