Database Integration

Drizzle Adapter

Using Drizzle ORM with database-enabled Fragments

The Drizzle adapter integrates Fragno database Fragments with your database.

Note that Fragno executes SQL via Kysely’s Dialect interface (so we can reuse Kysely/community dialects). Drizzle is still the “user-facing” ORM here: the Fragno CLI can generate Drizzle schema you can import into your Drizzle setup.

Prerequisites

  • Existing Drizzle ORM setup in your project
  • A Fragno Fragment that requires database integration

Installation

npm install drizzle-orm @fragno-dev/db

Adapter Configuration

Create a Drizzle adapter using a Kysely Dialect + a Fragno driverConfig:

lib/database-adapter.ts
import { DrizzleAdapter } from "@fragno-dev/db/adapters/drizzle";
import { PostgresDialect } from "@fragno-dev/db/dialects";
import { NodePostgresDriverConfig } from "@fragno-dev/db/drivers";
import { Pool } from "pg";

const dialect = new PostgresDialect({
  pool: new Pool({ connectionString: process.env.DATABASE_URL }),
});

export const fragmentDbAdapter = new DrizzleAdapter({
  dialect,
  driverConfig: new NodePostgresDriverConfig(),
});

Supported Providers

Pick a matching driverConfig from @fragno-dev/db/drivers:

  • PostgreSQL: NodePostgresDriverConfig or PGLiteDriverConfig
  • MySQL/MariaDB: MySQL2DriverConfig
  • SQLite: BetterSQLite3DriverConfig, SQLocalDriverConfig, or CloudflareDurableObjectsDriverConfig

Schema Generation & Migration

The Fragno CLI enables you to generate schema files based on the Fragment's database schema.

To generate a schema file, run the following command:

npx @fragno-dev/cli db generate lib/comment-fragment-server.ts -o schema/comment-fragment-schema.ts

This example assumes that comment-fragment-server.ts exports a fragment instance. The CLI uses this instance to determine the Fragment's database schema. The generated schema is written to schema/comment-fragment-schema.ts (defined by the -o option).

This output is a Drizzle schema module. Point your drizzle.config.ts at it (in addition to your own schema), or import it into your main schema file.

Optionally, you can update your main schema file to import the generated schema file.

src/schema/drizzle-schema.ts
import * as commentFragmentSchema from "./comment-fragment-schema";

export const user = pgTable(/* ... */);
// your models/tables here

export const schema = {
  ...commentFragmentSchema,
  user,
};

This way, you can import your schema elsewhere and use the tables defined in the Fragment. This even enables you to join tables from the Fragment with tables from your own schema.

Next Steps