Fixing bug on collection deletion

Extending Admin Interface
Adding cleanup procedure, that clears undeleted collection data
This commit is contained in:
Fabian Stamm
2019-11-07 01:27:56 +01:00
parent b3932aa54d
commit 1f193fd5a1
8 changed files with 284 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import { DatabaseManager } from "../../database/database";
import { MP } from "../../database/query";
import config from "../../config";
import Logging from "@hibas123/logging";
import { getView } from "../helper/hb";
const AdminRoute = new Router();
@ -17,6 +18,11 @@ AdminRoute.use(async (ctx, next) => {
return next();
})
AdminRoute.get("/", async ctx => {
//TODO: Main Interface
ctx.body = getView("admin");
});
AdminRoute.get("/settings", async ctx => {
let res = await new Promise<string[][]>((yes, no) => {
const stream = Settings.db.createReadStream({
@ -51,11 +57,11 @@ AdminRoute.get("/data", async ctx => {
keys: true,
values: true,
valueAsBuffer: true,
keyAsBuffer: false
keyAsBuffer: false,
limit: 1000
});
let res = [["key", "value"]];
stream.on("data", ({ key, value }: { key: string, value: Buffer }) => {
Logging.debug("Admin Key:", key);
res.push([key, key.split("/").length > 2 ? value.toString() : JSON.stringify(MP.decode(value))]);
})
@ -114,6 +120,48 @@ AdminRoute
ctx.body = "Success";
})
AdminRoute.get("/collections", async ctx => {
const { database } = ctx.query;
let db = DatabaseManager.getDatabase(database);
if (!db)
throw new BadRequestError("Database not found");
let res = await new Promise<string[]>((yes, no) => {
const stream = db.collections.createKeyStream({
keyAsBuffer: false,
limit: 1000
});
let res = [];
stream.on("data", (key: string) => {
res.push(key);
})
stream.on("error", no);
stream.on("end", () => yes(res))
})
if (ctx.query.view) {
return getTable("Databases", res, ctx);
} else {
ctx.body = res;
}
})
AdminRoute.get("/collections/cleanup", async ctx => {
const { database } = ctx.query;
let db = DatabaseManager.getDatabase(database);
if (!db)
throw new BadRequestError("Database not found");
let deleted = await db.runCleanup();
if (ctx.query.view) {
return getTable("Databases", deleted, ctx);
} else {
ctx.body = deleted;
}
})
AdminRoute.get("/database/new", getForm("/v1/admin/database", "New/Change Database", {
name: { label: "Name", type: "text", },
accesskey: { label: "Access Key", type: "text" },