Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/fajarnugraha37/drizzle-castor/llms.txt

Use this file to discover all available pages before exploring further.

Drizzle Castor is a type-safe CRUD library and Data Access Object (DAO) abstraction built on top of Drizzle ORM. It lets you query your database using intuitive JSON payloads, enforce access control through a built-in RBAC engine, intercept every operation via a Koa-style middleware pipeline, and run across PostgreSQL, MySQL, and SQLite without changing your application code.

Quickstart

Install Drizzle Castor and run your first type-safe query in under five minutes.

Installation

Install from npm or JSR across all major package managers and runtimes.

Schema Builder

Learn how to configure tables, relations, middleware, and policies with the builder API.

API Reference

Explore every public method, parameter type, and return value.

What Drizzle Castor provides

JSON-Based Querying

Filter, sort, and project relational data with expressive JSON payloads. The AST translator compiles them to dialect-correct Drizzle SQL automatically.

Unified RBAC

Enforce action-level and field-level access control at the repository layer — no scattered auth checks across your service code.

Middleware Pipeline

Intercept every database operation with Koa-style (ctx, next) middleware. Add logging, tracing, caching, or multi-tenancy as composable layers.

Multi-Dialect Support

Works across PostgreSQL, MySQL, and SQLite. Handles dialect-specific details like RETURNING clauses and temporary table fallbacks transparently.

Soft Deletes

Declare soft-delete behavior once on your table config. Safety filters are injected automatically into every query and join.

Safe Pagination

Paginate one-to-many relationships accurately using CTE Split Queries — no Cartesian fan-out, no off-by-one row counts.

Get started

1

Install the package

Add Drizzle Castor alongside Drizzle ORM to your project.
bun add @fajarnugraha37/drizzle-castor drizzle-orm
2

Initialize the schema builder

Pass your Drizzle database instance and table definitions to createSchemaBuilder.
import { drizzle } from "drizzle-orm/bun-sqlite";
import { createSchemaBuilder } from "@fajarnugraha37/drizzle-castor";
import { usersTable } from "./schema";

const db = drizzle("sqlite.db");

const builder = createSchemaBuilder(db, [usersTable] as const, "strict")
  .profiles(["admin", "user"] as const)
  .withLogger({ level: "INFO", format: "%d [%p] (%t) %s" });
3

Create a repository and query

Build the metadata and use repoFactory to get a fully typed repository.
const metadata = builder.build();
const userRepo = metadata.repoFactory("users");

const users = await userRepo.searchMany({
  filter: { "name": { $like: "%John%" } },
  order: { createdAt: "desc" }
}, "admin");
Drizzle Castor requires Drizzle ORM ^0.45.2 and TypeScript ^5. It runs on Bun 1.0+ and Node.js.

Build docs developers (and LLMs) love