success pt. 2

This commit is contained in:
2026-02-05 03:26:23 -08:00
parent e403e355ae
commit 8e5f8596b4
2 changed files with 19 additions and 87 deletions

View File

@@ -1,70 +1,26 @@
<script lang="ts">
import { desc } from "drizzle-orm";
import { onMount } from "svelte";
import { db, overwriteDatabaseFile } from "$lib/db/client-db";
import { animeTable } from "$lib/db/schema";
import { getAnimeList } from "$lib/db/client-amq";
type AnimeListItem = {
annId: number;
mainName: string;
year: number;
seasonId: number;
malId: number;
};
const SEED_URL = "/data/amq.sqlite";
const SEED_VERSION = "amq.sqlite.v1"; // bump when static/data/amq.sqlite changes
const SEED_KEY = "amq.seed.version";
let status = $state<"idle" | "loading" | "ready" | "error">("idle");
let error = $state<string | null>(null);
let anime = $state<AnimeListItem[]>([]);
async function ensureSeeded() {
const current = localStorage.getItem(SEED_KEY);
if (current === SEED_VERSION) return;
const res = await fetch(SEED_URL, { cache: "no-cache" });
if (!res.ok) {
throw new Error(
`Failed to fetch seed DB: ${res.status} ${res.statusText}`,
);
}
const stream = res.body;
if (stream) {
await overwriteDatabaseFile(stream);
} else {
const blob = await res.blob();
await overwriteDatabaseFile(blob);
}
localStorage.setItem(SEED_KEY, SEED_VERSION);
}
onMount(() => {
(async () => {
status = "loading";
error = null;
try {
await ensureSeeded();
const rows = await db
.select({
annId: animeTable.annId,
mainName: animeTable.mainName,
year: animeTable.year,
seasonId: animeTable.seasonId,
})
.from(animeTable)
.orderBy(
desc(animeTable.year),
desc(animeTable.seasonId),
desc(animeTable.annId),
)
.limit(20);
anime = rows;
anime = await getAnimeList(20);
status = "ready";
} catch (e) {
error = e instanceof Error ? e.message : String(e);
@@ -109,7 +65,7 @@
<div class="font-medium">{a.mainName}</div>
<div class="text-sm text-muted-foreground">
{a.year}
{seasonName(a.seasonId)} • ANN {a.annId}
{seasonName(a.seasonId)} • ANN {a.annId} • MAL {a.malId}
</div>
</li>
{/each}