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-metadata

A 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 postgres

For MySQL, install mysql2. The module builds a pool from a connection string with it, or you build the Drizzle instance yourself:

pnpm add mysql2

For 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/client

If 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.

tsconfig.json
{
  "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/swagger

Which peer dependencies does it need?

Four peers are required and two are optional. These ranges come from the package's peerDependencies:

PackageVersion rangeNeeded when
@nestjs/common>=10.0.0Always
@nestjs/core>=10.0.0Always
drizzle-orm>=0.28.0 <1.0.0Always
reflect-metadata^0.1.13 || ^0.2.0Always
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.

app.module.ts
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.

terminal
pnpm remove nestjs-drizzle-crud
pnpm add @avytrix/nestjs-drizzle-crud
users.service.ts
// 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

Next steps

Last updated View change history

On this page