2026-02-16 19:07:37 -06:00
|
|
|
import { mkdirSync } from "node:fs";
|
|
|
|
|
import { drizzle as drizzlePg } from "drizzle-orm/postgres-js";
|
|
|
|
|
import { drizzle as drizzlePglite } from "drizzle-orm/pglite";
|
2026-02-16 13:31:52 -06:00
|
|
|
import postgres from "postgres";
|
2026-02-16 19:07:37 -06:00
|
|
|
import { PGlite } from "@electric-sql/pglite";
|
2026-02-16 13:31:52 -06:00
|
|
|
import * as schema from "./schema/index.js";
|
|
|
|
|
|
|
|
|
|
export function createDb(url: string) {
|
|
|
|
|
const sql = postgres(url);
|
2026-02-16 19:07:37 -06:00
|
|
|
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;
|
2026-02-16 13:31:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Db = ReturnType<typeof createDb>;
|