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>
This commit is contained in:
@@ -1,10 +1,26 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
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 drizzle(sql, { schema });
|
||||
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>;
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export { createDb, type Db } from "./client.js";
|
||||
export { createDb, createPgliteDb, type Db } from "./client.js";
|
||||
export * from "./schema/index.js";
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
||||
import { migrate as migratePg } from "drizzle-orm/postgres-js/migrator";
|
||||
import { migrate as migratePglite } from "drizzle-orm/pglite/migrator";
|
||||
import postgres from "postgres";
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import { PGlite } from "@electric-sql/pglite";
|
||||
import { drizzle as drizzlePg } from "drizzle-orm/postgres-js";
|
||||
import { drizzle as drizzlePglite } from "drizzle-orm/pglite";
|
||||
|
||||
const migrationsFolder = new URL("./migrations", import.meta.url).pathname;
|
||||
const url = process.env.DATABASE_URL;
|
||||
if (!url) throw new Error("DATABASE_URL is required");
|
||||
|
||||
const sql = postgres(url, { max: 1 });
|
||||
const db = drizzle(sql);
|
||||
if (url) {
|
||||
const sql = postgres(url, { max: 1 });
|
||||
const db = drizzlePg(sql);
|
||||
await migratePg(db, { migrationsFolder });
|
||||
await sql.end();
|
||||
} else {
|
||||
const client = new PGlite("./data/pglite");
|
||||
const db = drizzlePglite({ client });
|
||||
await migratePglite(db, { migrationsFolder });
|
||||
await client.close();
|
||||
}
|
||||
|
||||
await migrate(db, { migrationsFolder: new URL("./migrations", import.meta.url).pathname });
|
||||
await sql.end();
|
||||
console.log("Migrations complete");
|
||||
|
||||
1
packages/db/src/migrations/meta/_journal.json
Normal file
1
packages/db/src/migrations/meta/_journal.json
Normal file
@@ -0,0 +1 @@
|
||||
{"version":"7","dialect":"postgresql","entries":[]}
|
||||
Reference in New Issue
Block a user