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, findAll with a { data, total, page, limit } envelope, count and exists. See CRUD operations.
  • Filtering. Column operators (gte, in, between, iContains and more), and, or and not groups, 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.

Installation · Quick start · CRUD operations

Core concepts

How a service gets its config, how relations are declared, and what soft delete, timestamps and primary key types change.

Defining services · Relations · Soft delete

Querying

Filters, relation filters, turning a query string into filters, sorting, offset and cursor pages, column selection, full-text search and grouped counts.

Filtering · Query parsing · Aggregates

Writing data

Upsert, bulk writes, nested creates and links, optimistic locking, transactions and row locks.

Bulk operations · Transactions · Optimistic locking

Securing it

Limit what callers may filter, sort and write, scope every query to a tenant, validate input and react to writes.

Allowlists · Multi-tenancy · Hooks and validation

Put it in your stack

Generated controllers, OpenAPI, async config, per-entity config, tests and coding agents.

Controller factory · OpenAPI · Testing

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.

API reference · Config types · Exports

Ship it

What changed in each release, where to ask for help, and how to support the project.

Changelog · Getting help · Sponsor

How do I install it?

pnpm add @avytrix/nestjs-drizzle-crud @nestjs/common @nestjs/core drizzle-orm reflect-metadata

Then 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().

users/users.service.ts
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
> {}
users/users.module.ts
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

FactValue
DatabasesPostgreSQL, MySQL and SQLite, libSQL and Cloudflare D1 (dialect: "postgresql", "mysql" or "sqlite")
Documented version3.8.3
LicenseMIT
Runtime dependenciesNone. Peer dependencies only, three of them optional (postgres, mysql2, @nestjs/swagger)
Node20 or later
SourceGitHub, 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 dialect setting accepts "postgresql", "mysql" and "sqlite" only, so SQL Server, Oracle and MongoDB are out.

Common questions

The library is built and maintained by Avytrix Technologies.

Next steps

Last updated View change history

On this page