feat: show D1 storage capacity in admin (#1117)

* feat: show D1 storage usage in admin

* test: add D1 storage E2E coverage

* test: use exact D1 storage labels
This commit is contained in:
Dream Hunter
2026-08-19 16:16:54 +08:00
committed by GitHub
parent 2762741226
commit 108b8ef4ac
13 changed files with 468 additions and 5 deletions
+31
View File
@@ -0,0 +1,31 @@
import { Context } from "hono";
import { getSetting, saveSetting } from "../utils";
const CONFIG_KEY_PREFIX = "admin-config:";
const CONFIG_KEY_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/;
const getStorageKey = (key: string) => `${CONFIG_KEY_PREFIX}${key}`;
export default {
get: async (c: Context<HonoCustomType>) => {
const key = c.req.param("key");
if (!CONFIG_KEY_PATTERN.test(key)) {
return c.text("Invalid config key", 400);
}
const value = await getSetting(c, getStorageKey(key));
return c.json({ key, value });
},
save: async (c: Context<HonoCustomType>) => {
const { key, value } = await c.req.json<{ key?: unknown, value?: unknown }>();
if (typeof key !== "string" || !CONFIG_KEY_PATTERN.test(key)) {
return c.text("Invalid config key", 400);
}
if (typeof value !== "string") {
return c.text("Config value must be a string", 400);
}
await saveSetting(c, getStorageKey(key), value);
return c.json({ success: true, key, value });
},
}
+3 -1
View File
@@ -219,11 +219,13 @@ export default {
},
getVersion: async (c: Context<HonoCustomType>) => {
const version = await utils.getSetting(c, CONSTANTS.DB_VERSION_KEY);
const sizeResult = await c.env.DB.prepare("SELECT 1").run();
return c.json({
need_initialization: !version,
need_migration: version && version != CONSTANTS.DB_VERSION,
current_db_version: version,
code_db_version: CONSTANTS.DB_VERSION
code_db_version: CONSTANTS.DB_VERSION,
database_size: sizeResult.meta.size_after ?? null
});
},
}
+5
View File
@@ -18,6 +18,7 @@ import db_api from './db_api'
import ip_blacklist_settings from './ip_blacklist_settings'
import ai_extract_settings from './ai_extract_settings'
import e2e_test_api from './e2e_test_api'
import config_api from './config_api'
export const api = new Hono<HonoCustomType>()
@@ -96,6 +97,10 @@ api.get('admin/db_version', db_api.getVersion)
api.post('admin/db_initialize', db_api.initialize)
api.post('admin/db_migration', db_api.migrate)
// generic admin config
api.get('/admin/config/:key', config_api.get)
api.post('/admin/config', config_api.save)
// IP blacklist settings
api.get('/admin/ip_blacklist/settings', ip_blacklist_settings.getIpBlacklistSettings)
api.post('/admin/ip_blacklist/settings', ip_blacklist_settings.saveIpBlacklistSettings)