NestJS Drizzle CRUD library
nestjs-drizzle-crud is a NestJS Drizzle library. Extend one base class per table and get typed CRUD, filters, pagination and relations.
nestjs-drizzle-crud is a NestJS Drizzle library: you extend one base class per table and get a
typed CRUD service on top of Drizzle ORM. You configure the connection once, bind each service to
its Drizzle table, and every service can find, list, filter, paginate, create, update, soft delete
and bulk write rows on PostgreSQL, MySQL or SQLite. It does not hide Drizzle: your schema stays yours.
Who this is for, and what you need
NestJS developers who use Drizzle ORM, or want to, and are tired of writing the same findAll,
create and update code in every service. You need Node 20 or later, @nestjs/common and
@nestjs/core 10 or later, drizzle-orm from 0.28.0 up to (not including) 1.0.0, and
reflect-metadata 0.1.13 or 0.2. Add postgres 3 or mysql2 3 if you want the module to
open the PostgreSQL or MySQL connection for you.
What does it do?
One empty service class gives you the whole read and write surface, and configuration turns on the rest:
- Reads.
find,findOne,findAllwith a{ data, total, page, limit }envelope,countandexists. See CRUD operations. - Filtering. Column operators (
gte,in,between,iContainsand more),and,orandnotgroups, and filters on related tables. See Filtering and Relation filters. - Pagination. Offset pages with a capped limit, multi-column sorts, and keyset pagination
through
findByCursor. See Pagination and sorting and Cursor pagination. - Relations. Belongs-to, has-many and many-to-many, eager loaded with one batched query per relation instead of one per row. See Relations.
- Writes.
create,update,upsert, soft delete, restore, and mass writes that run in one transaction. See Upsert, Bulk operations and Soft delete. - Safety. Column allowlists, tenant scopes, optimistic locking, and exceptions that map to 404, 409 and 400 without a custom filter. See Allowlists, Multi-tenancy and Errors.
- Wiring. A generated controller, OpenAPI decorators, test helpers and an MCP server for coding agents. See Controller factory and AI agents.
What does the journey look like?
The sidebar follows the order you will need things in. Each section is useful on its own, so stop when you have what you came for.
Start here
Install the package and its peers, wire one table end to end, then learn the methods every service inherits.
Core concepts
How a service gets its config, how relations are declared, and what soft delete, timestamps and primary key types change.
Querying
Filters, relation filters, turning a query string into filters, sorting, offset and cursor pages, column selection, full-text search and grouped counts.
Writing data
Upsert, bulk writes, nested creates and links, optimistic locking, transactions and row locks.
Securing it
Limit what callers may filter, sort and write, scope every query to a tenant, validate input and react to writes.
Put it in your stack
Generated controllers, OpenAPI, async config, per-entity config, tests and coding agents.
Compare
How it stacks up against @nestjsx/crud, nestjs-paginate, TypeORM and Prisma, and how to move
over.
Compared with nestjsx/crud · Migrate from nestjsx/crud · Drizzle, TypeORM or Prisma
Reference
Every method signature, config type and export in one place.
Ship it
What changed in each release, where to ask for help, and how to support the project.
How do I install it?
pnpm add @avytrix/nestjs-drizzle-crud @nestjs/common @nestjs/core drizzle-orm reflect-metadataThen add a driver: postgres for PostgreSQL, mysql2 for MySQL, or a Drizzle instance you build yourself, as on SQLite.
Install NestJS with Drizzle ORM covers which one you need and why.
What does a service look like?
A service is an empty class that names its row, DTO and filter types. forFeature binds it to a
table and builds it for you, so there is no constructor and no @Injectable().
import { type CrudFilter, SqlBaseCrudService } from "@avytrix/nestjs-drizzle-crud";
import type { users } from "../db/schema";
import type { CreateUserDto, UpdateUserDto } from "./users.dto";
export type User = typeof users.$inferSelect;
export class UsersService extends SqlBaseCrudService<
User,
CreateUserDto,
UpdateUserDto,
CrudFilter
> {}import { Module } from "@nestjs/common";
import { DrizzleCrudModule } from "@avytrix/nestjs-drizzle-crud";
import { users } from "../db/schema";
import { UsersController } from "./users.controller";
import { UsersService } from "./users.service";
@Module({
imports: [
DrizzleCrudModule.forFeature([{ service: UsersService, table: users }]),
],
controllers: [UsersController],
})
export class UsersModule {}UsersService now has every method in CRUD operations, typed against
User. The quick start builds the rest of the app around it.
Facts at a glance
| Fact | Value |
|---|---|
| Databases | PostgreSQL, MySQL and SQLite, libSQL and Cloudflare D1 (dialect: "postgresql", "mysql" or "sqlite") |
| Documented version | 3.8.3 |
| License | MIT |
| Runtime dependencies | None. Peer dependencies only, three of them optional (postgres, mysql2, @nestjs/swagger) |
| Node | 20 or later |
| Source | GitHub, published on npm |
When is it the wrong tool?
- Most of your endpoints are reports. Window functions, CTEs and hand-tuned joins read better as plain Drizzle. You can still add those as extra methods on the same service.
- Your writes are workflows. Multi-step domain operations belong in your own service code. Use the package for the plain entities around them.
- You need another dialect. The
dialectsetting accepts"postgresql","mysql"and"sqlite"only, so SQL Server, Oracle and MongoDB are out.
Common questions
No. It builds Drizzle queries from your own Drizzle tables. You keep your schema and migrations, and you can inject DRIZZLE_DB to run any Drizzle query yourself.
PostgreSQL, MySQL and SQLite (including libSQL and Cloudflare D1). Full-text search is PostgreSQL only. The module builds the connection from a connectionString on PostgreSQL and MySQL; on SQLite you pass a Drizzle instance you built yourself. On D1, bulk writes are not available.
No. createCrudController generates the list, get, create, update, delete and bulk routes for a service. You can also write a normal NestJS controller and call the service methods yourself.
No. A belongs-to relation is joined into the main query, and has-many and many-to-many relations load with one batched query per relation for the whole result, whatever the row count.
Yes. The package is MIT licensed and has no runtime dependencies of its own.
The library is built and maintained by Avytrix Technologies.
Next steps
Quick start
From an empty NestJS project to working CRUD endpoints for one table.
Installation
The package, its peer dependencies and the driver for your database.
CRUD operations
Every read and write method a service inherits, what it returns and what it throws.
Getting help
Where to ask a question, report a bug or request a feature.