2023-12-10 12:23:42 +00:00
|
|
|
import { DenoKvPrimitives } from "../plugos/lib/deno_kv_primitives.ts";
|
|
|
|
import { KvPrimitives } from "../plugos/lib/kv_primitives.ts";
|
2023-12-13 16:52:56 +00:00
|
|
|
import { MemoryKvPrimitives } from "../plugos/lib/memory_kv_primitives.ts";
|
2023-12-10 12:23:42 +00:00
|
|
|
import { path } from "./deps.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Environment variables:
|
2023-12-22 18:06:36 +00:00
|
|
|
* - SB_DB_BACKEND: "kv" or "memory" (default: kv)
|
|
|
|
* - SB_KV_DB (kv only): path to the database file (default .silverbullet.db)
|
2023-12-10 12:23:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
export async function determineDatabaseBackend(
|
|
|
|
singleTenantFolder?: string,
|
2023-12-13 16:52:56 +00:00
|
|
|
): Promise<KvPrimitives> {
|
2023-12-22 18:06:36 +00:00
|
|
|
const backendConfig = Deno.env.get("SB_DB_BACKEND") || "kv";
|
2023-12-10 12:23:42 +00:00
|
|
|
switch (backendConfig) {
|
2023-12-22 18:06:36 +00:00
|
|
|
case "kv": {
|
2023-12-10 12:23:42 +00:00
|
|
|
let dbFile: string | undefined = Deno.env.get("SB_KV_DB") ||
|
|
|
|
".silverbullet.db";
|
|
|
|
|
|
|
|
if (singleTenantFolder) {
|
|
|
|
// If we're running in single tenant mode, we may as well use the tenant's space folder to keep the database
|
|
|
|
dbFile = path.resolve(singleTenantFolder, dbFile);
|
|
|
|
}
|
|
|
|
|
2023-12-13 16:52:56 +00:00
|
|
|
if (Deno.env.get("DENO_DEPLOYMENT_ID") !== undefined) { // We're running in Deno Deploy
|
2023-12-10 12:23:42 +00:00
|
|
|
dbFile = undefined; // Deno Deploy will use the default KV store
|
|
|
|
}
|
|
|
|
const denoDb = await Deno.openKv(dbFile);
|
|
|
|
console.info(
|
2023-12-22 18:06:36 +00:00
|
|
|
`Using KV as a database backend (${dbFile || "cloud"}).`,
|
2023-12-10 12:23:42 +00:00
|
|
|
);
|
|
|
|
return new DenoKvPrimitives(denoDb);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
console.info(
|
2023-12-13 16:52:56 +00:00
|
|
|
"Running in in-memory database mode: index data will be flushed on every restart. Not recommended, but to each their own.",
|
2023-12-10 12:23:42 +00:00
|
|
|
);
|
2023-12-13 16:52:56 +00:00
|
|
|
return new MemoryKvPrimitives();
|
2023-12-10 12:23:42 +00:00
|
|
|
}
|
|
|
|
}
|