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 <[email protected]>
This commit is contained in:
1 parent
de5735bc8e
commit
13f775c57c
3 files changed
+75
-2
No files matched your search
@@ -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" });
|
||||
}
|
||||
Reference in new issue
Block a user