Registry / security / mongodb-casbin-adapter

mongodb-casbin-adapter

JSON →
library2.0.0jsnpmunverified

MongoDB adapter for Node Casbin (v2.0.0, 2024) providing policy storage and retrieval using MongoDB. Implements FilteredAdapter, BatchAdapter, and UpdatableAdapter interfaces, with automatic index creation and timestamp support. Differentiators include support for filtered policies, batch updates, and manual save control. Compatible with casbin v5.x. Active development with Apache 2.0/MIT licensing.

npm install mongodb-casbin-adapter
INSTALL
IMPORT
SIG · MONGODB-CASBIN-ADA
M
mongodb-casbin-adapter
securityjavascriptv2.0.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 'mongodb-casbin-adapter'
const MongoAdapter = require('mongodb-casbin-adapter')
ESM only since v2; named import required.
MongoAdapter.newAdapter
const adapter = await MongoAdapter.newAdapter({ uri, database, collection })
const adapter = new MongoAdapter({ uri, database, collection })
Constructor is private; use static factory method newAdapter.
FilteredAdapter
import { FilteredAdapter } from 'casbin'
import { FilteredAdapter } from 'mongodb-casbin-adapter'
FilteredAdapter is a casbin interface, not exported by this adapter.

Initializes a Casbin enforcer with MongoDB adapter, enforces a simple policy, and closes the adapter.

import { newEnforcer } from 'casbin'; import { MongoAdapter } from 'mongodb-casbin-adapter'; async function setup() { const adapter = await MongoAdapter.newAdapter({ uri: process.env.MONGO_URI ?? 'mongodb://localhost:27017', database: 'casbin', collection: 'policies', dropCollectionOnManualSave: false, }); const enforcer = await newEnforcer('model.conf', adapter); const allowed = await enforcer.enforce('alice', 'data1', 'read'); console.log(allowed ? 'Allow' : 'Deny'); await adapter.close(); } setup().catch(console.error);
Debug
Known issues
breakingv2 drops CommonJS support; only ESM imports are allowed.
fix
Update imports to use ES module syntax: import { MongoAdapter } from 'mongodb-casbin-adapter'.
affects: >=2.0.0
breakingnewAdapter() now returns a Promise. Direct constructor not accessible.
fix
Replace new MongoAdapter(...) with await MongoAdapter.newAdapter(...).
affects: >=2.0.0
gotchadropCollectionOnManualSave defaults to false, but if set to true, calling savePolicy() drops the collection entirely.
fix
Set dropCollectionOnManualSave: false unless you want to purge all policies on save.
affects: >=2.0.0
deprecatedThe older v1 adapter used require('mongodb-casbin-adapter').MongoAdapter which is no longer supported.
fix
Upgrade to v2 and switch to ESM import.
affects: <2.0.0
gotchaMongoDB connection options must be passed inside the options object; not directly on the adapter config.
fix
Use { uri, database, collection, options: { ... } } where options contains MongoDB client options.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: MongoAdapter is not a constructor
Attempting to instantiate MongoAdapter with new instead of using static factory method.
fix
Use await MongoAdapter.newAdapter(config) instead of new MongoAdapter(config).
Error: Cannot find module 'mongodb-casbin-adapter'
Package not installed or used in CommonJS context while adapter is ESM-only.
fix
Install with npm install mongodb-casbin-adapter and use import syntax in an ESM environment.
TypeError: adapter.close is not a function
Calling close on the enforcer instead of the adapter.
fix
Call adapter.close() to close the MongoDB connection, not enforcer.close().
MongoError: collection name must be a non-empty string
Missing or empty collection name in adapter config.
fix
Provide a valid collection string: { collection: 'policies' }
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
casbinrequiredpeer dependency required for enforcer integration
mongodbrequiredruntime dependency for MongoDB client
Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
1
Resources
mongodb-casbin-adapter — npm install mongodb-casbin-adapter · libregistry