import prisma from '@/lib/prisma' /** * Upsert a note embedding into the NoteEmbedding table. * Uses a single SQL UPSERT to avoid race conditions. * The SQL template is fully static — no user data is interpolated into the query string. */ export async function upsertNoteEmbedding(noteId: string, embedding: number[]): Promise { const vecStr = `[${embedding.join(',')}]` await prisma.$executeRawUnsafe( `INSERT INTO "NoteEmbedding" ("id", "noteId", "embedding", "createdAt") VALUES (gen_random_uuid(), $1, $2::vector, now()) ON CONFLICT ("noteId") DO UPDATE SET "embedding" = EXCLUDED."embedding"`, noteId, vecStr ) }