Files
paperclip/packages/db/src/client.ts
Forgotten 4bc8e8baa9 Add embedded PGlite support as zero-config database option
Add @electric-sql/pglite so the server can run without an external
Postgres instance. When DATABASE_URL is not set, the server auto-creates
an embedded PGlite database in ./data/pglite with schema push on startup.

- Add createPgliteDb() alongside the existing createDb()
- Make DATABASE_URL optional in server config
- Update drizzle config to glob schema files
- Update migrate script to support both Postgres and PGlite
- Add data/ to .gitignore for local PGlite storage

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 19:07:37 -06:00

27 lines
847 B
TypeScript

import { mkdirSync } from "node:fs";
import { drizzle as drizzlePg } from "drizzle-orm/postgres-js";
import { drizzle as drizzlePglite } from "drizzle-orm/pglite";
import postgres from "postgres";
import { PGlite } from "@electric-sql/pglite";
import * as schema from "./schema/index.js";
export function createDb(url: string) {
const sql = postgres(url);
return drizzlePg(sql, { schema });
}
export async function createPgliteDb(dataDir: string) {
mkdirSync(dataDir, { recursive: true });
const client = new PGlite(dataDir);
const db = drizzlePglite({ client, schema });
// Auto-push schema to PGlite on startup (like drizzle-kit push)
const { pushSchema } = await import("drizzle-kit/api");
const { apply } = await pushSchema(schema, db as any);
await apply();
return db;
}
export type Db = ReturnType<typeof createDb>;