@willsoto/casbin-objection-adapter
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Casbin Objection Adapter

tests

Installation

npm install @willsoto/casbin-objection-adapter --save
yarn add @willsoto/casbin-objection-adapter
pnpm add @willsoto/casbin-objection-adapter

Basic usage

See the Casbin adapters documentation for more information.

import { Knex } from "knex";
import { newEnforcer } from "casbin";
import { ObjectionAdapter } from "@willsoto/casbin-objection-adapter";

const knex = Knex({
  /* regular knex options */
});

// All configuration is optional
const adapter = await ObjectionAdapter.newAdapter(knex, {});

// Create the enforcer with the given model
const enforcer = await newEnforcer("basic_model.conf", adapter);

// Supports auto-save
// See: https://casbin.org/docs/en/adapters#autosave
enforcer.enableAutoSave(true);

// No need to save explicitly since auto-save is enabled
await enforcer.addPolicies([
  ["alice", "data1", "read"],
  ["bob", "data2", "write"],
]);

await enforcer.enforce("alice", "data1", "read"); // true
await enforcer.enforce("bob", "data1", "read"); // false

Advanced usage

The following options are available:

Option Default value Description
createTable true Whether or not to create the table when initialized.
modelClass CasbinRule The model to use when querying policies. You can override this if you would like to control the table name
logger noop An optional logger in case additional visiblity is needed into the adapter. The inteface should match console

Package Sidebar

Install

npm i @willsoto/casbin-objection-adapter

Weekly Downloads

2

Version

3.0.0

License

Apache-2.0

Unpacked Size

50.1 kB

Total Files

23

Last publish

Collaborators

  • paradox41