Implement database administration

Add routes and admin panel elements for creating a database backup,
restoring from a backup, deleting the existing schedule, and replacing
the database with the demo schedule.  These server as crude ways to
manage the data stored in the system.
This commit is contained in:
Hornwitser 2025-06-28 01:23:52 +02:00
parent b2f48e98e0
commit e5e923bc8d
6 changed files with 190 additions and 5 deletions

View file

@ -1,6 +1,5 @@
import { readFile, unlink, writeFile } from "node:fs/promises";
import type { ApiSchedule, ApiSubscription, ApiUserType } from "~/shared/types/api";
import { generateDemoSchedule, generateDemoAccounts } from "~/server/generate-demo-schedule";
import type { Id } from "~/shared/types/common";
export interface ServerSession {
@ -65,7 +64,10 @@ async function readJson<T>(filePath: string, fallback: T) {
}
export async function readSchedule() {
return readJson(schedulePath, generateDemoSchedule);
return readJson(schedulePath, (): ApiSchedule => ({
id: 111,
updatedAt: new Date().toISOString(),
}));
}
export async function writeSchedule(schedule: ApiSchedule) {
@ -85,6 +87,14 @@ export async function writeSubscriptions(subscriptions: ApiSubscription[]) {
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
}
export async function readNextUserId() {
return await readJson(nextUserIdPath, 0);
}
export async function writeNextUserId(nextId: number) {
await writeFile(nextUserIdPath, String(nextId), "utf-8");
}
export async function nextUserId() {
let nextId = await readJson(nextUserIdPath, 0);
if (nextId === 0) {
@ -95,13 +105,21 @@ export async function nextUserId() {
}
export async function readUsers() {
return await readJson(usersPath, generateDemoAccounts as () => ServerUser[]);
return await readJson(usersPath, (): ServerUser[] => []);
}
export async function writeUsers(users: ServerUser[]) {
await writeFile(usersPath, JSON.stringify(users, undefined, "\t") + "\n", "utf-8");
}
export async function readNextSessionId() {
return await readJson(nextSessionIdPath, 0);
}
export async function writeNextSessionId(nextId: number) {
await writeFile(nextSessionIdPath, String(nextId), "utf-8");
}
export async function nextSessionId() {
const nextId = await readJson(nextSessionIdPath, 0);
await writeFile(nextSessionIdPath, String(nextId + 1), "utf-8");