Fragno
Database Fragments

Kysely Adapter

Using Kysely with database-enabled Fragments

The Kysely adapter integrates Fragno database Fragments with your existing Kysely database instance. 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 that wraps your existing Kysely instance:

lib/database-adapter.ts
import { KyselyAdapter } from "@fragno-dev/db/adapters/kysely";
import { db } from "./database"; // Your existing Kysely instance

export const fragmentDbAdapter = new KyselyAdapter({
  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 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