Skip to content

knaadh/nestjs-drizzle

Repository files navigation

Nest Logo

A NestJS module for integrating Drizzle ORM

Nrwl Nx

CodeFactor Grade NPM Discord

Table of Contents

Installation

There is a separate module for each one of the major database drivers supported by the DrizzleORM. You can install them via your preferred package manager.

# PostgresJS
npm install @knaadh/nestjs-drizzle-postgres drizzle-orm postgres

# Node-Postgres
npm install @knaadh/nestjs-drizzle-pg drizzle-orm pg

# MySQL2
npm install @knaadh/nestjs-drizzle-mysql2 drizzle-orm mysql2

# Better-SQLite3
npm install @knaadh/nestjs-drizzle-better-sqlite3 drizzle-orm better-sqlite3

# Turso
npm install @knaadh/nestjs-drizzle-turso drizzle-orm @libsql/client

# PlanetScale
npm install @knaadh/nestjs-drizzle-planetscale drizzle-orm @planetscale/database

Usage

Import the Drizzle module of the respective database driver and pass an options object to initialize it as shown in the example below:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import * as schema from '../db/schema';
import { DrizzleBetterSQLiteModule } from '@knaadh/nestjs-drizzle-better-sqlite3';
@Module({
  imports: [
    DrizzleBetterSQLiteModule.register({
      tag: 'DB_DEV',
      sqlite3: {
        filename: 'demo.db',
      },
      config: { schema: { ...schema } },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

You can inject the Drizzle instances using their respective tag specified in the configuration

import { Inject, Injectable } from '@nestjs/common';
import * as schema from '../db/schema';
import { LibSQLDatabase } from 'drizzle-orm/libsql';
@Injectable()
export class AppService {
  constructor(
    @Inject('DB_DEV') private drizzleDev: LibSQLDatabase<typeof schema>
  ) {}
  async getData() {
    const books = await this.drizzleDev.query.books.findMany();
    const authors = await this.drizzleDev.query.authors.findMany();
    return {
      books: books,
      authors: authors,
    };
  }
}

In case you need the access to the underlying connection client for tasks such as connection termination or other operations, you must define the session property manually, as Drizzle does not currently expose it. In the following example, we demonstrate how to access the client to close all database connections when our NestJS application is terminated, using the onApplicationShutdown hook.

Note that the specific client type vary depending on the database driver being used, so adjust the type accordingly.

import { MySql2Client, MySql2Database } from 'drizzle-orm/mysql2';

import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';

import * as schema from './schema/mysql';

export type ExtendedMySql2Database<T extends Record<string, unknown>> =
  MySql2Database<T> & {
    session: {
      client: MySql2Client;
    };
  };

@Injectable()
export class AppService implements OnApplicationShutdown {
  constructor(
    @Inject('DB')
    private drizzleProd: ExtendedMySql2Database<typeof schema>,
  ) {}

  async getHello() {
    const books = await this.drizzleProd.query.books.findMany();
    const authors = await this.drizzleProd.query.authors.findMany();
    return {
      books: books,
      authors: authors,
    };
  }
  
  async onApplicationShutdown() {
    await this.drizzleProd.session.client.end();
  }
}

You can read the detailed documentation of each of the module from the links given below

Documentation

License

This package is MIT licensed.