owltide/server/database.ts

100 lines
3 KiB
TypeScript
Raw Normal View History

import { readFile, unlink, writeFile } from "node:fs/promises";
import type { ApiAccount, ApiSchedule, ApiSubscription } from "~/shared/types/api";
import { generateDemoSchedule, generateDemoAccounts } from "./generate-demo-schedule";
export interface ServerSession {
id: number,
accountId: number,
};
// For this demo I'm just storing the runtime data in JSON files. When making
// this into proper application this should be replaced with an actual database.
const schedulePath = "data/schedule.json";
const subscriptionsPath = "data/subscriptions.json";
const accountsPath = "data/accounts.json";
2025-03-07 23:53:57 +01:00
const nextAccountIdPath = "data/next-account-id.json";
const sessionsPath = "data/sessions.json";
const nextSessionIdPath = "data/next-session-id.json";
async function remove(path: string) {
try {
await unlink(path);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
}
export async function deleteDatbase() {
await remove(schedulePath);
await remove(subscriptionsPath);
await remove(accountsPath);
await remove(sessionsPath);
}
async function readJson<T>(filePath: string, fallback: T) {
let data: T extends () => infer R ? R : T;
try {
data = JSON.parse(await readFile(filePath, "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT")
throw err;
data = typeof fallback === "function" ? fallback() : fallback;
}
return data;
}
export async function readSchedule() {
return readJson(schedulePath, generateDemoSchedule);
}
export async function writeSchedule(schedule: ApiSchedule) {
await writeFile(schedulePath, JSON.stringify(schedule, undefined, "\t") + "\n", "utf-8");
}
export async function readSubscriptions() {
let subscriptions = await readJson<ApiSubscription[]>(subscriptionsPath, []);
if (subscriptions.length && "keys" in subscriptions[0]) {
// Discard old format
subscriptions = [];
}
return subscriptions;
}
export async function writeSubscriptions(subscriptions: ApiSubscription[]) {
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
}
2025-03-07 23:53:57 +01:00
export async function nextAccountId() {
let nextId = await readJson(nextAccountIdPath, 0);
if (nextId === 0) {
nextId = Math.max(...(await readAccounts()).map(account => account.id), -1) + 1;
}
await writeFile(nextAccountIdPath, String(nextId + 1), "utf-8");
return nextId;
}
export async function readAccounts() {
return await readJson(accountsPath, generateDemoAccounts);
}
export async function writeAccounts(accounts: ApiAccount[]) {
await writeFile(accountsPath, JSON.stringify(accounts, undefined, "\t") + "\n", "utf-8");
}
export async function nextSessionId() {
const nextId = await readJson(nextSessionIdPath, 0);
await writeFile(nextSessionIdPath, String(nextId + 1), "utf-8");
return nextId;
}
export async function readSessions() {
return await readJson<ServerSession[]>(sessionsPath, []);
}
export async function writeSessions(sessions: ServerSession[]) {
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
}