Cloudflare D1 driver for @rbac-ts/core.
Targets the Workers runtime — no ORM, no Node-only dependencies. Just the D1 binding API.
pnpm add @rbac-ts/core @rbac-ts/d1This package ships a migration file at migrations/0001_init.sql. With Wrangler:
# Copy or symlink to your project's migrations dir
mkdir -p migrations
cp node_modules/@rbac-ts/d1/migrations/0001_init.sql migrations/
wrangler d1 migrations apply <YOUR_DATABASE>Or apply once at runtime with the bundled DDL:
import { SCHEMA_SQL } from '@rbac-ts/d1/schema';
await env.DB.exec(SCHEMA_SQL);import { Rbac } from '@rbac-ts/core';
import { D1Driver } from '@rbac-ts/d1';
type Env = { DB: D1Database };
export default {
async fetch(req: Request, env: Env): Promise<Response> {
const rbac = new Rbac({ driver: new D1Driver(env.DB) });
await rbac.permissions.create({ name: 'articles.create' });
await rbac.roles.create({ name: 'editor', permissions: ['articles.create'] });
const subject = { type: 'User', key: 'u1' };
await rbac.for(subject).assignRole('editor');
const can = await rbac.for(subject).hasPermission('articles.create'); // true
return Response.json({ can });
},
};- Atomic sync operations (
syncRolePermissions,syncSubjectRoles,syncSubjectPermissions) usedb.batch([...]), which D1 executes as a single transaction. - Idempotent grants (
give*,assign*) useINSERT OR IGNORE, leaning on the composite primary keys on the pivot tables. - The driver does not import
@cloudflare/workers-types. TheD1Databaseparameter is structurally typed — any real D1 binding satisfies it. - Wrangler's
--localmode is supported transparently; the driver doesn't care which D1 implementation it's talking to.
MIT