success pt. 8.0

This commit is contained in:
2026-02-05 04:21:25 -08:00
parent 81849ea181
commit 356ca922f9
5 changed files with 282 additions and 3 deletions

View File

@@ -1,5 +1,11 @@
import { desc, like } from "drizzle-orm";
import { animeTable } from "$lib/db/schema";
import { desc, eq, like } from "drizzle-orm";
import {
animeSongLinksTable,
animeTable,
artistsTable,
groupsTable,
songsTable,
} from "$lib/db/schema";
import type { ClientDb } from "./index";
/**
@@ -75,6 +81,69 @@ export async function searchAnimeByName(
.limit(safeLimit);
}
/**
* Fetch a single anime plus its linked songs, including:
* - anime name
* - link metadata: type + number (OP1/ED2/etc)
* - song metadata: song name, artist/group name, fileName for audio playback
*
* Note: this assumes the SQLite snapshot has `songs.file_name`, `songs.song_artist_id`,
* and `songs.song_group_id` populated.
*/
export async function getAnimeWithSongsByAnnId(db: ClientDb, annId: number) {
const animeRows = await db
.select({
annId: animeTable.annId,
mainName: animeTable.mainName,
year: animeTable.year,
seasonId: animeTable.seasonId,
malId: animeTable.malId,
})
.from(animeTable)
.where(eq(animeTable.annId, annId))
.limit(1);
const anime = animeRows[0];
if (!anime) return null;
const rows = await db
.select({
annSongId: animeSongLinksTable.annSongId,
type: animeSongLinksTable.type,
number: animeSongLinksTable.number,
songName: songsTable.name,
fileName: songsTable.fileName,
artistName: artistsTable.name,
groupName: groupsTable.name,
})
.from(animeSongLinksTable)
.innerJoin(
songsTable,
eq(songsTable.annSongId, animeSongLinksTable.annSongId),
)
.leftJoin(
artistsTable,
eq(artistsTable.songArtistId, songsTable.songArtistId),
)
.leftJoin(groupsTable, eq(groupsTable.songGroupId, songsTable.songGroupId))
.where(eq(animeSongLinksTable.annId, annId))
.orderBy(desc(animeSongLinksTable.type), desc(animeSongLinksTable.number));
return {
anime,
songs: rows.map((r) => ({
annSongId: r.annSongId,
type: r.type,
number: r.number,
songName: r.songName,
fileName: r.fileName,
artistName: r.artistName ?? r.groupName ?? null,
})),
};
}
function clampLimit(limit: number) {
const n = Number(limit);
if (!Number.isFinite(n)) return DEFAULT_LIST_LIMIT;