Database Integration

Overview

Getting started with database-enabled Fragments

Some Fragno Fragments include a built-in database layer for persistent storage. When integrating these Fragments, you typically provide a database adapter that connects the Fragment to your existing database. If better-sqlite3 is installed, Fragno can default to a local SQLite file per fragment under FRAGNO_DATA_DIR (default: ~/.fragno).

Fragment Handles Schema

The Fragment defines its own schema and handles migrations. You just provide the database connection - no need to manually create tables or write SQL.

Identifying Database Fragments

A Fragment uses the database layer if its documentation mentions FragnoPublicConfigWithDatabase or requires a databaseAdapter option.

Check the Fragment's creation function:

// Fragment without database
createExampleFragment(config, options);

// Fragment with database - may require databaseAdapter
createCommentFragment(config, { databaseAdapter });

Quick Start

Installation

npm install @fragno-dev/db

And install the CLI to perform migrations / schema generation:

npm install --save-dev @fragno-dev/cli

Choose Your Workflow

Fragno uses a single runtime adapter (SqlAdapter) for all SQL databases. You choose the schema output format that matches your workflow: SQL (default), Drizzle, or Prisma.

Start here: Database Adapter

Create Database Adapter

Adapters are configured with a Kysely Dialect plus a Fragno driverConfig (they must match). See Database Adapter for the full setup and driver details.

lib/database-adapter.ts
import { SqlAdapter } from "@fragno-dev/db/adapters/sql";
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 SqlAdapter({
  dialect,
  driverConfig: new NodePostgresDriverConfig(),
});

If you're using Prisma with SQLite, set sqliteProfile: "prisma" on the SqlAdapter.

Pass Adapter to Fragment

lib/comment-fragment-server.ts
import { createCommentFragment } from "@fragno-dev/comment-fragment";
import { fragmentDbAdapter } from "./database-adapter";

export function createCommentFragmentInstance() {
  return createCommentFragment(
    {
      // Fragment-specific config
    },
    {
      databaseAdapter: fragmentDbAdapter,
    },
  );
}

// IMPORTANT: Export the Fragment so it's accessible by the Fragno CLI.
export const fragment = createCommentFragmentInstance();

If better-sqlite3 is installed, you can omit databaseAdapter to use the default local SQLite file.

Migrate your Database

Generate and apply database migrations (SQL output is the default):

# Generate SQL migrations
npx fragno-cli db generate lib/comment-fragment-server.ts

# Or apply migrations directly
npx fragno-cli db migrate lib/comment-fragment-server.ts

For schema outputs, choose an explicit format:

# Drizzle schema output
npx fragno-cli db generate lib/comment-fragment-server.ts --format drizzle -o schema/fragno-schema.ts

# Prisma schema output
npx fragno-cli db generate lib/comment-fragment-server.ts --format prisma -o prisma/schema/fragno.prisma

For details on the CLI commands, see the CLI Reference.

Supported Databases

The SqlAdapter supports:

  • PostgreSQL
  • MySQL
  • SQLite

The adapter automatically handles database-specific SQL generation.

FAQ

Q: Do I need to learn a new ORM?

A: No. You continue using your existing Kysely, Drizzle, or Prisma setup. The Fragment just adds its tables to your database.

Q: Can I query Fragment tables directly?

A: Yes, but there are some caveats. In general we recommend using the Fragment's service methods instead. You can query the Fragment's tables directly, but you have to be aware of the fact that the Fragment author may change the schema in the future.

Q: What if I don't use Kysely, Drizzle, or Prisma?

A: Fragno uses Kysely dialects under the hood and currently supports SQL migrations plus Drizzle or Prisma schema outputs. Support for other workflows may be added in the future. Please let us know on GitHub if you'd like to see additional formats.

Q: Can I use multiple database Fragments?

A: Yes! Each Fragment manages its own namespaced tables. You can use the same adapter for all Fragments:

const comments = createCommentFragment({}, { databaseAdapter });
const ratings = createRatingFragment({}, { databaseAdapter });

Next Steps

Choose your database workflow to continue: