Add DATABASE_URL env var support for EmDash

- Add .env.example documenting SQLite/PostgreSQL/MySQL config
- Add src/lib/db.ts with getDatabaseConfig() helper that parses
  DATABASE_URL and returns the appropriate db config
- Update astro.config.mjs to use getDatabaseConfig() instead of
  hardcoded sqlite path

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Kunthawat Greethong
2026-05-02 16:31:43 +07:00
parent de5735bc8e
commit 13f775c57c
3 changed files with 75 additions and 2 deletions

37
src/lib/db.ts Normal file
View File

@@ -0,0 +1,37 @@
import { sqlite, postgres, mysql } from "emdash/db";
/**
* Database configuration from environment variables.
*
* Supports:
* - SQLite: DATABASE_URL=file:./storage/data.db
* - PostgreSQL: DATABASE_URL=postgresql://user:pass@host:5432/db
* - MySQL: DATABASE_URL=mysql://user:pass@host:3306/db
*/
export function getDatabaseConfig() {
const url = process.env.DATABASE_URL;
if (!url) {
// Default to SQLite
return sqlite({ url: "file:./storage/data.db" });
}
if (url.startsWith("file:")) {
// SQLite
return sqlite({ url: url });
}
if (url.startsWith("postgresql:")) {
// PostgreSQL
return postgres({ connectionString: url });
}
if (url.startsWith("mysql:")) {
// MySQL
return mysql({ connectionString: url });
}
// Fallback to SQLite
console.warn(`Unknown DATABASE_URL protocol, falling back to SQLite`);
return sqlite({ url: "file:./storage/data.db" });
}