( documentIds: string[], requestId: string )
| 1968 | } |
| 1969 | |
| 1970 | export async function hardDeleteDocuments( |
| 1971 | documentIds: string[], |
| 1972 | requestId: string |
| 1973 | ): Promise<number> { |
| 1974 | const ids = [...new Set(documentIds)] |
| 1975 | if (ids.length === 0) { |
| 1976 | return 0 |
| 1977 | } |
| 1978 | |
| 1979 | const documentsToDelete = await db |
| 1980 | .select({ |
| 1981 | id: document.id, |
| 1982 | fileUrl: document.fileUrl, |
| 1983 | workspaceId: knowledgeBase.workspaceId, |
| 1984 | }) |
| 1985 | .from(document) |
| 1986 | .innerJoin(knowledgeBase, eq(document.knowledgeBaseId, knowledgeBase.id)) |
| 1987 | .where(inArray(document.id, ids)) |
| 1988 | |
| 1989 | if (documentsToDelete.length === 0) { |
| 1990 | return 0 |
| 1991 | } |
| 1992 | |
| 1993 | const existingIds = documentsToDelete.map((doc) => doc.id) |
| 1994 | |
| 1995 | await db.transaction(async (tx) => { |
| 1996 | await tx.delete(embedding).where(inArray(embedding.documentId, existingIds)) |
| 1997 | await tx.delete(document).where(inArray(document.id, existingIds)) |
| 1998 | }) |
| 1999 | |
| 2000 | await deleteDocumentStorageFiles(documentsToDelete, requestId) |
| 2001 | |
| 2002 | logger.info(`[${requestId}] Hard deleted ${existingIds.length} documents`, { |
| 2003 | documentIds: existingIds, |
| 2004 | }) |
| 2005 | |
| 2006 | return existingIds.length |
| 2007 | } |
| 2008 | |
| 2009 | export async function deleteDocument( |
| 2010 | documentId: string, |
no test coverage detected