Skip to content

Adding a New Database

Each ORM adapter uses an internal DialectConfig object to isolate all database-specific code (column types, client setup, dependencies). Adding a new database to an existing ORM means adding a new DialectConfig and registering it — no other code needs to change.

How it works

DrizzleAdapter("mysql")

    └─▶ DIALECTS["mysql"]  →  DrizzleDialectConfig
            typeMap, idPkExpr, clientFile, deps…

The adapter dispatches every database-specific decision to the dialect config. The rest of the adapter (query builders, route imports, etc.) is completely database-agnostic.

Step 1: Understand what changes per database

For a new database "mydb" you need to define:

WhatExample (SQLite → MyDB)
ORM core packagedrizzle-orm/sqlite-coredrizzle-orm/mydb-core
Table factory function namesqliteTablemydbTable
Column type for stringtextvarchar(255)
Column type for booleaninteger (0/1)boolean
Column type for dateinteger (epoch ms)datetime
UUID column expressiontext('id').$defaultFn(randomUUID) → …
Client file (db.ts) contentnew Database('sqlite.db')createPool(url)
Runtime npm dependenciesbetter-sqlite3mydb-driver

Step 2: Add a DialectConfig to the ORM adapter

Open the relevant adapter (e.g. packages/core/src/orm/drizzle/index.ts) and add a new constant:

ts
const mydbDialect: DrizzleDialectConfig = {
  tableFactory: 'mydbTable',
  corePackage: 'drizzle-orm/mydb-core',
  typeMap: {
    string: { fn: 'varchar', import: 'varchar', extraArgs: '{ length: 255 }' },
    number: { fn: 'int', import: 'int' },
    boolean: { fn: 'boolean', import: 'boolean' },
    date: { fn: 'datetime', import: 'datetime' },
  },
  uuidImport: 'varchar',
  dateNowImport: 'datetime',
  fkExpr: (col) => `varchar('${col}', { length: 36 })`,
  idPkExpr: (col) =>
    `varchar('${col}', { length: 36 }).primaryKey().$defaultFn(() => crypto.randomUUID())`,
  uuidExpr: (col) =>
    `varchar('${col}', { length: 36 }).$defaultFn(() => crypto.randomUUID())`,
  dateNowExpr: (col) => `datetime('${col}').$defaultFn(() => new Date())`,
  generateClientFile: () =>
    [
      `// Auto-generated by Codabra — do not edit manually`,
      `import { createPool } from 'mydb-driver';`,
      `import { drizzle } from 'drizzle-orm/mydb';`,
      `import * as schema from '@/drizzle/schema';`,
      ``,
      `const pool = createPool({ uri: process.env.DATABASE_URL });`,
      `export const db = drizzle(pool, { schema });`,
      ``,
    ].join('\n'),
  getDependencies: () => ({
    'drizzle-orm': '^0.30.0',
    'mydb-driver': '^1.0.0',
  }),
  getDevDependencies: () => ({ 'drizzle-kit': '^0.21.0' }),
};

Then register it in the DIALECTS map (same file):

ts
const DIALECTS: Record<string, DrizzleDialectConfig> = {
  sqlite: sqliteDialect,
  postgresql: postgresqlDialect,
  mysql: mysqlDialect,
  mydb: mydbDialect, // ← add this
};

That's it for the adapter — no other method in DrizzleAdapter needs to change.

Step 3: Register the database in the ORM registry

Open packages/core/src/orm/index.ts and import the new dialect entry, then add it to supportedDatabases:

ts
export const mydb: DatabaseDialectEntry = { name: 'mydb', label: 'MyDB' };
export const allDialects: readonly DatabaseDialectEntry[] = [
  sqlite,
  postgresql,
  mysql,
  mydb,
];
ts
import { sqlite, postgresql, mysql, mydb } from './dialects';

export const ormRegistry: OrmRegistryEntry[] = [
  {
    name: 'drizzle',
    label: 'Drizzle ORM',
    supportedDatabases: [sqlite, postgresql, mysql, mydb], // ← add mydb
    defaultDatabase: 'sqlite',
    create: (database) => new DrizzleAdapter(database),
  },
  // ...
];

Step 4: Update the JSON Schema

Add "mydb" to the database enum in packages/core/src/schemas/codabra.schema.json:

json
"database": {
    "type": "string",
    "enum": ["sqlite", "postgresql", "mysql", "mydb"]
}

Step 5: Update Prisma if applicable

If the new database also works with Prisma and Prisma supports it natively, you only need to:

  1. Add mydb to SUPPORTED_DATABASES in packages/core/src/orm/prisma/index.ts
  2. Prisma's datasource db { provider = "mydb" } will be generated automatically — no other change needed.

Step 6: Test it

bash
node packages/cli/bin/create-codabra.js my-test-app
# Select: Drizzle ORM → MyDB → ...

# Or generate into an existing project:
# Set "database": "mydb" in codabra.json, then:
node packages/cli/bin/codabra.js generate

Check apps/nextjs/src/lib/db.ts and apps/nextjs/src/drizzle/schema.ts for the correct MyDB output.

Released under the Elastic License 2.0.