owltide/server/database.ts
Hornwitser fe06d0d6bd
All checks were successful
/ build (push) Successful in 2m5s
/ deploy (push) Successful in 16s
Refactor API types and sync logic
Rename and refactor the types passed over the API to be based on an
entity that's either living or a tombstone.  A living entity has a
deleted property that's either undefined or false, while a tombstone
has a deleted property set to true.  All entities have a numeric id
and an updatedAt timestamp.

To sync entities, an array of replacements are passed around. Living
entities are replaced with tombstones when they're deleted. And
tombstones are replaced with living entities when restored.
2025-06-11 21:05:17 +02:00

99 lines
3 KiB
TypeScript

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";
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");
}
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");
}