Registry / security / casbin-mongodb-adapter

casbin-mongodb-adapter

JSON →
library1.3.0jsnpmunverified

Pure MongoDB adapter for Casbin (node-casbin), version 1.3.0, actively maintained. Provides persistent policy storage using MongoDB collections. It supports loading, saving, and automatically synchronizing policies between Casbin enforcer and MongoDB. Requires peer dependencies casbin ^5.17.0 and mongodb ^4.9.0. Ships TypeScript declarations. Differentiator: lightweight, no ORM, direct MongoDB driver integration.

npm install casbin-mongodb-adapter
INSTALL
IMPORT
SIG · CASBIN-MONGODB-ADA
C
casbin-mongodb-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.

MongoAdapter
import { MongoAdapter } from 'casbin-mongodb-adapter'
const MongoAdapter = require('casbin-mongodb-adapter')
Package is ESM-only. Use named import.
MongoAdapter.newAdapter
const adapter = await MongoAdapter.newAdapter({...})
new MongoAdapter({...})
newAdapter is a static async factory method; direct constructor is not exported.
Enforcer
import { newEnforcer, Enforcer } from 'casbin'
import { newEnforcer } from 'casbin-mongodb-adapter'
Enforcer and newEnforcer come from casbin, not this adapter.

Demonstrates creating a Casbin enforcer with MongoDB adapter, loading policies, and enforcing an access rule.

import { newEnforcer } from 'casben'; import { MongoAdapter } from 'casben-mongodb-adapter'; import path from 'path'; async function main() { const model = path.resolve(__dirname, 'rbac_model.conf'); const adapter = await MongoAdapter.newAdapter({ uri: process.env.MONGO_URI ?? 'mongodb://localhost:27017', database: 'casben_test', collection: 'policy' }); const e = await newEnforcer(model, adapter); await e.loadPolicy(); console.log(await e.enforce('alice', 'data1', 'read')); } main();
Debug
Known issues
breakingv1.x requires casbin ^5.17.0 and mongodb ^4.x; older versions (v0.x) used casbin v2 and mongodb ^3.x.
fix
Ensure peer dependencies match: casbin ^5.17.0, mongodb ^4.9.0.
affects: >=1.0.0
deprecatedMongoAdapter.newAdapter's option field is deprecated; use direct mongodb connection options instead.
fix
Pass mongodb driver options directly in the config object (no separate option key).
affects: >=1.2.0
gotchaAdapter filters are not supported; filter adaptations must be handled at the Casbin enforcer level.
fix
Use Casbin's policy filtering mechanisms instead.
affects: *
gotchaThe adapter will create the collection if it does not exist; be aware of collection creation permissions.
fix
Ensure MongoDB user has permissions to create collections.
affects: *
Errors
Common errors & fixes
Error: Cannot find module 'casbin-mongodb-adapter'
Package not installed or import path wrong
fix
Run: npm install casbin-mongodb-adapter
TypeError: MongoAdapter.newAdapter is not a function
Wrong import (default vs named)
fix
Use: import { MongoAdapter } from 'casbin-mongodb-adapter'
MongoNotConnectedError
Missing or invalid MongoDB URI
fix
Provide valid uri in adapter config, e.g., mongodb://localhost:27017
Error: casbin v5 is required
Incompatible casbin version
fix
Install casbin ^5.17.0: npm install casbin@^5.17.0
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies
casbinrequiredpeer dependency - core authorization library
mongodbrequiredpeer dependency - MongoDB driver
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
1
Resources
casbin-mongodb-adapter — npm install casbin-mongodb-adapter · libregistry