Skip to content

Configuration

Complete reference for all configuration options.

Basic Configuration

typescript
import { defineConfig } from 'drizzle-multitenant';

export default defineConfig({
  connection: {
    url: process.env.DATABASE_URL!,
    poolConfig: {
      max: 10,
      idleTimeoutMillis: 30000,
    },
    retry: {
      maxAttempts: 3,
      initialDelayMs: 100,
      maxDelayMs: 5000,
      backoffMultiplier: 2,
      jitter: true,
    },
  },
  isolation: {
    strategy: 'schema',
    schemaNameTemplate: (tenantId) => `tenant_${tenantId}`,
    maxPools: 50,
    poolTtlMs: 60 * 60 * 1000,
  },
  schemas: {
    tenant: tenantSchema,
    shared: sharedSchema,
  },
  hooks: {
    onPoolCreated: (tenantId) => console.log(`Pool created: ${tenantId}`),
    onPoolEvicted: (tenantId) => console.log(`Pool evicted: ${tenantId}`),
    onError: (tenantId, error) => console.error(`Error: ${tenantId}`, error),
  },
  debug: {
    enabled: process.env.NODE_ENV === 'development',
    logQueries: true,
    logPoolEvents: true,
    slowQueryThreshold: 1000,
  },
  migrations: {
    tenantFolder: './drizzle/tenant',
    migrationsTable: '__drizzle_migrations',
    tableFormat: 'auto',
    tenantDiscovery: async () => getTenantIds(),
  },
});

Connection Options

OptionTypeDefaultDescription
urlstringRequiredPostgreSQL connection URL
poolConfig.maxnumber10Maximum connections per pool
poolConfig.idleTimeoutMillisnumber30000Idle connection timeout
retry.maxAttemptsnumber3Maximum retry attempts
retry.initialDelayMsnumber100Initial delay before first retry
retry.maxDelayMsnumber5000Maximum delay between retries
retry.backoffMultipliernumber2Exponential backoff multiplier
retry.jitterbooleantrueAdd randomness to prevent thundering herd

Isolation Options

OptionTypeDefaultDescription
strategy'schema'RequiredIsolation strategy (only schema supported)
schemaNameTemplatefunctionRequiredFunction to generate schema names
maxPoolsnumber50Maximum concurrent pools (LRU eviction)
poolTtlMsnumber3600000Pool TTL before cleanup (1 hour)

Debug Options

OptionTypeDefaultDescription
enabledbooleanfalseEnable debug mode
logQueriesbooleantrueLog SQL queries
logPoolEventsbooleantrueLog pool lifecycle events
slowQueryThresholdnumber1000Slow query threshold in ms
loggerfunctionconsole.logCustom logger function

Custom Logger Example

typescript
import pino from 'pino';

const logger = pino();

export default defineConfig({
  debug: {
    enabled: true,
    logger: (message, context) => {
      if (context?.type === 'slow_query') {
        logger.warn({ ...context }, message);
      } else {
        logger.debug({ ...context }, message);
      }
    },
  },
});

Migration Options

Tenant Migration Options

OptionTypeDefaultDescription
tenantFolderstringRequiredPath to tenant migrations
migrationsTablestring__drizzle_migrationsTracking table name
tableFormat'auto' | 'name' | 'hash' | 'drizzle-kit''auto'Migration table format
defaultFormat'name' | 'hash' | 'drizzle-kit''name'Default format when creating new table
tenantDiscoveryfunctionRequiredFunction to discover tenant IDs

Shared Schema Migration Options

OptionTypeDefaultDescription
sharedFolderstringAuto-detectedPath to shared migrations (from drizzle.config.ts if not set)
sharedTablestring__drizzle_migrationsShared migrations tracking table
sharedTableFormat'auto' | 'name' | 'hash' | 'drizzle-kit''auto'Shared schema table format
sharedDefaultFormat'name' | 'hash' | 'drizzle-kit''name'Default format when creating new shared table

drizzle.config.ts Auto-Detection

If you have an existing drizzle.config.ts file, shared schema settings are automatically detected:

typescript
// drizzle.config.ts
export default defineConfig({
  out: './drizzle',              // → sharedFolder
  migrations: {
    table: '__drizzle_migrations', // → sharedTable
  },
});

The CLI menu shows the detected source:

✔ Configuration loaded
  └─ Shared schema: ./drizzle (from drizzle.config.ts)

Priority: tenant.config.ts > drizzle.config.ts > defaults

Table Format Detection

When tableFormat or sharedTableFormat is set to 'auto' (default), the format is detected from existing tables:

FormatIdentifier ColumnTimestamp ColumnTimestamp Type
namenameapplied_attimestamp
hashhashcreated_attimestamp
drizzle-kithashcreated_atbigint

If no table exists, the format specified in defaultFormat or sharedDefaultFormat is used.

Released under the MIT License.