Files
bewcloud/lib/models/dashboard.ts
Bruno Bernardino 6cfb62d1a2 Refactor data handlers + misc fixes
This refactors the data handlers into a more standard/understood model-like architecture, to prepare for a new, more robust config system.

It also fixes a problem with creating new Notes and uploading new Photos via the web interface (related to #58).

Finally, it speeds up docker builds by sending in less files, which aren't necessary or will be built anyway.

This is all in preparation to allow building #13 more robustly.
2025-05-24 08:24:10 +01:00

46 lines
1.0 KiB
TypeScript

import Database, { sql } from '/lib/interfaces/database.ts';
import { Dashboard } from '/lib/types.ts';
const db = new Database();
export class DashboardModel {
static async getByUserId(userId: string) {
const dashboard =
(await db.query<Dashboard>(sql`SELECT * FROM "bewcloud_dashboards" WHERE "user_id" = $1 LIMIT 1`, [
userId,
]))[0];
return dashboard;
}
static async create(userId: string) {
const data: Dashboard['data'] = { links: [], notes: '' };
const newDashboard = (await db.query<Dashboard>(
sql`INSERT INTO "bewcloud_dashboards" (
"user_id",
"data"
) VALUES ($1, $2)
RETURNING *`,
[
userId,
JSON.stringify(data),
],
))[0];
return newDashboard;
}
static async update(dashboard: Dashboard) {
await db.query(
sql`UPDATE "bewcloud_dashboards" SET
"data" = $2
WHERE "id" = $1`,
[
dashboard.id,
JSON.stringify(dashboard.data),
],
);
}
}