feat: adding env aware endpoint to obtain sqlite connection, remove libsql hard dependency

This commit is contained in:
dswbx
2025-06-13 11:09:47 +02:00
parent 046c1d21b1
commit bbb7bfb7a1
28 changed files with 288 additions and 159 deletions

View File

@@ -2,9 +2,16 @@ import path from "node:path";
import { serve as honoServe } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import { registerLocalMediaAdapter } from "adapter/node/index";
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
import {
type RuntimeBkndConfig,
createRuntimeApp,
type RuntimeOptions,
Connection,
} from "bknd/adapter";
import { config as $config } from "bknd/core";
import { $console } from "core";
import { sqlite } from "bknd/adapter/sqlite";
import type { App } from "App";
type NodeEnv = NodeJS.ProcessEnv;
export type NodeBkndConfig<Env = NodeEnv> = RuntimeBkndConfig<Env> & {
@@ -28,10 +35,18 @@ export async function createApp<Env = NodeEnv>(
console.warn("relativeDistPath is deprecated, please use distPath instead");
}
let connection: Connection | undefined;
if (Connection.isConnection(config.connection)) {
connection = config.connection;
} else {
connection = sqlite(config.connection ?? { url: ":memory:" });
}
registerLocalMediaAdapter();
return await createRuntimeApp(
{
...config,
connection,
serveStatic: serveStatic({ root }),
},
// @ts-ignore
@@ -45,8 +60,11 @@ export function createHandler<Env = NodeEnv>(
args: Env = {} as Env,
opts?: RuntimeOptions,
) {
let app: App | undefined;
return async (req: Request) => {
const app = await createApp(config, args ?? (process.env as Env), opts);
if (!app) {
app = await createApp(config, args ?? (process.env as Env), opts);
}
return app.fetch(req);
};
}