nestjs-drizzle

repository·main·Indexed 42 days ago

https://github.com/knaadh/nestjs-drizzle

A collection of NestJS modules for integrating Drizzle ORM with various database drivers, including Postgres (via node-postgres and postgres-js), MySQL (via mysql2), SQLite (via better-sqlite3), Turso, and PlanetScale. Supports static registration, asynchronous factories, and class-based configuration with tagging for multiple database instances.

Tokens
17.8K
Snippets
54
Records
63
Agent score
50%

What's inside nestjs-drizzle

  1. Configure DrizzlePlanetScaleModule

    main

    The DrizzlePlanetScaleModule can be initialized using three different NestJS patterns. You can provide a tag to uniquely identify the database instance, which is used later for injection.

    Method 1: Static Registration

    Use DrizzlePlanetScaleModule.register() to pass a configuration object directly.

    Method 2: Asynchronous Registration with useFactory

    Use DrizzlePlanetScaleModule.registerAsync() with a useFactory function. This is useful for loading configuration from environment variables or other services.

    Method 3: Asynchronous Registration with useClass

    Use DrizzlePlanetScaleModule.registerAsync() with a useClass to delegate configuration to a dedicated service class.

    import { Module } from '@nestjs/common';
    import * as schema from '../db/schema';
    import { DrizzlePlanetScaleModule } from '@knaadh/nestjs-drizzle-planetscale';
    
    @Module({
      imports: [
        // Method #1: Pass options object
        DrizzlePlanetScaleModule.register({
          tag: 'DB_DEV',
          planetscale: {
            config: {
              username: 'PLANETSCALE_USERNAME',
              password: 'PLANETSCALE_PASSWORD',
              host: 'PLANETSCALE_HOST',
            },
          },
          config: { schema: { ...schema } },
        }),
    
        // Method #2: useFactory()
        DrizzlePlanetScaleModule.registerAsync({
          tag: 'DB_PROD',
          useFactory() {
            return {
              planetscale: {
                config: {
                  username: 'PLANETSCALE_USERNAME',
                  password: 'PLANETSCALE_PASSWORD',
                  host: 'PLANETSCALE_HOST',
                },
              },
              config: { schema: { ...schema } },
            };
          },
        }),
    
        // Method #3: useClass()
        DrizzlePlanetScaleModule.registerAsync({
          tag: 'DB_STAGING',
          useClass: DBConfigService,
        }),
      ],
    })
    export class AppModule {}
    
    // Example of a configuration class for Method #3
    export class DBConfigService {
      create = () => {
        return {
          planetscale: {
            config: {
              username: 'PLANETSCALE_USERNAME',
              password: 'PLANETSCALE_PASSWORD',
              host: 'PLANETSCALE_HOST',
            },
          },
          config: { schema: { ...schema } },
        };
      };
    }
  2. Initialize DrizzleBetterSQLiteModule in NestJS

    main

    You can initialize the DrizzleBetterSQLiteModule using three different NestJS patterns: static registration, asynchronous factory, or asynchronous class-based configuration. Use the tag property to uniquely identify different database instances if you are connecting to multiple databases.

    import { Module } from '@nestjs/common';
    import * as schema from '../db/schema';
    import { DrizzleBetterSQLiteModule } from '@knaadh/nestjs-drizzle-better-sqlite3';
    
    @Module({
      imports: [
        // Method #1: Pass options object directly
        DrizzleBetterSQLiteModule.register({
          tag: 'DB_DEV',
          sqlite3: {
            filename: 'demo.db',
          },
          config: { schema: { ...schema } },
        }),
    
        // Method #2: Use useFactory() for async configuration
       DrizzleBetterSQLiteModule.registerAsync({
          tag: 'DB_PROD',
          useFactory() {
            return {
              sqlite3: {
                filename: 'demo.db',
              },
              config: { schema: { ...schema } },
            };
          },
        }),
    
        // Method #3: Use useClass() with a configuration service
        DrizzleBetterSQLiteModule.registerAsync({
          tag: 'DB_STAGING',
          useClass: DBConfigService,
        }),
      ],
    })
    export class AppModule {}
    
    // Example configuration service for Method #3
    export class DBConfigService {
      create = () => {
        return {
          sqlite3: {
            filename: 'demo.db',
          },
          config: { schema: { ...schema } },
        };
      };
    }
  3. Initialize DrizzleMySqlModule in NestJS

    main

    You can initialize the DrizzleMySqlModule using three different NestJS patterns: synchronous registration, asynchronous factory, or asynchronous class-based configuration. Use the tag property to uniquely identify different database connections (e.g., for dev, prod, or staging environments).

    import { Module } from '@nestjs/common';
    import * as schema from '../db/schema';
    import { DrizzleMySqlModule } from '@knaadh/nestjs-drizzle-mysql2';
    
    @Module({
      imports: [
        // Method #1: Pass options object directly
        DrizzleMySqlModule.register({
          tag: 'DB_DEV',
          mysql: {
            connection: 'client',
            config: {
              host: '127.0.0.1',
              user: 'root',
              database: 'drizzleDB',
            },
          },
          config: { schema: { ...schema }, mode: 'default' },
        }),
    
        // Method #2: Use factory for async configuration
       DrizzleMySqlModule.registerAsync({
          tag: 'DB_PROD',
          useFactory() {
            return {
              mysql: {
                connection: 'client',
                config: {
                  host: '127.0.0.1',
                  user: 'root',
                  database: 'drizzleDB',
                },
              },
              config: { schema: { ...schema }, mode: 'default' },
            };
          },
        }),
    
        // Method #3: Use a configuration class
        DrizzleMySqlModule.registerAsync({
          tag: 'DB_STAGING',
          useClass: DBConfigService,
        }),
      ],
    })
    export class AppModule {}
  4. Initialize DrizzlePostgresModule in NestJS

    main

    You can initialize the DrizzlePostgresModule using three different NestJS patterns: synchronous registration, asynchronous registration with a factory, or asynchronous registration with a configuration class. Use the tag property to uniquely identify different database connections, which allows you to inject multiple Drizzle instances into your services.

    import { Module } from '@nestjs/common';
    import * as schema from '../db/schema';
    import { DrizzlePostgresModule } from '@knaadh/nestjs-drizzle-postgres';
    
    @Module({
      imports: [
        // Method #1: Pass options object directly
        DrizzlePostgresModule.register({
          tag: 'DB_DEV',
          postgres: {
            url: 'postgres://postgres:@127.0.0.1:5432/drizzleDB',
          },
          config: { schema: { ...schema } },
        }),
    
        // Method #2: useFactory() for async configuration
        DrizzlePostgresModule.registerAsync({
          tag: 'DB_PROD',
          useFactory() {
            return {
              postgres: {
                url: 'postgres://postgres:@127.0.0.1:5432/drizzleDB',
              },
              config: { schema: { ...schema } },
            };
          },
        }),
    
        // Method #3: useClass() using a configuration service
         DrizzlePostgresModule.registerAsync({
          tag: 'DB_STAGING',
          useClass: DBConfigService,
        }),
      ],
    })
    export class AppModule {}
    
    // Example of a configuration service for Method #3
    export class DBConfigService {
      create = () => {
        return {
          postgres: {
            url: 'postgres://postgres:@127.0.0.1:5432/drizzleDB',
          },
          config: { schema: { ...schema } },
        };
      };
    }
  5. Initialize DrizzlePGModule in NestJS

    main

    You can initialize the DrizzlePGModule using three different methods depending on your configuration needs. You can also specify a tag to allow for multiple database connections within the same application.

    import { Module } from '@nestjs/common';
    import * as schema from '../db/schema';
    import { DrizzlePGModule } from '@knaadh/nestjs-drizzle-pg';
    
    @Module({
      imports: [
        // Method #1: Pass options object directly
        DrizzlePGModule.register({
          tag: 'DB_DEV',
          pg: {
            connection: 'client',
            config: {
              connectionString: 'postgres://postgres:@127.0.0.1:5432/drizzleDB',
            },
          },
          config: { schema: { ...schema } },
        }),
    
        // Method #2: Use useFactory() for dynamic configuration
        DrizzlePGModule.registerAsync({
          tag: 'DB_PROD',
          useFactory() {
            return {
              pg: {
                connection: 'client',
                config: {
                  connectionString: 'postgres://postgres:@127.0.0.1:5432/drizzleDB',
                },
              },
              config: { schema: { ...schema } },
            };
          },
        }),
    
        // Method #3: Use useClass() with a configuration service
        DrizzlePGModule.registerAsync({
          tag: 'DB_STAGING',
          useClass: DBConfigService,
        }),
      ],
    })
    export class AppModule {}
  6. Install the NestJS Drizzle module for your database driver

    main

    The project provides separate modules for each major database driver supported by Drizzle ORM. Install the module corresponding to your driver along with drizzle-orm and the driver's native client package.

    # 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