mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
chore: updated cf deps, improved vite dev
This commit is contained in:
106
app/vite.dev.ts
106
app/vite.dev.ts
@@ -1,76 +1,62 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { showRoutes } from "hono/dev";
|
||||
import { App, registries } from "./src";
|
||||
import { App, registries, type CreateAppConfig } from "./src";
|
||||
import { StorageLocalAdapter } from "./src/adapter/node";
|
||||
import type { Connection } from "./src/data/connection/Connection";
|
||||
import { __bknd } from "modules/db/DbModuleManager";
|
||||
import { nodeSqlite } from "./src/adapter/node/connection/NodeSqliteConnection";
|
||||
import { libsql } from "./src/data/connection/sqlite/libsql/LibsqlConnection";
|
||||
import { $console } from "core/utils/console";
|
||||
import { createClient } from "@libsql/client";
|
||||
import util from "node:util";
|
||||
import { d1Sqlite } from "adapter/cloudflare/connection/D1Connection";
|
||||
|
||||
util.inspect.defaultOptions.depth = 5;
|
||||
|
||||
registries.media.register("local", StorageLocalAdapter);
|
||||
|
||||
const config: CreateAppConfig = {};
|
||||
|
||||
const dbType = import.meta.env.VITE_DB_TYPE ?? "node";
|
||||
$console.debug("Using db type", dbType);
|
||||
|
||||
let dbUrl = import.meta.env.VITE_DB_URL ?? ":memory:";
|
||||
|
||||
const example = import.meta.env.VITE_EXAMPLE;
|
||||
|
||||
let connection: Connection;
|
||||
|
||||
if (import.meta.env.VITE_DB_LIBSQL_URL) {
|
||||
connection = libsql(
|
||||
createClient({
|
||||
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 {
|
||||
const dbUrl = example ? `file:.configs/${example}.db` : import.meta.env.VITE_DB_URL;
|
||||
if (dbUrl) {
|
||||
connection = nodeSqlite({ url: dbUrl });
|
||||
$console.debug("Using node-sqlite connection", dbUrl);
|
||||
} else if (import.meta.env.VITE_DB_LIBSQL_URL) {
|
||||
connection = libsql(
|
||||
createClient({
|
||||
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) {
|
||||
const configPath = `.configs/${example}.json`;
|
||||
$console.debug("Loading config from", configPath);
|
||||
const exampleConfig = JSON.parse(await readFile(configPath, "utf-8"));
|
||||
config.config = exampleConfig;
|
||||
dbUrl = `file:.configs/${example}.db`;
|
||||
}
|
||||
|
||||
/* if (example) {
|
||||
const { version, ...config } = JSON.parse(await readFile(`.configs/${example}.json`, "utf-8"));
|
||||
|
||||
// create db with config
|
||||
const conn = new LibsqlConnection(credentials);
|
||||
const em = new EntityManager([__bknd], conn);
|
||||
try {
|
||||
await em.schema().sync({ force: true });
|
||||
} catch (e) {}
|
||||
const { data: existing } = await em.repo(__bknd).findOne({ type: "config" });
|
||||
|
||||
if (!existing || existing.version !== version) {
|
||||
if (existing) await em.mutator(__bknd).deleteOne(existing.id);
|
||||
await em.mutator(__bknd).insertOne({
|
||||
version,
|
||||
json: config,
|
||||
created_at: new Date(),
|
||||
type: "config",
|
||||
});
|
||||
} else {
|
||||
await em.mutator(__bknd).updateOne(existing.id, {
|
||||
json: config,
|
||||
});
|
||||
switch (dbType) {
|
||||
case "libsql": {
|
||||
$console.debug("Using libsql connection", dbUrl);
|
||||
const authToken = import.meta.env.VITE_DB_LIBSQL_TOKEN;
|
||||
config.connection = libsql(
|
||||
createClient({
|
||||
url: dbUrl,
|
||||
authToken,
|
||||
}),
|
||||
);
|
||||
break;
|
||||
}
|
||||
} */
|
||||
case "d1": {
|
||||
$console.debug("Using d1 connection");
|
||||
|
||||
const { getPlatformProxy } = await import("wrangler");
|
||||
const platformProxy = await getPlatformProxy({
|
||||
configPath: "./vite.wrangler.json",
|
||||
});
|
||||
config.connection = d1Sqlite({ binding: platformProxy.env.DB as any });
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
$console.debug("Using node-sqlite connection", dbUrl);
|
||||
config.connection = nodeSqlite({ url: dbUrl });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let app: App;
|
||||
const recreate = import.meta.env.VITE_APP_FRESH === "1";
|
||||
@@ -79,9 +65,9 @@ let firstStart = true;
|
||||
export default {
|
||||
async fetch(request: Request) {
|
||||
if (!app || recreate) {
|
||||
app = App.create({
|
||||
connection,
|
||||
});
|
||||
const sync = !!(firstStart && example);
|
||||
|
||||
app = App.create(config);
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
async () => {
|
||||
@@ -91,7 +77,7 @@ export default {
|
||||
"sync",
|
||||
);
|
||||
await app.build({
|
||||
sync: !!(firstStart && example),
|
||||
sync,
|
||||
});
|
||||
|
||||
// log routes
|
||||
|
||||
Reference in New Issue
Block a user