npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

casbin-drizzle-adapter

v1.3.0

Published

Drizzle adapter for Casbin

Readme

Drizzle Adapter

This Adapter is based on Prisma Adapter

Drizzle Adapter is the Drizzle adapter for Node-Casbin. With this library, Node-Casbin can load policy from a Drizzle-supported database or save policy to it.

Based on Officially Supported Databases, the current supported databases are:

  • PostgreSQL — any driver (node-postgres, postgres.js, neon, vercel-postgres, PGlite, ...)
  • MySQL — any driver (mysql2, planetscale, ...)
  • SQLite — drivers with asynchronous transactions (libsql, D1, ...)

better-sqlite3 is not supported: it runs transaction callbacks synchronously and would not await this adapter's writes. The type signature rejects it.

Installation

pnpm add casbin-drizzle-adapter casbin drizzle-orm

casbin and drizzle-orm are peer dependencies, alongside whichever database driver you already use (pg, mysql2, @libsql/client, ...).

Getting Started

1. Define the table

The quickest way is the bundled helper, which also indexes the columns every lookup uses:

import { pgCasbinTable } from "casbin-drizzle-adapter/pg"

export const casbinTable = pgCasbinTable("casbin_rule")

casbin-drizzle-adapter/mysql and casbin-drizzle-adapter/sqlite export mysqlCasbinTable and sqliteCasbinTable for the other dialects.

Or define it yourself — the adapter accepts any table with the right shape:

import { pgTable, serial, varchar } from "drizzle-orm/pg-core"

// Only the property keys matter, never the SQL names: the table may be called
// anything and each column may map to any database column, but the keys ptype
// and v0..v5 must all be present. v0..v5 must be nullable — a rule shorter than
// six values is stored with its unused columns left NULL. Any other column (id
// here) is never written by the adapter, so it must be generated or nullable.
export const casbinTable = pgTable("casbin_rule", {
    id: serial("id").primaryKey().notNull(),
    ptype: varchar("ptype", { length: 254 }),
    v0: varchar("v0", { length: 254 }),
    v1: varchar("v1", { length: 254 }),
    v2: varchar("v2", { length: 254 }),
    v3: varchar("v3", { length: 254 }),
    v4: varchar("v4", { length: 254 }),
    v5: varchar("v5", { length: 254 }),
})

A table missing those columns, or declaring v0..v5 as NOT NULL, is a compile-time error — and still a constructor error for JavaScript callers.

2. Create the enforcer

import casbin from "casbin"
import { DrizzleAdapter } from "casbin-drizzle-adapter"
import { Pool } from "pg"
import { drizzle } from "drizzle-orm/node-postgres"
import { casbinTable } from "./your-table-schema"

async function main() {
    const pool = new Pool({
        connectionString: process.env.DATABASE_URL,
    })
    const d = drizzle(pool)

    const a = DrizzleAdapter.newAdapter(d, casbinTable)
    const e = await casbin.newEnforcer("examples/rbac_model.conf", a)

    // Check the permission.
    await e.enforce("alice", "data1", "read")

    // Modify the policy.
    // await e.addPolicy(...);
    // await e.removePolicy(...);

    // Save the policy back to DB.
    await e.savePolicy()
}

main()

Batched writes

addPolicies and removePolicies are implemented, so the enforcer's batch APIs work. Each validates every rule before writing any of them, runs in a single transaction, and chunks its statements to stay inside the database's bind parameter limit:

await e.addPolicies([
    ["alice", "data1", "read"],
    ["bob", "data2", "write"],
])

Large policy tables

loadPolicy reads the entire table into memory. Once that is too much, filter the load and casbin only ever sees the rules you asked for. The filter is pushed into SQL, not applied afterwards:

// p = sub, dom, obj, act  →  the tenant is v1
// g = _, _, _             →  the tenant is v2
await e.loadFilteredPolicy({ p: ["", "tenant_a"], g: ["", "", "tenant_a"] })

Each array holds the values a rule must have at v0, v1, ... in order. "" matches anything at that position, and a ptype the filter does not name is loaded in full — the same semantics as casbin's own filtered adapters.

There is no tenant column. A rule is only filterable by values it already stores, which is what casbin's domain models are for: put the tenant in the rule and it lands in a policy column. Note that it sits at a different index per ptype, which is why the filter is written per ptype.

Two things to know:

  • A filter narrower than your model needs is not an error. The missing rules never load, so enforce reports a deny rather than failing loudly.
  • savePolicy is refused after a filtered load. The model holds only the rules that filter matched, so writing it back would delete every rule outside the filter. Call loadPolicy first if you really mean to replace everything.

Use adapter.isFiltered() to check which state you are in.

Supported casbin interfaces

| Interface | Methods | | ------------------ | ------------------------------------------------------------- | | Adapter | loadPolicy, savePolicy, addPolicy, removePolicy, removeFilteredPolicy | | BatchAdapter | addPolicies, removePolicies | | FilteredAdapter | loadFilteredPolicy, isFiltered | | UpdatableAdapter | updatePolicy |

Getting Help

License

This project is under MIT. See the LICENSE file for the full license text.