rbac-library (1.0.1)
Published 2026-01-27 15:22:13 +00:00 by otmatas
Installation
registry=npm install rbac-library@1.0.1"rbac-library": "1.0.1"About this package
@otmatas/rbac
A TypeScript Role-Based Access Control (RBAC) plugin for ElysiaJS with DrizzleORM and PostgreSQL.
Features
- 🔒 Easy Integration - Simple one-line setup with ElysiaJS
- ⚡ High Performance - LRU cache with 30-second TTL
- 🔄 Auto Permissions - Automatically populates permissions from routes
- 📊 Multiple Roles - Users can have multiple roles
- 🎯 Flexible - Roles can have multiple permissions
- 🗃️ PostgreSQL - Uses DrizzleORM for database operations
Installation
From Git (Bun)
bun add git+https://git.otmatas.com/otmatas/rbac-library.git
From Git (npm/pnpm)
npm install git+https://git.otmatas.com/otmatas/rbac-library.git
# or
pnpm add git+https://git.otmatas.com/otmatas/rbac-library.git
Quick Start
import { Elysia } from "elysia";
import { rbac } from "rbac-library";
const app = new Elysia()
.use(await rbac({
connectionString: "postgres://user:pass@localhost:5432/mydb",
getUserId: (ctx) => ctx.headers["x-user-id"] || null,
excludePaths: ["/health", "/auth/*"],
}))
.get("/health", () => "OK")
.get("/api/users", () => "Users list")
.post("/api/users", () => "Create user")
.listen(3000);
console.log("Server running on http://localhost:3000");
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
connectionString |
string |
required | PostgreSQL connection string |
getUserId |
(ctx) => string | null |
required | Extract user ID from request |
excludePaths |
string[] |
[] |
Paths to skip RBAC checks (supports wildcards) |
autoPopulate |
boolean |
true |
Auto-populate permissions from routes |
cacheTTL |
number |
30000 |
Cache TTL in milliseconds |
Database Tables
The package automatically creates these tables on initialization:
rbac_users- User accountsrbac_roles- Role definitionsrbac_permissions- Permission definitionsrbac_paths- Registered paths/routesrbac_user_roles- User-Role assignmentsrbac_role_permissions- Role-Permission assignmentsrbac_path_permissions- Path-Permission requirements
Managing Roles & Permissions
import {
createRole,
createPermission,
createUser,
assignRoleToUser,
assignPermissionToRole,
assignPermissionToPath,
getOrCreatePath,
getRBACDatabase,
} from "rbac-library";
const db = getRBACDatabase();
// Create a role
const adminRoleId = await createRole(db, "admin", "Administrator role");
// Create a permission
const permissionId = await createPermission(db, "users:read", "Read users");
// Assign permission to role
await assignPermissionToRole(db, adminRoleId, permissionId);
// Create user and assign role
const userId = await createUser(db, "admin@example.com", "hashedPassword");
await assignRoleToUser(db, userId, adminRoleId);
// Link permission to a path
const pathId = await getOrCreatePath(db, "GET", "/api/users");
await assignPermissionToPath(db, pathId, permissionId);
Cache Management
import { clearCache, invalidateUserCache, invalidatePathCache } from "rbac-library";
// Clear all cached permissions
clearCache();
// Invalidate cache for a specific user (after role change)
invalidateUserCache("user-id");
// Invalidate cache for a specific path
invalidatePathCache("GET", "/api/users");
Using the Schema Directly
import { schema } from "rbac-library";
// Use with your own Drizzle queries
const allUsers = await db.select().from(schema.users);
Testing
bun test
License
MIT
Repository
Dependencies
Dependencies
| ID | Version |
|---|---|
| drizzle-orm | ^0.45.1 |
| elysia | ^1.4.22 |
| lru-cache | ^11.2.5 |
| postgres | ^3.4.8 |
Development dependencies
| ID | Version |
|---|---|
| @types/bun | ^1.3.6 |
| drizzle-kit | ^0.31.8 |
Peer dependencies
| ID | Version |
|---|---|
| elysia | >=1.0.0 |
| typescript | ^5 |
Keywords
rbac
role-based-access-control
elysia
elysiajs
drizzle
drizzle-orm
postgres
authorization
permissions
bun
Details
2026-01-27 15:22:13 +00:00
Assets (1)
Versions (3)
View all
npm
1
otmatas
MIT
75 KiB
rbac-library-1.0.1.tgz
75 KiB