add kv
This commit is contained in:
+6
-11
@@ -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()
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
removeAsync,
|
||||
renameAsync,
|
||||
} from "fs-jetpack";
|
||||
|
||||
export const _ = {
|
||||
url: "/_file/**",
|
||||
async api() {
|
||||
|
||||
@@ -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" },
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user