mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-07-14 18:15:56 -04:00
Replace separate DATABASE_URL and IMAGE_CACHE_DIR env vars with a single DATA_DIR root (default: ./data, Docker: /data). DATABASE_URL and CACHE_DIR are derived from it but can still be overridden individually. Also: - Pin pnpm to @10 for reproducible Docker builds - Add --link to COPY instructions for better BuildKit cache reuse - Add syntax directive for BuildKit features - Simplify Dockerfile mkdir to just /data (ensureImageDirs handles subdirs) - Remove redundant DATABASE_URL from docker-compose.yml Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { createClient } from "@libsql/client";
|
|
import { drizzle } from "drizzle-orm/libsql";
|
|
import * as schema from "./schema";
|
|
|
|
const globalForDb = globalThis as unknown as {
|
|
_db: ReturnType<typeof drizzle> | undefined;
|
|
_client: ReturnType<typeof createClient> | undefined;
|
|
};
|
|
|
|
const DATA_DIR = process.env.DATA_DIR || "./data";
|
|
const DATABASE_URL = process.env.DATABASE_URL || `file:${DATA_DIR}/sqlite.db`;
|
|
|
|
function getClient() {
|
|
if (!globalForDb._client) {
|
|
globalForDb._client = createClient({
|
|
url: DATABASE_URL,
|
|
});
|
|
globalForDb._client.execute("PRAGMA journal_mode = WAL");
|
|
globalForDb._client.execute("PRAGMA foreign_keys = ON");
|
|
globalForDb._client.execute("PRAGMA busy_timeout = 5000");
|
|
}
|
|
return globalForDb._client;
|
|
}
|
|
|
|
function getDb() {
|
|
if (!globalForDb._db) {
|
|
globalForDb._db = drizzle({ client: getClient(), schema });
|
|
}
|
|
return globalForDb._db;
|
|
}
|
|
|
|
export const db = new Proxy({} as ReturnType<typeof drizzle>, {
|
|
get(_, prop) {
|
|
return Reflect.get(getDb(), prop);
|
|
},
|
|
});
|
|
|
|
export const client = new Proxy({} as ReturnType<typeof createClient>, {
|
|
get(_, prop) {
|
|
return Reflect.get(getClient(), prop);
|
|
},
|
|
});
|