Install NestJS with Drizzle ORM
Set up NestJS with Drizzle ORM. Install nestjs-drizzle-crud with its four peers, then the driver for PostgreSQL, MySQL or SQLite.
To use NestJS with Drizzle ORM through nestjs-drizzle-crud, install the package and its four
required peers: @nestjs/common, @nestjs/core, drizzle-orm and reflect-metadata. Then add
a database driver. On PostgreSQL that is postgres and on MySQL mysql2 if you let the module
open the connection; on SQLite you install your driver, such as @libsql/client, and pass a
Drizzle instance you built. The package itself has no runtime dependencies.
Before you start
You need Node 20 or later and a NestJS 10 or later project with decorators turned on. The documented version is 3.8.3, published as @avytrix/nestjs-drizzle-crud on npm.
Renamed to @avytrix/nestjs-drizzle-crud
From 3.5.1 the package is published under the @avytrix scope. The unscoped
nestjs-drizzle-crud name is deprecated on npm and points to the new one. To move, change the
dependency and the imports; nothing else changes. See
how to switch to the scoped package.
How do I install it?
Install the package and its required peers
pnpm add @avytrix/nestjs-drizzle-crud @nestjs/common @nestjs/core drizzle-orm reflect-metadataA NestJS project already has @nestjs/common, @nestjs/core and reflect-metadata, so in
practice you add the package and drizzle-orm.
Add the driver for your database
For PostgreSQL with a connection string, install postgres (postgres.js):
pnpm add postgresFor MySQL, install mysql2. The module builds a pool from a connection string with it, or you
build the Drizzle instance yourself:
pnpm add mysql2For SQLite or libSQL (Turso), install @libsql/client and build the Drizzle instance yourself.
SQLite, libSQL and Cloudflare D1 covers the other SQLite drivers.
pnpm add @libsql/clientIf you build a PostgreSQL Drizzle instance yourself (node-postgres, PGlite, Neon), install that
driver instead and skip postgres. Which driver do I need? explains
the choice.
Turn on decorators
NestJS reads constructor types through decorator metadata, and the package reads per-service
config from the @CrudService decorator. A project made with the Nest CLI already has both flags.
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Add OpenAPI support, if you want it
ApiCrudQuery and ApiCrudList document list routes in Swagger. They need @nestjs/swagger,
which the package loads only the first time one of them runs.
pnpm add @nestjs/swaggerWhich peer dependencies does it need?
Four peers are required and two are optional. These ranges come from the package's
peerDependencies:
| Package | Version range | Needed when |
|---|---|---|
@nestjs/common | >=10.0.0 | Always |
@nestjs/core | >=10.0.0 | Always |
drizzle-orm | >=0.28.0 <1.0.0 | Always |
reflect-metadata | ^0.1.13 || ^0.2.0 | Always |
postgres | >=3.0.0 (optional) | You pass connectionString with dialect: "postgresql" |
mysql2 | >=3.0.0 (optional) | You pass connectionString with dialect: "mysql" |
@nestjs/swagger | >=7.0.0 (optional) | You use ApiCrudQuery or ApiCrudList |
A SQLite driver is never a peer: the module does not open SQLite connections, so the driver is your app's own dependency.
Which driver do I need?
It depends on who builds the connection: let the module build it from a connection string
(PostgreSQL and MySQL), or build a Drizzle instance yourself and pass it as db (any dialect).
The module creates a postgres.js client, wraps it with drizzle-orm/postgres-js, and closes it
when the app shuts down. Install postgres.
import { Module } from "@nestjs/common";
import { DrizzleCrudModule } from "@avytrix/nestjs-drizzle-crud";
import * as schema from "./db/schema";
@Module({
imports: [
DrizzleCrudModule.forRoot({
dialect: "postgresql",
connectionString: process.env.DATABASE_URL,
schema,
}),
],
})
export class AppModule {}connectionString without the driver
postgres is an optional peer, so neither npm nor pnpm installs it for you. If you pass
connectionString and it is missing, the app fails at startup when the module imports it.
Install it, or pass your own db.
To read the connection settings from ConfigService instead of process.env, use
forRootAsync. Every forRoot option is on the
forRoot page.
How do I switch to the scoped package?
Replace the dependency, then the import paths. The API, the config and the behaviour are the same; only the name changed.
pnpm remove nestjs-drizzle-crud
pnpm add @avytrix/nestjs-drizzle-crud// before
import { SqlBaseCrudService } from "nestjs-drizzle-crud";
// after
import { SqlBaseCrudService } from "@avytrix/nestjs-drizzle-crud";If you copied the agent rules templates or point an MCP client at the package, update those
paths too: node_modules/@avytrix/nestjs-drizzle-crud/AGENT.md, and
npx -p @avytrix/nestjs-drizzle-crud nestjs-drizzle-crud-mcp for the MCP server. See
AI agents and MCP.
How are versions numbered?
The package follows semver:
- Patch releases fix bugs and do not change the API.
- Minor releases add methods or options and stay backwards compatible.
- Major releases may change or remove public API, such as the deprecated exceptions and config flags marked for removal.
The changelog lists what changed in each release. The npm tarball ships the
same CHANGELOG.md.
Common questions
Yes. From 3.5.1 the package is published as @avytrix/nestjs-drizzle-crud and the unscoped name is deprecated. Change the dependency and the imports; nothing else changes.
Any drizzle-orm from 0.28.0 up to, but not including, 1.0.0, as declared in the package's peer dependencies.
Only when you pass connectionString with dialect: "postgresql". If you build the Drizzle instance yourself and pass it as db, install whichever driver you used instead.
Yes, since 3.7.0. With dialect: "mysql" it builds a mysql2 pool from the URL. Install mysql2, which is an optional peer.
libSQL (@libsql/client), for a local file or Turso. It is the driver the package is tested on, and it supports the bulk writes. Cloudflare D1 and synchronous drivers such as better-sqlite3 run the single-row methods only.
Yes. The peer range for @nestjs/common and @nestjs/core is 10.0.0 and later, and the example app in the repository runs on NestJS 11.
Next steps
Quick start
Wire one table end to end: schema, module, service and controller.
forRoot
Every connection and project default option the module accepts.
CRUD operations
The read and write methods every service inherits.
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 quick start
Build NestJS Drizzle CRUD endpoints for one table. Define the schema, configure the module, add an empty service and call it from a controller.