mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 12:37:20 +00:00
feat: adding env aware endpoint to obtain sqlite connection, remove libsql hard dependency
This commit is contained in:
@@ -3,30 +3,33 @@ import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { showRoutes } from "hono/dev";
|
||||
import { App, registries } from "./src";
|
||||
import { StorageLocalAdapter } from "./src/adapter/node";
|
||||
import { EntityManager, LibsqlConnection } from "data";
|
||||
import type { Connection } from "./src/data/connection/Connection";
|
||||
import { __bknd } from "modules/ModuleManager";
|
||||
import { nodeSqlite } from "./src/adapter/node/connection/NodeSqliteConnection";
|
||||
import { libsql } from "./src/data/connection/sqlite/LibsqlConnection";
|
||||
import { $console } from "core";
|
||||
//import { DatabaseSync } from "node:sqlite";
|
||||
//import { nodeSqlite } from "./src/adapter/node/connection/NodeSqliteConnection";
|
||||
|
||||
registries.media.register("local", StorageLocalAdapter);
|
||||
|
||||
const example = import.meta.env.VITE_EXAMPLE;
|
||||
const dbUrl = example ? `file:.configs/${example}.db` : import.meta.env.VITE_DB_URL;
|
||||
|
||||
const credentials = example
|
||||
? {
|
||||
url: `file:.configs/${example}.db`,
|
||||
}
|
||||
: import.meta.env.VITE_DB_URL
|
||||
? {
|
||||
url: import.meta.env.VITE_DB_URL!,
|
||||
authToken: import.meta.env.VITE_DB_TOKEN!,
|
||||
}
|
||||
: {
|
||||
url: ":memory:",
|
||||
};
|
||||
let connection: Connection;
|
||||
if (dbUrl) {
|
||||
connection = nodeSqlite({ url: dbUrl });
|
||||
$console.debug("Using node-sqlite connection", dbUrl);
|
||||
} else if (import.meta.env.VITE_DB_LIBSQL_URL) {
|
||||
connection = libsql({
|
||||
url: import.meta.env.VITE_DB_LIBSQL_URL!,
|
||||
authToken: import.meta.env.VITE_DB_LIBSQL_TOKEN!,
|
||||
});
|
||||
$console.debug("Using libsql connection", import.meta.env.VITE_DB_URL);
|
||||
} else {
|
||||
connection = nodeSqlite();
|
||||
$console.debug("No connection provided, using in-memory database");
|
||||
}
|
||||
|
||||
if (example) {
|
||||
/* if (example) {
|
||||
const { version, ...config } = JSON.parse(await readFile(`.configs/${example}.json`, "utf-8"));
|
||||
|
||||
// create db with config
|
||||
@@ -50,7 +53,7 @@ if (example) {
|
||||
json: config,
|
||||
});
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
let app: App;
|
||||
const recreate = import.meta.env.VITE_APP_FRESH === "1";
|
||||
@@ -60,8 +63,7 @@ export default {
|
||||
async fetch(request: Request) {
|
||||
if (!app || recreate) {
|
||||
app = App.create({
|
||||
connection: credentials,
|
||||
//connection: nodeSqlite({ database: new DatabaseSync(":memory:") }),
|
||||
connection,
|
||||
});
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
@@ -78,15 +80,11 @@ export default {
|
||||
// log routes
|
||||
if (firstStart) {
|
||||
firstStart = false;
|
||||
// biome-ignore lint/suspicious/noConsoleLog:
|
||||
console.log("[DB]", credentials);
|
||||
|
||||
if (import.meta.env.VITE_SHOW_ROUTES === "1") {
|
||||
// biome-ignore lint/suspicious/noConsoleLog:
|
||||
console.log("\n[APP ROUTES]");
|
||||
console.info("\n[APP ROUTES]");
|
||||
showRoutes(app.server);
|
||||
// biome-ignore lint/suspicious/noConsoleLog:
|
||||
console.log("-------\n");
|
||||
console.info("-------\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user