Database Integration

Kysely Adapter

Using Kysely with database-enabled Fragments

The Kysely adapter integrates Fragno database Fragments with your database via a Kysely Dialect. Kysely is a type-safe SQL query builder for TypeScript.

Prerequisites

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

Installation

npm install kysely @fragno-dev/db

Adapter Configuration

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

lib/database-adapter.ts
import { KyselyAdapter } from "@fragno-dev/db/adapters/kysely";
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 KyselyAdapter({
  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 migration files or apply migrations directly to your database.

To generate a migration SQL file, run the following command:

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

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 migration file is written to migrations/ (defined by the -o option).

You can use this migration file to apply the changes to your database, using your existing migration system.

Alternatively, you can apply the migrations directly to your database using the following command:

npx @fragno-dev/cli db migrate lib/comment-fragment-server.ts

Next Steps