248 lines
7.4 KiB
TypeScript
248 lines
7.4 KiB
TypeScript
import { DatabaseSync, type SQLOutputValue } from "node:sqlite";
|
|
import {
|
|
type Comment,
|
|
Dump,
|
|
type Playlist,
|
|
type RichContent,
|
|
type User,
|
|
} from "./interfaces.ts";
|
|
|
|
export const db = new DatabaseSync("api/sql/gerbeur.db");
|
|
db.exec("PRAGMA foreign_keys = ON;");
|
|
|
|
// Migration: add is_private column if it doesn't exist yet
|
|
try {
|
|
db.exec(`ALTER TABLE dumps ADD COLUMN is_private INTEGER NOT NULL DEFAULT 0;`);
|
|
} catch { /* column already exists */ }
|
|
|
|
// Migration: create comments table if it doesn't exist yet
|
|
try {
|
|
db.exec(`CREATE TABLE IF NOT EXISTS comments (
|
|
id TEXT PRIMARY KEY,
|
|
dump_id TEXT NOT NULL REFERENCES dumps(id) ON DELETE CASCADE,
|
|
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
parent_id TEXT REFERENCES comments(id) ON DELETE CASCADE,
|
|
body TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
deleted INTEGER NOT NULL DEFAULT 0
|
|
);`);
|
|
db.exec(`CREATE INDEX IF NOT EXISTS idx_comments_dump ON comments(dump_id, created_at);`);
|
|
db.exec(`CREATE INDEX IF NOT EXISTS idx_votes_user ON votes(user_id);`);
|
|
db.exec(`CREATE INDEX IF NOT EXISTS idx_playlist_dumps_dump ON playlist_dumps(dump_id);`);
|
|
} catch { /* already exists */ }
|
|
|
|
// Migration: add deleted column to comments if it doesn't exist yet
|
|
try {
|
|
db.exec(`ALTER TABLE comments ADD COLUMN deleted INTEGER NOT NULL DEFAULT 0;`);
|
|
} catch { /* column already exists */ }
|
|
|
|
/**
|
|
* Database Row Types
|
|
*/
|
|
|
|
export interface DumpRow {
|
|
id: string;
|
|
kind: string;
|
|
title: string;
|
|
comment: string | null;
|
|
user_id: string;
|
|
created_at: string;
|
|
url: string | null;
|
|
rich_content: string | null;
|
|
file_name: string | null;
|
|
file_mime: string | null;
|
|
file_size: number | null;
|
|
vote_count: number;
|
|
comment_count?: number;
|
|
is_private: number;
|
|
[key: string]: SQLOutputValue; // Index signature
|
|
}
|
|
|
|
export interface UserRow {
|
|
id: string;
|
|
username: string;
|
|
password_hash: string;
|
|
is_admin: number;
|
|
created_at: string;
|
|
avatar_mime: string | null;
|
|
[key: string]: SQLOutputValue; // Index signature
|
|
}
|
|
|
|
/**
|
|
* Type Guards
|
|
*/
|
|
|
|
export function isDumpRow(obj: Record<string, SQLOutputValue>): obj is DumpRow {
|
|
return !!obj &&
|
|
typeof obj === "object" &&
|
|
"id" in obj && typeof obj.id === "string" &&
|
|
"kind" in obj && typeof obj.kind === "string" &&
|
|
"title" in obj && typeof obj.title === "string" &&
|
|
"comment" in obj &&
|
|
(typeof obj.comment === "string" || obj.comment === null) &&
|
|
"user_id" in obj && typeof obj.user_id === "string" &&
|
|
"created_at" in obj && typeof obj.created_at === "string" &&
|
|
"url" in obj && (typeof obj.url === "string" || obj.url === null) &&
|
|
"rich_content" in obj &&
|
|
(typeof obj.rich_content === "string" || obj.rich_content === null) &&
|
|
"file_name" in obj &&
|
|
(typeof obj.file_name === "string" || obj.file_name === null) &&
|
|
"file_mime" in obj &&
|
|
(typeof obj.file_mime === "string" || obj.file_mime === null) &&
|
|
"file_size" in obj &&
|
|
(typeof obj.file_size === "number" || obj.file_size === null) &&
|
|
"vote_count" in obj && typeof obj.vote_count === "number" &&
|
|
"is_private" in obj && typeof obj.is_private === "number";
|
|
}
|
|
|
|
export function isUserRow(obj: Record<string, SQLOutputValue>): obj is UserRow {
|
|
return !!obj &&
|
|
typeof obj === "object" &&
|
|
"id" in obj && typeof obj.id === "string" &&
|
|
"username" in obj && typeof obj.username === "string" &&
|
|
"password_hash" in obj && typeof obj.password_hash === "string" &&
|
|
"is_admin" in obj && typeof obj.is_admin === "number" &&
|
|
"created_at" in obj && typeof obj.created_at === "string" &&
|
|
"avatar_mime" in obj &&
|
|
(typeof obj.avatar_mime === "string" || obj.avatar_mime === null);
|
|
}
|
|
|
|
/**
|
|
* Conversion Helpers
|
|
*/
|
|
|
|
export function dumpRowToApi(row: DumpRow): Dump {
|
|
return {
|
|
id: row.id,
|
|
kind: row.kind as "url" | "file",
|
|
title: row.title,
|
|
comment: row.comment ?? undefined,
|
|
userId: row.user_id,
|
|
createdAt: new Date(row.created_at),
|
|
url: row.url ?? undefined,
|
|
richContent: row.rich_content
|
|
? (JSON.parse(row.rich_content) as RichContent)
|
|
: undefined,
|
|
fileName: row.file_name ?? undefined,
|
|
fileMime: row.file_mime ?? undefined,
|
|
fileSize: row.file_size ?? undefined,
|
|
voteCount: row.vote_count,
|
|
commentCount: row.comment_count ?? 0,
|
|
isPrivate: Boolean(row.is_private),
|
|
};
|
|
}
|
|
|
|
export function dumpApiToRow(dump: Dump): DumpRow {
|
|
return {
|
|
id: dump.id,
|
|
kind: dump.kind,
|
|
title: dump.title,
|
|
comment: dump.comment ?? null,
|
|
user_id: dump.userId,
|
|
created_at: dump.createdAt.toISOString(),
|
|
url: dump.url ?? null,
|
|
rich_content: dump.richContent ? JSON.stringify(dump.richContent) : null,
|
|
file_name: dump.fileName ?? null,
|
|
file_mime: dump.fileMime ?? null,
|
|
file_size: dump.fileSize ?? null,
|
|
vote_count: dump.voteCount,
|
|
is_private: dump.isPrivate ? 1 : 0,
|
|
};
|
|
}
|
|
|
|
export function userRowToApi(row: UserRow): User {
|
|
return {
|
|
id: row.id,
|
|
username: row.username,
|
|
passwordHash: row.password_hash,
|
|
isAdmin: Boolean(row.is_admin),
|
|
createdAt: new Date(row.created_at),
|
|
avatarMime: row.avatar_mime ?? undefined,
|
|
};
|
|
}
|
|
|
|
export function userApiToRow(user: User): UserRow {
|
|
return {
|
|
id: user.id,
|
|
username: user.username,
|
|
password_hash: user.passwordHash,
|
|
is_admin: user.isAdmin ? 1 : 0,
|
|
created_at: user.createdAt.toISOString(),
|
|
avatar_mime: user.avatarMime ?? null,
|
|
};
|
|
}
|
|
|
|
export interface CommentRow {
|
|
id: string;
|
|
dump_id: string;
|
|
user_id: string;
|
|
parent_id: string | null;
|
|
body: string;
|
|
created_at: string;
|
|
deleted: number;
|
|
author_username: string;
|
|
author_avatar_mime: string | null;
|
|
[key: string]: SQLOutputValue;
|
|
}
|
|
|
|
export function isCommentRow(obj: Record<string, SQLOutputValue>): obj is CommentRow {
|
|
return !!obj && typeof obj === "object" &&
|
|
typeof obj.id === "string" &&
|
|
typeof obj.dump_id === "string" &&
|
|
typeof obj.user_id === "string" &&
|
|
(typeof obj.parent_id === "string" || obj.parent_id === null) &&
|
|
typeof obj.body === "string" &&
|
|
typeof obj.created_at === "string" &&
|
|
typeof obj.deleted === "number" &&
|
|
typeof obj.author_username === "string" &&
|
|
(typeof obj.author_avatar_mime === "string" || obj.author_avatar_mime === null);
|
|
}
|
|
|
|
export function commentRowToApi(row: CommentRow): Comment {
|
|
return {
|
|
id: row.id,
|
|
dumpId: row.dump_id,
|
|
userId: row.user_id,
|
|
parentId: row.parent_id ?? undefined,
|
|
body: row.body,
|
|
createdAt: new Date(row.created_at),
|
|
deleted: Boolean(row.deleted),
|
|
authorUsername: row.author_username,
|
|
authorAvatarMime: row.author_avatar_mime ?? undefined,
|
|
};
|
|
}
|
|
|
|
export interface PlaylistRow {
|
|
id: string;
|
|
user_id: string;
|
|
title: string;
|
|
description: string | null;
|
|
is_public: number;
|
|
created_at: string;
|
|
image_mime: string | null;
|
|
[key: string]: SQLOutputValue;
|
|
}
|
|
|
|
export function isPlaylistRow(
|
|
obj: Record<string, SQLOutputValue>,
|
|
): obj is PlaylistRow {
|
|
return !!obj && typeof obj.id === "string" &&
|
|
typeof obj.user_id === "string" &&
|
|
typeof obj.title === "string" &&
|
|
typeof obj.is_public === "number" &&
|
|
typeof obj.created_at === "string";
|
|
}
|
|
|
|
export function playlistRowToApi(row: PlaylistRow): Playlist {
|
|
return {
|
|
id: row.id,
|
|
userId: row.user_id,
|
|
title: row.title,
|
|
description: row.description ?? undefined,
|
|
isPublic: Boolean(row.is_public),
|
|
createdAt: new Date(row.created_at),
|
|
imageMime: row.image_mime ?? undefined,
|
|
dumpCount: typeof row.dump_count === "number" ? row.dump_count : undefined,
|
|
};
|
|
}
|