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

drizzle-adapter

v1.4.0

Published

Drizzle ORM adapter for Casbin

Readme

Drizzle Adapter

CI Coverage Status NPM version NPM download Discord

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

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

  • PostgreSQL
  • MySQL
  • SQLite
  • Turso
  • Neon
  • PlanetScale
  • Vercel Postgres
  • Xata
  • And more...

Installation

npm install drizzle-adapter
# or
yarn add drizzle-adapter

Simple Example

import { newEnforcer } from 'casbin';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';
import DrizzleAdapter, { casbinRuleSqlite } from 'drizzle-adapter';

async function myFunction() {
    // Initialize a SQLite database
    const sqlite = new Database('casbin.db');
    const db = drizzle(sqlite);
    
    // Create the casbin_rule table if it doesn't exist
    db.run(`
        CREATE TABLE IF NOT EXISTS casbin_rule (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            ptype TEXT,
            v0 TEXT,
            v1 TEXT,
            v2 TEXT,
            v3 TEXT,
            v4 TEXT,
            v5 TEXT
        )
    `);

    // Initialize a Drizzle adapter and use it in a Node-Casbin enforcer
    const a = await DrizzleAdapter.newAdapter({
        db: db,
        table: casbinRuleSqlite,
    });

    const e = await newEnforcer('examples/rbac_model.conf', a);

    // Load the policy from DB.
    await e.loadPolicy();

    // 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();
}

PostgreSQL Example

import { newEnforcer } from 'casbin';
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import DrizzleAdapter, { casbinRulePostgres } from 'drizzle-adapter';

async function myFunction() {
    // Initialize a PostgreSQL connection
    const pool = new Pool({
        host: 'localhost',
        port: 5432,
        user: 'postgres',
        password: 'password',
        database: 'casbin',
    });
    const db = drizzle(pool);

    // Initialize a Drizzle adapter
    const a = await DrizzleAdapter.newAdapter({
        db: db,
        table: casbinRulePostgres,
    });

    const e = await newEnforcer('examples/rbac_model.conf', a);

    // Load the policy from DB.
    await e.loadPolicy();

    // Check the permission.
    await e.enforce('alice', 'data1', 'read');
}

MySQL Example

import { newEnforcer } from 'casbin';
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import DrizzleAdapter, { casbinRuleMysql } from 'drizzle-adapter';

async function myFunction() {
    // Initialize a MySQL connection
    const connection = await mysql.createConnection({
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: 'password',
        database: 'casbin',
    });
    const db = drizzle(connection);

    // Initialize a Drizzle adapter
    const a = await DrizzleAdapter.newAdapter({
        db: db,
        table: casbinRuleMysql,
    });

    const e = await newEnforcer('examples/rbac_model.conf', a);

    // Load the policy from DB.
    await e.loadPolicy();

    // Check the permission.
    await e.enforce('alice', 'data1', 'read');
}

Filtered Policy Example

import { newEnforcer } from 'casbin';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';
import DrizzleAdapter, { casbinRuleSqlite } from 'drizzle-adapter';

async function myFunction() {
    const sqlite = new Database('casbin.db');
    const db = drizzle(sqlite);

    const a = await DrizzleAdapter.newAdapter({
        db: db,
        table: casbinRuleSqlite,
    });

    const e = await newEnforcer('examples/rbac_model.conf', a);

    // Load the filtered policy from DB.
    await e.loadFilteredPolicy({
        'ptype': 'p',
        'v0': 'alice'
    });

    // Check the permission.
    await e.enforce('alice', 'data1', 'read');
}

Custom Table Schema

You can create a custom table schema if you need additional fields or different column names:

import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
import DrizzleAdapter from 'drizzle-adapter';

// Define a custom table schema
const customCasbinRule = sqliteTable('my_casbin_rules', {
    id: integer('id').primaryKey({ autoIncrement: true }),
    ptype: text('ptype'),
    v0: text('v0'),
    v1: text('v1'),
    v2: text('v2'),
    v3: text('v3'),
    v4: text('v4'),
    v5: text('v5'),
});

async function myFunction() {
    const sqlite = new Database('casbin.db');
    const db = drizzle(sqlite);

    const a = await DrizzleAdapter.newAdapter({
        db: db,
        table: customCasbinRule,
    });

    const e = await newEnforcer('examples/rbac_model.conf', a);
    await e.loadPolicy();
}

API Reference

DrizzleAdapter

newAdapter(options: DrizzleAdapterOptions): Promise<DrizzleAdapter>

Creates a new Drizzle adapter instance.

Options:

  • db: Drizzle database instance
  • table: (Optional) Drizzle table schema. Defaults to casbinRuleSqlite

loadPolicy(model: Model): Promise<void>

Loads all policy rules from the database.

loadFilteredPolicy(model: Model, filter: FilterOptions): Promise<void>

Loads policy rules that match the filter from the database.

FilterOptions:

  • ptype: Policy type (e.g., 'p', 'g')
  • v0 to v5: Policy values

savePolicy(model: Model): Promise<boolean>

Saves all policy rules to the database (clears existing policies first).

addPolicy(sec: string, ptype: string, rule: string[]): Promise<void>

Adds a policy rule to the database.

addPolicies(sec: string, ptype: string, rules: string[][]): Promise<void>

Adds multiple policy rules to the database.

removePolicy(sec: string, ptype: string, rule: string[]): Promise<void>

Removes a policy rule from the database.

removePolicies(sec: string, ptype: string, rules: string[][]): Promise<void>

Removes multiple policy rules from the database.

removeFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<void>

Removes policy rules that match the filter from the database.

updatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<void>

Updates a policy rule in the database.

isFiltered(): boolean

Returns whether the loaded policy has been filtered.

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.