add kv
This commit is contained in:
parent
e8fc0363c8
commit
b7ff711617
|
|
@ -20,6 +20,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"brotli-wasm": "^2.0.1",
|
"brotli-wasm": "^2.0.1",
|
||||||
|
"bun-sqlite-key-value": "^1.4.5",
|
||||||
"exit-hook": "^4.0.0",
|
"exit-hook": "^4.0.0",
|
||||||
"firebase-admin": "^12.2.0",
|
"firebase-admin": "^12.2.0",
|
||||||
"prisma": "^5.17.0"
|
"prisma": "^5.17.0"
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,12 @@
|
||||||
import { $ } from "execa";
|
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import {
|
import { dirAsync, readAsync, removeAsync, writeAsync } from "fs-jetpack";
|
||||||
dirAsync,
|
|
||||||
readAsync,
|
|
||||||
removeAsync,
|
|
||||||
writeAsync
|
|
||||||
} from "fs-jetpack";
|
|
||||||
import { apiContext } from "service-srv";
|
import { apiContext } from "service-srv";
|
||||||
import { deploy } from "utils/deploy";
|
import { deploy } from "utils/deploy";
|
||||||
import { dir } from "utils/dir";
|
import { dir } from "utils/dir";
|
||||||
import { g } from "utils/global";
|
import { g } from "utils/global";
|
||||||
import { genEnv, parseEnv } from "utils/parse-env";
|
import { genEnv, parseEnv } from "utils/parse-env";
|
||||||
import { restartServer } from "utils/restart";
|
import { restartServer } from "utils/restart";
|
||||||
|
import { $ } from "bun";
|
||||||
|
|
||||||
export const _ = {
|
export const _ = {
|
||||||
url: "/_deploy",
|
url: "/_deploy",
|
||||||
|
|
@ -84,7 +79,7 @@ export const _ = {
|
||||||
return "ok";
|
return "ok";
|
||||||
case "db-gen":
|
case "db-gen":
|
||||||
{
|
{
|
||||||
await $({ cwd: dir("app/db") })`bun prisma generate`;
|
await $`bun prisma generate`.cwd(dir("app/db"));
|
||||||
|
|
||||||
res.send("ok");
|
res.send("ok");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
@ -118,9 +113,9 @@ export const _ = {
|
||||||
dir("app/db/.env"),
|
dir("app/db/.env"),
|
||||||
`DATABASE_URL=${ENV.DATABASE_URL}`
|
`DATABASE_URL=${ENV.DATABASE_URL}`
|
||||||
);
|
);
|
||||||
await $({ cwd: dir("app/db") })`bun install`;
|
await $`bun install`.cwd(dir("app/db"));
|
||||||
await $({ cwd: dir("app/db") })`bun prisma db pull --force`;
|
await $`bun prisma db pull --force`.cwd(dir("app/db"));
|
||||||
await $({ cwd: dir("app/db") })`bun prisma generate`;
|
await $`bun prisma generate`.cwd(dir("app/db"));
|
||||||
await Bun.write(
|
await Bun.write(
|
||||||
dir(`${g.datadir}/db-ver`),
|
dir(`${g.datadir}/db-ver`),
|
||||||
Date.now().toString()
|
Date.now().toString()
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import {
|
||||||
removeAsync,
|
removeAsync,
|
||||||
renameAsync,
|
renameAsync,
|
||||||
} from "fs-jetpack";
|
} from "fs-jetpack";
|
||||||
|
|
||||||
export const _ = {
|
export const _ = {
|
||||||
url: "/_file/**",
|
url: "/_file/**",
|
||||||
async api() {
|
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" },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
@ -6,6 +6,7 @@ import { PrismaClient } from "../../app/db/db";
|
||||||
import admin from "firebase-admin";
|
import admin from "firebase-admin";
|
||||||
import { Database } from "bun:sqlite";
|
import { Database } from "bun:sqlite";
|
||||||
import { prodIndex } from "./prod-index";
|
import { prodIndex } from "./prod-index";
|
||||||
|
import { BunSqliteKeyValue } from "bun-sqlite-key-value";
|
||||||
|
|
||||||
type SingleRoute = {
|
type SingleRoute = {
|
||||||
url: string;
|
url: string;
|
||||||
|
|
@ -39,6 +40,7 @@ type PrasiServer = {
|
||||||
|
|
||||||
export const g = global as unknown as {
|
export const g = global as unknown as {
|
||||||
db: PrismaClient;
|
db: PrismaClient;
|
||||||
|
kv: BunSqliteKeyValue;
|
||||||
dburl: string;
|
dburl: string;
|
||||||
datadir: string;
|
datadir: string;
|
||||||
mode: "dev" | "prod";
|
mode: "dev" | "prod";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue