owltide/server/database.ts
Hornwitser 251e83f640
All checks were successful
/ build (push) Successful in 1m12s
/ deploy (push) Successful in 16s
Rename AcountSession to ServerSession
Start the work of clearly distingushing client side types, server side
types and types shared over the API by renaming "AccountSession" and
"Session" names used on the server to "ServerSession".
2025-06-09 16:51:05 +02:00

100 lines
3 KiB
TypeScript

import { readFile, unlink, writeFile } from "node:fs/promises";
import { Schedule } from "~/shared/types/schedule";
import { Account, Subscription } from "~/shared/types/account";
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";
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: Schedule) {
await writeFile(schedulePath, JSON.stringify(schedule, undefined, "\t") + "\n", "utf-8");
}
export async function readSubscriptions() {
let subscriptions = await readJson<Subscription[]>(subscriptionsPath, []);
if (subscriptions.length && "keys" in subscriptions[0]) {
// Discard old format
subscriptions = [];
}
return subscriptions;
}
export async function writeSubscriptions(subscriptions: Subscription[]) {
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
}
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: Account[]) {
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");
}