Repo of the Day
knaadh/nestjs-drizzle
Published: Feb 28, 2024
Open repository ↗A NestJS module for integrating DrizzleORM with Postgres, MySQL, SQLite, Turso and Planetscale drivers
Summary
knaadh/nestjs-drizzle is a collection of first-party NestJS modules that wire up Drizzle ORM into a Nest application via dependency injection. The repo ships one package per supported driver — PostgresJS, node-postgres, MySQL2, Better-SQLite3, Turso, and PlanetScale — so you pick the module that matches the underlying client you already use. It is MIT-licensed and written in TypeScript.
What it is useful for
This is the glue layer for teams who want Drizzle's typed query builder inside a NestJS service without hand-rolling a custom provider. It is useful when you need:
- A Nest
@Modulethat initializes a Drizzle instance from a config object (driver config plus your schema), instead of building aDrizzleModule.forRootAsyncyourself. - Multiple database connections in one app, since each registration accepts a
tagstring that becomes the DI token (the README shows@Inject('DB_DEV')retrieving a LibSQL Drizzle instance, and@Inject('DB')retrieving a MySQL2 instance in the same code path). - Driver parity: the same
register({ tag, sqlite3, config })shape works across SQLite, Postgres, MySQL, Turso, and PlanetScale, so swapping backends in a Nest service is mostly a package swap rather than a rewrite. - Access to the raw client when you need it — for example, calling
client.end()fromonApplicationShutdownfor graceful connection teardown.
The README explicitly notes one documented limitation: Drizzle does not currently expose the underlying session, so if you need the raw client you must extend the type yourself (the README shows an ExtendedMySql2Database example adding a session: { client } field).
How engineers can use it
- Install the package that matches your driver. From the README:
npm install @knaadh/nestjs-drizzle-postgres drizzle-orm postgres
# or pick mysql2, better-sqlite3, node-postgres, turso, planetscale
- Register the module in your
AppModule, supplying a uniquetag, driver-specific config, and your Drizzle schema:
DrizzleBetterSQLiteModule.register({
tag: 'DB_DEV',
sqlite3: { filename: 'demo.db' },
config: { schema: { ...schema } },
}),
-
Inject the Drizzle instance into a service using the tag, then call Drizzle's typed query API —
this.drizzleDev.query.books.findMany()andthis.drizzleDev.query.authors.findMany()are shown working against a SQLite file. -
For shutdown handling, extend the database type and implement
OnApplicationShutdownto callclient.end(), as the README demonstrates for MySQL2.
Per-driver options (connection pooling, SSL, PlanetScale URLs, etc.) are documented in the linked sub-READMEs under packages/ in the repo — start there if the generic examples above do not match your driver.