first commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* A process-wide database registry. The framework configures it at server
|
||||
* startup from `wrnexus.config.ts`: the `db` setting becomes the **default**
|
||||
* connection, and each entry under `databases` becomes a **named** connection.
|
||||
* Pages and API routes then call `getDb()` for the default, or `getDb("<name>")`
|
||||
* for a named one, to run queries (including the generated typed functions).
|
||||
*
|
||||
* const users = await getDb().all("SELECT * FROM users"); // default db
|
||||
* const events = await getDb("analytics").all("SELECT * FROM hits"); // named db
|
||||
*/
|
||||
|
||||
import type { Db } from "./driver.ts";
|
||||
|
||||
const DEFAULT = "default";
|
||||
const registry = new Map<string, Db>();
|
||||
|
||||
/** Set the default database (called by the runtime at startup). */
|
||||
export function setDb(db: Db): Db;
|
||||
/** Set a named database (from `databases.<name>` in config). */
|
||||
export function setDb(name: string, db: Db): Db;
|
||||
export function setDb(a: string | Db, b?: Db): Db {
|
||||
const name = typeof a === "string" ? a : DEFAULT;
|
||||
const db = typeof a === "string" ? b! : a;
|
||||
registry.set(name, db);
|
||||
return db;
|
||||
}
|
||||
|
||||
/** Register a named database. Alias of `setDb(name, db)` for readability. */
|
||||
export function registerDb(name: string, db: Db): Db {
|
||||
return setDb(name, db);
|
||||
}
|
||||
|
||||
/** The default database, or a named one. Throws if it isn't configured. */
|
||||
export function getDb(name = DEFAULT): Db {
|
||||
const db = registry.get(name);
|
||||
if (!db) {
|
||||
throw new Error(
|
||||
name === DEFAULT
|
||||
? "No database configured. Add `db: { driver, url }` to wrnexus.config.ts."
|
||||
: `No database named '${name}'. Add it under \`databases\` in wrnexus.config.ts ` +
|
||||
`(e.g. databases: { ${name}: { driver, url } }).`,
|
||||
);
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
/** Whether the default (or a named) database has been configured. */
|
||||
export function hasDb(name = DEFAULT): boolean {
|
||||
return registry.has(name);
|
||||
}
|
||||
|
||||
/** Names of all configured databases (the default appears as "default"). */
|
||||
export function databaseNames(): string[] {
|
||||
return [...registry.keys()];
|
||||
}
|
||||
|
||||
/** Close every configured database and clear the registry. */
|
||||
export async function closeDatabases(): Promise<void> {
|
||||
for (const db of registry.values()) await db.close();
|
||||
registry.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user