diff --git a/bun.lockb b/bun.lockb index 5ac24ee..ba9c6de 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 4ce5398..9cb8d28 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "dependencies": { "brotli-wasm": "^2.0.1", + "bun-sqlite-key-value": "^1.4.5", "exit-hook": "^4.0.0", "firebase-admin": "^12.2.0", "prisma": "^5.17.0" diff --git a/pkgs/api/_deploy.ts b/pkgs/api/_deploy.ts index 0d959a3..47de049 100644 --- a/pkgs/api/_deploy.ts +++ b/pkgs/api/_deploy.ts @@ -1,17 +1,12 @@ -import { $ } from "execa"; import * as fs from "fs"; -import { - dirAsync, - readAsync, - removeAsync, - writeAsync -} from "fs-jetpack"; +import { dirAsync, readAsync, removeAsync, writeAsync } from "fs-jetpack"; import { apiContext } from "service-srv"; import { deploy } from "utils/deploy"; import { dir } from "utils/dir"; import { g } from "utils/global"; import { genEnv, parseEnv } from "utils/parse-env"; import { restartServer } from "utils/restart"; +import { $ } from "bun"; export const _ = { url: "/_deploy", @@ -84,7 +79,7 @@ export const _ = { return "ok"; case "db-gen": { - await $({ cwd: dir("app/db") })`bun prisma generate`; + await $`bun prisma generate`.cwd(dir("app/db")); res.send("ok"); setTimeout(() => { @@ -118,9 +113,9 @@ export const _ = { dir("app/db/.env"), `DATABASE_URL=${ENV.DATABASE_URL}` ); - await $({ cwd: dir("app/db") })`bun install`; - await $({ cwd: dir("app/db") })`bun prisma db pull --force`; - await $({ cwd: dir("app/db") })`bun prisma generate`; + await $`bun install`.cwd(dir("app/db")); + await $`bun prisma db pull --force`.cwd(dir("app/db")); + await $`bun prisma generate`.cwd(dir("app/db")); await Bun.write( dir(`${g.datadir}/db-ver`), Date.now().toString() diff --git a/pkgs/api/_file.ts b/pkgs/api/_file.ts index d3b7bab..83cb898 100644 --- a/pkgs/api/_file.ts +++ b/pkgs/api/_file.ts @@ -11,6 +11,7 @@ import { removeAsync, renameAsync, } from "fs-jetpack"; + export const _ = { url: "/_file/**", async api() { diff --git a/pkgs/api/_kv.ts b/pkgs/api/_kv.ts new file mode 100644 index 0000000..e500a79 --- /dev/null +++ b/pkgs/api/_kv.ts @@ -0,0 +1,48 @@ +import { BunSqliteKeyValue } from "bun-sqlite-key-value"; +import { apiContext } from "service-srv"; +import { dir } from "utils/dir"; +import { g } from "utils/global"; + +export const _ = { + url: "/_kv/**", + raw: true, + async api() { + const { req } = apiContext(this); + + if (!g.kv) { + g.kv = new BunSqliteKeyValue(dir(`${g.datadir}/db-kv.sqlite`)); + } + + try { + const parts = req.params._.split("/"); + switch (parts[0]) { + case "set": { + const body = await req.json(); + if (typeof parts[1] === "string" && typeof body !== "undefined") { + g.kv.set(parts[1], body); + + return new Response(JSON.stringify({ status: "ok" }), { + headers: { "content-type": "application/json" }, + }); + } + + return new Response( + JSON.stringify({ status: "failed", reason: "no key or body" }), + { + headers: { "content-type": "application/json" }, + } + ); + } + case "get": { + return new Response(JSON.stringify(g.kv.get(parts[1])), { + headers: { "content-type": "application/json" }, + }); + } + } + } catch (e) {} + + return new Response(JSON.stringify({ status: "failed" }), { + headers: { "content-type": "application/json" }, + }); + }, +}; diff --git a/pkgs/utils/global.ts b/pkgs/utils/global.ts index d4a9f41..6bc52c8 100644 --- a/pkgs/utils/global.ts +++ b/pkgs/utils/global.ts @@ -6,6 +6,7 @@ import { PrismaClient } from "../../app/db/db"; import admin from "firebase-admin"; import { Database } from "bun:sqlite"; import { prodIndex } from "./prod-index"; +import { BunSqliteKeyValue } from "bun-sqlite-key-value"; type SingleRoute = { url: string; @@ -39,6 +40,7 @@ type PrasiServer = { export const g = global as unknown as { db: PrismaClient; + kv: BunSqliteKeyValue; dburl: string; datadir: string; mode: "dev" | "prod";