Registry / security / casbin-basic-adapter

casbin-basic-adapter

JSON →
library1.3.0jsnpmunverified

A database adapter for Node-Casbin supporting PostgreSQL, MySQL, SQLite3, and MSSQL (Oracle listed but experimental). Current version 1.3.0, released sporadically. Key differentiator: provides a unified interface for Casbin policy persistence across multiple SQL databases, with automatic table creation and optional custom table naming. Unlike other Casbin adapters (e.g., sequelize, mongoose), this operates on raw SQL and requires database-specific client libraries as peer dependencies. Ships TypeScript types.

npm install casbin-basic-adapter
INSTALL
IMPORT
SIG · CASBIN-BASIC-ADAPT
C
casbin-basic-adapter
securityjavascriptv1.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BasicAdapter
import { BasicAdapter } from 'casbin-basic-adapter'
import BasicAdapter from 'casbin-basic-adapter'
ESM named export only. Default import is incorrect and will result in undefined.
BasicAdapter.newAdapter
const a = await BasicAdapter.newAdapter('pg', new pg.Client(...))
const a = await BasicAdapter.newAdapter({ type: 'pg', client: ... })
The first argument is a string for the database type, second is a client instance. Passing an object will fail.
BasicAdapter
const { BasicAdapter } = require('casbin-basic-adapter')
Also works with require() in CommonJS. The package provides dual ESM/CJS support.

Shows setting up a BasicAdapter with PostgreSQL client, creating an enforcer, and performing an access check.

import { newEnforcer } from 'casbin'; import { BasicAdapter } from 'casbin-basic-adapter'; import pg from 'pg'; async function main() { // Use environment variables for credentials const client = new pg.Client({ user: process.env.DB_USER ?? 'postgres', host: process.env.DB_HOST ?? 'localhost', database: process.env.DB_NAME ?? 'casbin', password: process.env.DB_PASSWORD ?? '', port: Number(process.env.DB_PORT ?? 5432), }); await client.connect(); const adapter = await BasicAdapter.newAdapter('pg', client); const enforcer = await newEnforcer('examples/rbac_model.conf', adapter); const allowed = await enforcer.enforce('alice', 'data1', 'read'); console.log('Allowed:', allowed); await client.end(); } main().catch(console.error);
Debug
Known issues
gotchaThe adapter expects a client instance, not a connection pool. Using a pool will not work as intended.
fix
Instantiate a single client (e.g., new pg.Client(...)) and pass it to newAdapter. Do not use Pool.
affects: >=1.0.0
gotchaAutomatic table creation is not always done. The adapter uses the table named 'casbin_rule' by default but will not create the database itself.
fix
Ensure the database exists before using the adapter. The adapter will create the table if it does not exist.
affects: >=1.0.0
breakingOracle support (oracledb) is marked as experimental and may not work reliably.
fix
Use PostgreSQL or MySQL instead of Oracle for production.
affects: >=1.3.0
gotchaTable name customization is supported as a third argument to newAdapter, but it is not well documented.
fix
Pass the desired table name as the third parameter: BasicAdapter.newAdapter('pg', client, 'custom_table')
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: BasicAdapter.newAdapter is not a function
Default import used instead of named import.
fix
Use import { BasicAdapter } from 'casbin-basic-adapter'
Error: Cannot find module 'pg'
Missing database-specific client module.
fix
Install the required database client: npm install pg
error: relation "casbin_rule" does not exist
Table not created because adapter does not auto-create in some cases or database not ready.
fix
Ensure the database is created and the adapter has permissions to create tables. Alternatively, manually create the table using the adapter's schema.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies
casbinrequiredPeer dependency: required for newEnforcer, model loading, and policy enforcement. Must be installed separately.
Agent activity
28 hits · last 30 days
node
26
Resources
casbin-basic-adapter — npm install casbin-basic-adapter · libregistry