Skip to content

@vperms/mongodb-adapter

import { MongoClient } from "mongodb";
import { VeguiPermsMongoDBAdapter } from "@vperms/mongodb-adapter";
const client = new MongoClient(process.env.MONGODB_URI!);
await client.connect();
const adapter = new VeguiPermsMongoDBAdapter({ db: client.db("app") });
await adapter.migrate();
interface VeguiPermsMongoDBAdapterOptions {
db: Db;
subjectsCollection?: string; // default "vperms_subjects"
grantsCollection?: string; // default "vperms_grants"
}
class VeguiPermsMongoDBAdapter extends VeguiPermsAdapter {
constructor(options: VeguiPermsMongoDBAdapterOptions);
migrate(): Promise<void>;
}

migrate() idempotently creates both collections and their unique indexes:

  • subjects: { workspaceId: 1, id: 1 }
  • grants: { workspaceId: 1, subjectId: 1, permission: 1 }

The adapter never opens or closes the MongoDB connection; pass a Db from a client you manage.

const SUBJECTS_COLLECTION = "vperms_subjects";
const GRANTS_COLLECTION = "vperms_grants";
interface SubjectDocument {
workspaceId: string;
id: string;
type: SubjectType;
parents: string[];
}
interface GrantDocument {
workspaceId: string;
subjectId: string;
permission: string;
value: boolean;
}