openreader/src/lib/server/audiobook-prune.ts
Richard R 4a5f3060f2 refactor(documents): migrate from IndexedDB to server-backed storage with caching
- Replace client-side IndexedDB with server-first document storage
- Add document caching layer for offline capability and performance
- Implement document transfer during anonymous-to-authenticated account linking
- Add database indexes for improved query performance
- Update document contexts to use new caching system
- Refactor document upload and deletion APIs
- Add audiobook pruning for missing files
- Improve test isolation with namespace support
- Add unit tests for document cache and transfer functions
2026-02-08 11:15:57 -07:00

45 lines
1.5 KiB
TypeScript

import { and, eq, notInArray } from 'drizzle-orm';
import { db } from '@/db';
import { audiobooks, audiobookChapters } from '@/db/schema';
export async function pruneAudiobookIfMissingDir(bookId: string, userId: string, intermediateDirExists: boolean): Promise<void> {
if (intermediateDirExists) return;
await db.delete(audiobookChapters).where(and(eq(audiobookChapters.bookId, bookId), eq(audiobookChapters.userId, userId)));
await db.delete(audiobooks).where(and(eq(audiobooks.id, bookId), eq(audiobooks.userId, userId)));
}
export async function pruneAudiobookChaptersNotOnDisk(
bookId: string,
userId: string,
presentChapterIndexes: number[],
): Promise<void> {
if (presentChapterIndexes.length === 0) {
await db
.delete(audiobookChapters)
.where(and(eq(audiobookChapters.bookId, bookId), eq(audiobookChapters.userId, userId)));
return;
}
await db
.delete(audiobookChapters)
.where(
and(
eq(audiobookChapters.bookId, bookId),
eq(audiobookChapters.userId, userId),
notInArray(audiobookChapters.chapterIndex, presentChapterIndexes),
),
);
}
export async function pruneAudiobookChapterIfMissingFile(bookId: string, userId: string, chapterIndex: number, chapterFileExists: boolean): Promise<void> {
if (chapterFileExists) return;
await db
.delete(audiobookChapters)
.where(
and(
eq(audiobookChapters.bookId, bookId),
eq(audiobookChapters.userId, userId),
eq(audiobookChapters.chapterIndex, chapterIndex),
),
);
}