Packages / Drizzle

Drizzle

Drizzle ORM integration for PostgreSQL, MySQL, and SQLite.

@miiajs/drizzle integrates Drizzle ORM with MiiaJS, providing connection management, retry logic, and DI-based access to the database.

Installation

Install the core package and a database driver. MiiaJS is bun-native - swap bun add for npm install, pnpm add, or yarn add if you use a different package manager.

bun add @miiajs/drizzle drizzle-orm postgres

SQLite needs no driver package on Bun: dialect: 'sqlite' uses the built-in bun:sqlite there and better-sqlite3 everywhere else. connection.driver picks one explicitly. SQLite is not supported on Deno - better-sqlite3 crashes the Deno process instead of failing with an error, so DrizzleModule refuses the dialect on Deno unless connection.driver is set.

Setup

Configure the module

typescript
import { Module } from '@miiajs/core'
import { DrizzleModule } from '@miiajs/drizzle'

@Module({
  imports: [
    DrizzleModule.configure(
      {
        dialect: 'postgres',
        connection: { url: 'postgres://localhost:5432/mydb' },
      },
      { global: true },
    ),
  ],
})
class AppModule {}

DrizzleModule.configure() exports its drizzleDb() token. { global: true } makes the db visible to every module; without it, a module that injects the db imports DrizzleModule bare (imports: [DrizzleModule]) and gets this configuration.

With ConfigService (from @miiajs/config):

typescript
import { ConfigService } from '@miiajs/config'

DrizzleModule.configure((resolve) => {
  const config = resolve(ConfigService)
  return {
    dialect: 'postgres',
    connection: {
      url: config.getOrThrow('DATABASE_URL'),
      retry: { attempts: 10, delay: 10_000 },
    },
  }
})

Configuration options

typescript
interface DrizzleModuleOptions {
  dialect: 'postgres' | 'mysql' | 'sqlite'
  connection: {
    url: string
    driver?: 'bun:sqlite' | 'better-sqlite3' // sqlite only. Default: 'bun:sqlite' on Bun, 'better-sqlite3' elsewhere
    retry?: {
      attempts?: number  // Default: 10
      delay?: number     // Default: 10_000 ms
    }
  }
  schema?: Record<string, unknown>
  casing?: 'snake_case' | 'camelCase'
}

Define schemas

Use standard Drizzle table definitions:

typescript
import { pgTable, serial, varchar, timestamp } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: varchar('name', { length: 255 }).notNull(),
  email: varchar('email', { length: 255 }).notNull().unique(),
  role: varchar('role', { length: 50 }).notNull().default('user'),
  createdAt: timestamp('created_at').default(sql`now()`),
})

Register schemas

All schemas live in DrizzleModule.configure({ schema }). Drizzle is schema-first - tables are imported directly into services as values, so there is no per-feature DI registration. Use a barrel file (src/db.schema.ts) that re-exports every table for a single typeof schema reference:

typescript
// src/db.schema.ts
export * from './user/user.table.js'
export * from './post/post.table.js'

miia g table <name> from the CLI writes a table file for the project's dialect and adds its line to this barrel.

typescript
// src/app.module.ts
import * as schema from './db.schema.js'

@Module({
  imports: [
    DrizzleModule.configure({
      dialect: 'postgres',
      connection: { url: DATABASE_URL },
      schema,
    }),
  ],
})
class AppModule {}

Use in services

drizzleDb<TDb>() returns a typed DI token - a Token object from @miiajs/core, not a class. Define it once and inject it with the framework's standard inject() helper:

typescript
// src/db.ts
import { drizzleDb } from '@miiajs/drizzle'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import type * as schema from './db.schema.js'

export const db = drizzleDb<PostgresJsDatabase<typeof schema>>()
typescript
// src/user/user.service.ts
import { Injectable, inject } from '@miiajs/core'
import { eq } from 'drizzle-orm'
import { db } from '../db.js'
import { users } from './user.table.js'

@Injectable()
class UserService {
  private db = inject(db)

  async findAll() {
    return this.db.select().from(users)
  }

  async findById(id: number) {
    const [user] = await this.db
      .select()
      .from(users)
      .where(eq(users.id, id))
    return user ?? null
  }

  async create(data: { name: string; email: string }) {
    const [user] = await this.db
      .insert(users)
      .values(data)
      .returning()
    return user
  }

  async update(id: number, data: Partial<{ name: string; email: string }>) {
    const [user] = await this.db
      .update(users)
      .set(data)
      .where(eq(users.id, id))
      .returning()
    return user
  }

  async delete(id: number) {
    await this.db.delete(users).where(eq(users.id, id))
  }
}

Type safety

drizzleDb<TDb>() accepts the concrete Drizzle database type as a generic parameter. The type flows through inject(db) so every call site gets full autocompletion for select, insert, update, delete, execute, and - when you pass a schema - db.query.* relational queries.

For MySQL and SQLite, swap the type accordingly: MySql2Database<typeof schema> from drizzle-orm/mysql2; for SQLite BunSQLiteDatabase<typeof schema> from drizzle-orm/bun-sqlite on Bun, or BetterSQLite3Database<typeof schema> from drizzle-orm/better-sqlite3 with better-sqlite3.

Relational queries

When schemas include relations, db.query.* is typed automatically through the typeof schema generic:

typescript
import { relations } from 'drizzle-orm'

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}))

Make sure the relations live in the schema barrel so they are part of typeof schema. Then:

typescript
const usersWithPosts = await this.db.query.users.findMany({
  with: { posts: true },
})

Migrations

@miiajs/drizzle handles the runtime connection, but it does not run migrations on startup - onInit() only connects and retries on transient failures. Schema changes are a build/deploy step handled by drizzle-kit.

Install it as a dev dependency:

bun add -D drizzle-kit

Create drizzle.config.ts at the project root:

typescript
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  schema: './src/db.schema.ts',
  out: './drizzle',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.DATABASE_URL!,
  },
})

Then generate and apply migrations:

bash
# Generate a new SQL migration from your schema changes
npx drizzle-kit generate

# Apply pending migrations against the database
npx drizzle-kit migrate

Commit both the generated SQL files in drizzle/ and the drizzle/meta/ snapshots - the snapshot is the baseline for the next generate.

A complete working example - including drizzle.config.ts, db:generate / db:migrate scripts, and a committed initial migration - lives in examples/drizzle-app.

Multiple connections

Use named connections for multi-database setups: pass { name } as the second argument of configure(). Each connection gets its own schema barrel and its own typed drizzleDb<TDb>(name) token:

typescript
// src/db.ts - tokens for both connections
import { drizzleDb } from '@miiajs/drizzle'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import type * as mainSchema from './db.schema.js'
import type * as analyticsSchema from './analytics/db.schema.js'

export const db = drizzleDb<PostgresJsDatabase<typeof mainSchema>>()
export const analytics = drizzleDb<PostgresJsDatabase<typeof analyticsSchema>>('analytics')
typescript
// Root module - one configure() per connection
import * as mainSchema from './db.schema.js'
import * as analyticsSchema from './analytics/db.schema.js'

@Module({
  imports: [
    DrizzleModule.configure(
      {
        dialect: 'postgres',
        connection: { url: MAIN_DB },
        schema: mainSchema,
      },
      { global: true },
    ),
    DrizzleModule.configure(
      {
        dialect: 'postgres',
        connection: { url: ANALYTICS_DB },
        schema: analyticsSchema,
      },
      { name: 'analytics', global: true },
    ),
    UserModule,
    AnalyticsModule,
  ],
})
class AppModule {}

A bare imports: [DrizzleModule] reaches only the configuration without a name. A named connection is either global, as here, or imported by one module that re-exports it to the modules that import that one:

typescript
const analyticsDb = DrizzleModule.configure(analyticsOptions, { name: 'analytics' })

@Module({ imports: [analyticsDb], exports: [analyticsDb] })
class AnalyticsDbModule {}

Inject the connection you need:

typescript
@Injectable()
class AnalyticsService {
  private db = inject(analytics)

  async getEvents() {
    return this.db.select().from(events)
  }
}

Supported databases

DialectDriverVersion
postgrespostgres>= 3.0.0
mysqlmysql2>= 3.0.0
sqlitebun:sqlite (Bun, built in)-
sqlitebetter-sqlite3 (Node.js)>= 11.0.0; 13.x needs Node.js >= 22.14 (it segfaults on 22.12 and 22.13)

All drivers are optional peer dependencies - install only what you need.

Connection lifecycle

  • onInit - establishes connection with automatic retry on transient errors (ECONNREFUSED, ECONNRESET, ETIMEDOUT, ENOTFOUND, EAI_AGAIN)
  • onDestroy - closes the connection gracefully

A provider that injects the db gets its onInit() after the connection opens and its onDestroy() before the connection closes, wherever its module sits in imports.

Testing

Override services at the DI level with TestApp.override() - handlers call your service, not the database directly, so you don't need to stub the Drizzle proxy API.

typescript
import { TestApp } from '@miiajs/testing'
import { AppModule } from './app.module.js'
import { UsersService } from './users/users.service.js'

const app = await TestApp.create(AppModule)
  .override(UsersService, {
    findAll: async () => [{ id: 1, name: 'Alice' }]
  })
  .compile()

const res = await app.request('GET', '/users')
expect(res.status).toBe(200)
expect(await res.json()).toEqual([{ id: 1, name: 'Alice' }])

await app.close()

For unit tests that exercise the service itself, override the database token instead:

typescript
import { drizzleDb } from '@miiajs/drizzle'

const app = await TestApp.create(AppModule)
  .override(drizzleDb(), mockDb)
  .compile()

Exports

typescript
import {
  DrizzleModule,
  drizzleDb,
  type Dialect,
  type DrizzleModuleOptions,
  type DrizzleDbToken,
} from '@miiajs/drizzle'

DrizzleDbToken<TDb> is Token<TDb> from @miiajs/core.

Edit this page on GitHub