import { and, eq } from 'drizzle-orm' import type { drizzle } from 'drizzle-orm/postgres-js' import { files, type FileRow } from '@/entities/file/model/schema' /** The handle the shared `db()` returns (`@/shared/api`). */ type Db = ReturnType export type NewFile = Omit /** * Attachment persistence. Writes name their author, as the notes repository does: * a visitor removes only the files they attached. */ export function fileRepository(db: Db) { return { async create(input: NewFile): Promise { const [row] = await db.insert(files).values(input).returning() if (!row) throw new Error('the insert returned no row') return row }, async listByNote(noteId: string): Promise { return db.select().from(files).where(eq(files.noteId, noteId)) }, /** The attachment, or `undefined` when `authorId` did not attach it. */ async get(id: string, authorId: string): Promise { const [row] = await db .select() .from(files) .where(and(eq(files.id, id), eq(files.authorId, authorId))) .limit(1) return row }, /** Every attachment the visitor made, across their notes. */ async listByAuthor(authorId: string): Promise { return db.select().from(files).where(eq(files.authorId, authorId)) }, /** The removed file, or `undefined` when `authorId` did not attach it. */ async remove(id: string, authorId: string): Promise { const [row] = await db .delete(files) .where(and(eq(files.id, id), eq(files.authorId, authorId))) .returning() return row }, } }