What it does
MongoDB adapter stores permissions and roles in your MongoDB database. It works with the native mongodb driver or with mongoose if your app already uses it.
When to use it
Use MongoDB when:
- Your app already uses MongoDB
- You're using MongoDB Atlas
- You prefer document-based storage
- You want to avoid SQL entirely
How it works
- Install the driver: use
mongodb(native) or usemongooseif you already have it - Connect: permly doesn't manage the connection; you do
- Create adapter: pass your database to
mongodbAdapter() - Create collections: run
npx permly migrateto create collections and indexes - Use permly: all queries work through the adapter
With mongoose, pass mongoose itself and call await mongoose.connect() before using permly. With the native driver, pass client.db() and manage the connection yourself.
MongoDB collections are created if they don't exist. But indexes are not created automatically at startup—if they're missing, permly throws an error with the exact command to run.
Examples
With mongoose
// @typecheck-only: needs mongoose and real MongoDB database
import mongoose from "mongoose";
import { createPermissions } from "permly";
import { mongodbAdapter } from "permly/mongodb";
const perms = createPermissions({
adapter: mongodbAdapter(mongoose),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await mongoose.connect(process.env.DATABASE_URL || "");
await perms.sync();
await perms.user(1).assignRole("editor");
const allowed = await perms.user(1).can("posts.edit");
console.log(allowed); // true
// @typecheck-only: needs mongoose and real MongoDB database
import mongoose from "mongoose";
import { createPermissions } from "permly";
import { mongodbAdapter } from "permly/mongodb";
const perms = createPermissions({
adapter: mongodbAdapter(mongoose),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await mongoose.connect(process.env.DATABASE_URL ?? "");
await perms.sync();
const allowed: boolean = await perms.user(1).can("posts.edit");
With native driver
// @typecheck-only: needs mongodb driver and real MongoDB database
import { MongoClient } from "mongodb";
import { createPermissions } from "permly";
import { mongodbAdapter } from "permly/mongodb";
const client = new MongoClient(process.env.DATABASE_URL || "");
const perms = createPermissions({
adapter: mongodbAdapter(client.db()),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await perms.sync();
await perms.user(1).assignRole("editor");
const allowed = await perms.user(1).can("posts.edit");
console.log(allowed); // true
// @typecheck-only: needs mongodb driver and real MongoDB database
import { MongoClient } from "mongodb";
import { createPermissions } from "permly";
import { mongodbAdapter } from "permly/mongodb";
const client = new MongoClient(process.env.DATABASE_URL ?? "");
const perms = createPermissions({
adapter: mongodbAdapter(client.db()),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await perms.sync();
const allowed: boolean = await perms.user(1).can("posts.edit");
MongoDB Atlas
Use your mongodb+srv:// connection string with the database name in the path:
mongodb+srv://user:password@cluster.mongodb.net/mydb
The database name (mydb) is required. Your user needs readWrite permissions on that database, and your IP must be on the access list.
Standalone vs. replica sets
Standalone: permly uses lease locks (stored in the perm_locks collection) to sequence concurrent updates. They're checked and refreshed; expired locks are ignored.
Replica sets and sharded clusters: permly uses MongoDB transactions to ensure concurrent updates are safe. Changes are all-or-nothing.
Atlas: If you're on Atlas (even a single-node cluster), you're using a replica set. Transactions work automatically.
With standalone servers, concurrent calls to syncRoles() on the same user will use lease locks. One update runs at a time; others wait or fail with LOCK_TIMEOUT if they wait more than 10 seconds.
Migration and setup
Create collections and indexes with:
DATABASE_URL=mongodb://localhost:27017/mydb npx permly migrate
The command creates six collections: perm_permissions, perm_roles, perm_user_roles, perm_user_permissions, perm_role_permissions, and perm_locks.
Indexes are created for performance. If indexes are missing after migration, permly will tell you how to create them.
Common mistakes
Passing the client instead of the database — Use client.db(), not the client itself.
Using callbacks instead of async — MongoDB driver is async. Always use await.
Missing database name in Atlas URL — mongodb+srv://user:pass@cluster.mongodb.net/ needs the database name at the end.
Forgetting indexes — Run npx permly migrate to create them. Without indexes, queries are slow.
Assuming persistent connection — Connections can drop. Mongoose reconnects automatically; with the native driver, you manage it.
Related pages
- Installation — How to set up permly
- Quick Start — Your first permissions check
- MySQL & MariaDB — Use MySQL instead
- PostgreSQL — Use Postgres instead