Fragno
Database Fragments

Drizzle Adapter

Using Drizzle ORM with database-enabled Fragments

The Drizzle adapter integrates Fragno database Fragments with your existing Drizzle ORM database instance. Drizzle is a modern, lightweight TypeScript ORM.

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 that wraps your existing Drizzle instance:

lib/database-adapter.ts
import { DrizzleAdapter } from "@fragno-dev/db/adapters/drizzle";
import { db } from "./database"; // Your existing Drizzle instance

export const fragmentDbAdapter = new DrizzleAdapter({
  db,
  provider: "postgresql", // or "mysql", "sqlite"
});

Supported Providers

  • "postgresql" - PostgreSQL databases, including support for PGLite
  • "mysql" - MySQL/MariaDB databases
  • "sqlite" - SQLite databases, including support for Cloudflare

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).

You can then update your drizzle.config.ts file to point to the generated schema file in addition to your own database schema.

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