https://github.com/willsoto/casbin-objection-adapter
https://github.com/willsoto/casbin-objection-adapter
casbin casbin-adapter knex objectionjs
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/willsoto/casbin-objection-adapter
- Owner: willsoto
- License: apache-2.0
- Created: 2020-05-08T18:49:38.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-21T00:05:56.000Z (over 1 year ago)
- Last Synced: 2024-12-01T13:08:59.887Z (10 months ago)
- Topics: casbin, casbin-adapter, knex, objectionjs
- Language: TypeScript
- Homepage:
- Size: 3.13 MB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Casbin Objection Adapter

- [Installation](#installation)
- [Basic usage](#basic-usage)
- [Advanced usage](#advanced-usage)## Installation
```bash
npm install @willsoto/casbin-objection-adapter --save
``````bash
yarn add @willsoto/casbin-objection-adapter
``````bash
pnpm add @willsoto/casbin-objection-adapter
```## Basic usage
See [the Casbin adapters documentation](https://casbin.org/docs/en/adapters) for more information.
```js
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` |