Not loaded · from marathon-ralph
SQLite database setup with LibSQL client and Drizzle ORM for local dev, Turso cloud, and Cloudflare D1 deployments. Includes schema patterns and migration config.
Claude Code won't load this skill
It ships in marathon-ralph at skills/, but Claude Code only loads skills/<name>/SKILL.md and folders listed under skills in plugin.json. Installing the plugin doesn't add it.
SQLite database implementation using LibSQL client and Drizzle ORM. This skill covers local development, Turso cloud, and Cloudflare D1 deployments.
NEVER use bun:sqlite with Next.js applications.
Next.js runs on Node.js, not Bun runtime. Using bun:sqlite causes:
Cannot find module 'bun:sqlite'
Always use @libsql/client - it works in both Node.js and Bun environments.
| Package | Version | Purpose |
|---|---|---|
@libsql/client | 0.15.15 | LibSQL database client |
libsql | 0.5.22 | Native LibSQL bindings |
drizzle-orm | ^0.45.1 | ORM with type-safe queries |
drizzle-kit | ^0.31.8 | Migrations and studio |
Uses Turso CLI to run local SQLite file.
Environment:
DATABASE_URL=file:local.db
Start local database:
turso dev --db-file local.db
Distributed SQLite hosted on Turso.
Environment:
DATABASE_URL=libsql://your-db-name-org.turso.io
DATABASE_AUTH_TOKEN=your-auth-token
Serverless SQLite on Cloudflare Workers.
Note: Requires Workers runtime and different driver configuration.
ni @libsql/client libsql drizzle-orm drizzle-kit
packages/db/src/index.tsimport { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";
import * as schema from "./schema";
// Environment validation
const env = {
DATABASE_URL: process.env.DATABASE_URL,
DATABASE_AUTH_TOKEN: process.env.DATABASE_AUTH_TOKEN,
};
if (!env.DATABASE_URL) {
throw new Error("DATABASE_URL is required");
}
// Create LibSQL client
const client = createClient({
url: env.DATABASE_URL,
authToken: env.DATABASE_AUTH_TOKEN, // Optional for local, required for Turso
});
// Export Drizzle instance with schema
export const db = drizzle({ client, schema });
// Re-export schema for convenience
export * from "./schema";
drizzle.config.tsimport { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/schema",
out: "./src/migrations",
dialect: "turso",
dbCredentials: {
url: process.env.DATABASE_URL || "",
authToken: process.env.DATABASE_AUTH_TOKEN,
},
});
import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/schema",
out: "./src/migrations",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: process.env.CLOUDFLARE_D1_ID!,
token: process.env.CLOUDFLARE_API_TOKEN!,
},
});
packages/db/src/schema/index.tsimport { sql } from "drizzle-orm";
import {
sqliteTable,
text,
integer,
index,
primaryKey,
} from "drizzle-orm/sqlite-core";
// Basic table with common patterns
export const users = sqliteTable(
"users",
{
id: text("id").primaryKey(),
email: text("email").notNull().unique(),
name: text("name"),
createdAt: integer("created_at", { mode: "timestamp_ms" })
.default(sql`(unixepoch() * 1000)`)
.notNull(),
updatedAt: integer("updated_at", { mode: "timestamp_ms" })
.default(sql`(unixepoch() * 1000)`)
.notNull(),
},
(table) => [
index("users_email_idx").on(table.email),
]
);
// Boolean columns (SQLite uses integers)
export const todos = sqliteTable("todos", {
id: text("id").primaryKey(),
title: text("title").notNull(),
completed: integer("completed", { mode: "boolean" }).default(false),
userId: text("user_id").references(() => users.id, { onDelete: "cascade" }),
createdAt: integer("created_at", { mode: "timestamp_ms" })
.default(sql`(unixepoch() * 1000)`),
});
// Composite primary key
export const userRoles = sqliteTable(
"user_roles",
{
userId: text("user_id").notNull().references(() => users.id),
role: text("role").notNull(),
},
(table) => [
primaryKey({ columns: [table.userId, table.role] }),
]
);
| TypeScript Type | SQLite Column | Drizzle Definition |
|---|---|---|
string | TEXT | text("column") |
number | INTEGER | integer("column") |
boolean | INTEGER | integer("column", { mode: "boolean" }) |
Date | INTEGER | integer("column", { mode: "timestamp_ms" }) |
object | TEXT | text("column", { mode: "json" }) |
import { db, users, todos } from "@repo/db";
import { eq, and, desc, like } from "drizzle-orm";
import { nanoid } from "nanoid";
// CREATE
const newUser = await db.insert(users).values({
id: nanoid(),
email: "[email protected]",
name: "John Doe",
}).returning();
// READ - Single
const user = await db.query.users.findFirst({
where: eq(users.email, "[email protected]"),
});
// READ - Multiple with filters
const activeTodos = await db.query.todos.findMany({
where: and(
eq(todos.userId, userId),
eq(todos.completed, false)
),
orderBy: desc(todos.createdAt),
limit: 10,
});
// UPDATE
await db.update(todos)
.set({ completed: true })
.where(eq(todos.id, todoId));
// DELETE
await db.delete(todos)
.where(eq(todos.id, todoId));
// Define relations in schema
import { relations } from "drizzle-orm";
export const usersRelations = relations(users, ({ many }) => ({
todos: many(todos),
}));
export const todosRelations = relations(todos, ({ one }) => ({
user: one(users, {
fields: [todos.userId],
references: [users.id],
}),
}));
// Query with relations
const userWithTodos = await db.query.users.findFirst({
where: eq(users.id, userId),
with: {
todos: {
where: eq(todos.completed, false),
orderBy: desc(todos.createdAt),
},
},
});
{
"scripts": {
"db:local": "turso dev --db-file local.db",
"db:push": "drizzle-kit push",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio"
}
}
| Command | Purpose |
|---|---|
nr db:local | Start local SQLite server |
nr db:push | Push schema changes directly (dev) |
nr db:generate | Generate migration files |
nr db:migrate | Run pending migrations |
nr db:studio | Open Drizzle Studio GUI |
# .env.local
DATABASE_URL=file:local.db
# .env.production
DATABASE_URL=libsql://your-db-name-org.turso.io
DATABASE_AUTH_TOKEN=eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9...
# Install Turso CLI
curl -sSfL https://get.tur.so/install.sh | bash
# Login
turso auth login
# Create database
turso db create my-app-db
# Get connection URL
turso db show my-app-db --url
# Create auth token
turso db tokens create my-app-db
For rapid iteration, use push to sync schema directly:
nr db:push
Generate and apply migration files:
# 1. Generate migration from schema changes
nr db:generate
# 2. Review generated SQL in src/migrations/
# 3. Apply migrations
nr db:migrate
import { nanoid } from "nanoid";
// In insert operations
await db.insert(users).values({
id: nanoid(), // Generates: "V1StGXR8_Z5jdHi6B-myT"
// ...
});
// Auto-set on insert via default
createdAt: integer("created_at", { mode: "timestamp_ms" })
.default(sql`(unixepoch() * 1000)`)
.notNull(),
// Manual update for updatedAt
await db.update(users)
.set({
name: "New Name",
updatedAt: new Date(),
})
.where(eq(users.id, userId));
await db.transaction(async (tx) => {
const [user] = await tx.insert(users).values({
id: nanoid(),
email: "[email protected]",
}).returning();
await tx.insert(todos).values({
id: nanoid(),
title: "Welcome todo",
userId: user.id,
});
});
Cause: Using bun:sqlite in a Node.js environment (Next.js).
Solution: Use @libsql/client instead:
// WRONG
import { Database } from "bun:sqlite";
// CORRECT
import { createClient } from "@libsql/client";
Cause: Multiple connections attempting writes.
Solution: Use WAL mode or connection pooling:
const client = createClient({
url: env.DATABASE_URL,
// Enable connection reuse
syncUrl: env.DATABASE_URL,
});
Cause: Migrations not applied.
Solution:
nr db:push # For dev
# or
nr db:migrate # For production
packages/db/
src/
index.ts # Database client export
schema/
index.ts # All table definitions
users.ts # User table (optional split)
todos.ts # Todo table (optional split)
migrations/ # Generated migration files
drizzle.config.ts # Drizzle Kit configuration
package.json
// Import everything you need
import { db, users, todos } from "@repo/db";
import { eq, and, or, desc, asc, like, sql } from "drizzle-orm";
// Insert
await db.insert(users).values({ ... }).returning();
// Select
await db.query.users.findFirst({ where: eq(users.id, id) });
await db.query.users.findMany({ limit: 10, orderBy: desc(users.createdAt) });
// Update
await db.update(users).set({ ... }).where(eq(users.id, id));
// Delete
await db.delete(users).where(eq(users.id, id));
// Raw SQL
await db.run(sql`VACUUM`);
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub gruckion/marathon-ralph --plugin marathon-ralph8 skills in this plugin
/marathon-ralphAutonomous long-running development from specifications. Use when user wants to build an application from a spec file, run continuous development, or automate feature implementation. Triggers on phrases like "marathon this", "build from spec", "autonomous development", "keep coding until done"./project-detectionDetects project type, package manager, and monorepo structure. Returns correct commands for test/build/lint/dev. Run at project initialization and cache results in state. Use before running any build/test commands./setup-playwrightConfigure Playwright for E2E testing. Use when setting up end-to-end tests, when no E2E framework is detected, or when the user asks to configure browser testing./setup-vitestConfigure Vitest for unit and integration testing. Use when setting up a test framework, when no test runner is detected, or when the user asks to configure testing./update-stateProgrammatically update marathon-ralph state file using deterministic jq commands. Use this instead of manually editing the JSON file./visual-verificationVisually verify implemented features work correctly before marking complete. Use when testing UI changes, verifying web features, or checking user flows work in the browser.Other plugins' skills for the same job