Refactor code saving data

Move the code dealing with saving and loading data to server/database to
gather it all up into one place.
This commit is contained in:
Hornwitser 2025-03-05 18:41:47 +01:00
parent 754d175ce2
commit 6ea3567c94
8 changed files with 64 additions and 58 deletions

View file

@ -1,11 +1,11 @@
import { Schedule } from "~/shared/types/schedule"; import { Schedule } from "~/shared/types/schedule";
import { readFile, writeFile } from "node:fs/promises";
import { broadcastUpdate } from "~/server/streams"; import { broadcastUpdate } from "~/server/streams";
import { sendPush } from "~/server/web-push"; import { sendPush } from "~/server/web-push";
import { readSchedule, writeSchedule } from "~/server/database";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const formData = await readFormData(event); const formData = await readFormData(event);
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8")); const schedule: Schedule = await readSchedule();
const id = formData.get("id") as string; const id = formData.get("id") as string;
const name = formData.get("name") as string; const name = formData.get("name") as string;
const description = formData.get("description") as string; const description = formData.get("description") as string;
@ -26,6 +26,6 @@ export default defineEventHandler(async (event) => {
] ]
}); });
broadcastUpdate(schedule); broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8"); await writeSchedule(schedule);
await sendPush("New event", `${name} will start at ${start}`); await sendPush("New event", `${name} will start at ${start}`);
}); });

View file

@ -1,10 +1,10 @@
import { Schedule } from "~/shared/types/schedule"; import { Schedule } from "~/shared/types/schedule";
import { readFile, writeFile } from "fs/promises";
import { broadcastUpdate } from "~/server/streams"; import { broadcastUpdate } from "~/server/streams";
import { readSchedule, writeSchedule } from "~/server/database";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const formData = await readFormData(event); const formData = await readFormData(event);
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8")); const schedule: Schedule = await readSchedule();
const id = formData.get("id") as string; const id = formData.get("id") as string;
const index = schedule.events.findIndex(event => event.id === id); const index = schedule.events.findIndex(event => event.id === id);
if (index === -1) { if (index === -1) {
@ -12,5 +12,5 @@ export default defineEventHandler(async (event) => {
} }
schedule.events.splice(index, 1); schedule.events.splice(index, 1);
broadcastUpdate(schedule); broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8"); await writeSchedule(schedule);
}); });

View file

@ -1,11 +1,11 @@
import { Schedule } from "~/shared/types/schedule"; import { Schedule } from "~/shared/types/schedule";
import { readFile, writeFile } from "fs/promises";
import { broadcastUpdate } from "~/server/streams"; import { broadcastUpdate } from "~/server/streams";
import { sendPush } from "~/server/web-push"; import { sendPush } from "~/server/web-push";
import { readSchedule, writeSchedule } from "~/server/database";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const formData = await readFormData(event); const formData = await readFormData(event);
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8")); const schedule: Schedule = await readSchedule();
const id = formData.get("id") as string; const id = formData.get("id") as string;
const name = formData.get("name") as string; const name = formData.get("name") as string;
const description = formData.get("description") as string; const description = formData.get("description") as string;
@ -31,7 +31,7 @@ export default defineEventHandler(async (event) => {
] ]
}; };
broadcastUpdate(schedule); broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8"); await writeSchedule(schedule);
if (timeChanged) if (timeChanged)
await sendPush(`New time for ${name}`, `${name} will now start at ${start}`); await sendPush(`New time for ${name}`, `${name} will now start at ${start}`);
}); });

View file

@ -1,6 +1,5 @@
import { readFile } from "node:fs/promises"; import { readSchedule } from "~/server/database";
import { Schedule } from "~/shared/types/schedule";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
return JSON.parse(await readFile("schedule.json", "utf-8")) as Schedule; return await readSchedule();
}) })

View file

@ -1,27 +1,15 @@
import { readFile, writeFile } from "node:fs/promises"; import { readSubscriptions, writeSubscriptions } from "~/server/database";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body: { subscription: PushSubscriptionJSON } = await readBody(event); const body: { subscription: PushSubscriptionJSON } = await readBody(event);
let subscriptions: PushSubscriptionJSON[]; const subscriptions = await readSubscriptions();
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
subscriptions = [];
}
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint); const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
if (existingIndex !== -1) { if (existingIndex !== -1) {
subscriptions[existingIndex] = body.subscription; subscriptions[existingIndex] = body.subscription;
} else { } else {
subscriptions.push(body.subscription); subscriptions.push(body.subscription);
} }
await writeFile( await writeSubscriptions(subscriptions);
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
if (existingIndex !== -1) { if (existingIndex !== -1) {
return { message: "Existing subscription refreshed."}; return { message: "Existing subscription refreshed."};
} }

View file

@ -1,26 +1,14 @@
import { readFile, writeFile } from "fs/promises"; import { readSubscriptions, writeSubscriptions } from "~/server/database";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body: { subscription: PushSubscriptionJSON } = await readBody(event); const body: { subscription: PushSubscriptionJSON } = await readBody(event);
let subscriptions: PushSubscriptionJSON[]; const subscriptions = await readSubscriptions();
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
subscriptions = [];
}
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint); const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
if (existingIndex !== -1) { if (existingIndex !== -1) {
subscriptions.splice(existingIndex, 1); subscriptions.splice(existingIndex, 1);
} else { } else {
return { message: "No subscription registered."}; return { message: "No subscription registered."};
} }
await writeFile( await writeSubscriptions(subscriptions);
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
return { message: "Existing subscription removed."}; return { message: "Existing subscription removed."};
}) })

44
server/database.ts Normal file
View file

@ -0,0 +1,44 @@
import { readFile, writeFile } from "node:fs/promises";
import { Schedule } from "~/shared/types/schedule";
// 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 = "schedule.json"
const subscriptionsPath = "push-subscriptions.json"
export async function readSchedule() {
let schedule: Schedule;
try {
schedule = JSON.parse(await readFile(schedulePath, "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT")
throw err;
// Use an empty schedule if nothing is stored yet.
schedule = {
locations: [],
events: [],
}
}
return schedule;;
}
export async function writeSchedule(schedule: Schedule) {
await writeFile(schedulePath, JSON.stringify(schedule, undefined, "\t") + "\n", "utf-8");
}
export async function readSubscriptions() {
let subscriptions: PushSubscriptionJSON[];
try {
subscriptions = JSON.parse(await readFile(subscriptionsPath, "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT")
throw err;
subscriptions = [];
}
return subscriptions;;
}
export async function writeSubscriptions(subscriptions: PushSubscriptionJSON[]) {
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
}

View file

@ -1,5 +1,5 @@
import { readFile, writeFile } from "node:fs/promises";
import webPush from "web-push"; import webPush from "web-push";
import { readSubscriptions, writeSubscriptions } from "~/server/database";
webPush.setVapidDetails( webPush.setVapidDetails(
"mailto:webmaster@hornwitser.no", "mailto:webmaster@hornwitser.no",
@ -9,16 +9,7 @@ webPush.setVapidDetails(
export async function sendPush(title: string, body: string) { export async function sendPush(title: string, body: string) {
const payload = JSON.stringify({ title, body }); const payload = JSON.stringify({ title, body });
let subscriptions: PushSubscriptionJSON[]; const subscriptions = await readSubscriptions();
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
console.log(`Dropping "${payload}", no push subscribers`);
return;
}
subscriptions = [];
}
console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`); console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`);
const removeIndexes = []; const removeIndexes = [];
for (let index = 0; index < subscriptions.length; index += 1) { for (let index = 0; index < subscriptions.length; index += 1) {
@ -46,11 +37,7 @@ export async function sendPush(title: string, body: string) {
for (const index of removeIndexes) { for (const index of removeIndexes) {
subscriptions.splice(index, 1); subscriptions.splice(index, 1);
} }
await writeFile( await writeSubscriptions(subscriptions);
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
} }
console.log("Push notices sent"); console.log("Push notices sent");
} }