(
documents: Array<{
filename: string
fileUrl: string
fileSize: number
mimeType: string
documentTagsData?: string
tag1?: string
tag2?: string
tag3?: string
tag4?: string
tag5?: string
tag6?: string
tag7?: string
}>,
knowledgeBaseId: string,
requestId: string,
uploadedBy: string | null = null
)
| 852 | } |
| 853 | |
| 854 | export async function createDocumentRecords( |
| 855 | documents: Array<{ |
| 856 | filename: string |
| 857 | fileUrl: string |
| 858 | fileSize: number |
| 859 | mimeType: string |
| 860 | documentTagsData?: string |
| 861 | tag1?: string |
| 862 | tag2?: string |
| 863 | tag3?: string |
| 864 | tag4?: string |
| 865 | tag5?: string |
| 866 | tag6?: string |
| 867 | tag7?: string |
| 868 | }>, |
| 869 | knowledgeBaseId: string, |
| 870 | requestId: string, |
| 871 | uploadedBy: string | null = null |
| 872 | ): Promise<DocumentData[]> { |
| 873 | return await db.transaction(async (tx) => { |
| 874 | await tx.execute(sql`SELECT 1 FROM knowledge_base WHERE id = ${knowledgeBaseId} FOR UPDATE`) |
| 875 | |
| 876 | const kb = await tx |
| 877 | .select({ |
| 878 | id: knowledgeBase.id, |
| 879 | workspaceId: knowledgeBase.workspaceId, |
| 880 | userId: knowledgeBase.userId, |
| 881 | }) |
| 882 | .from(knowledgeBase) |
| 883 | .where(and(eq(knowledgeBase.id, knowledgeBaseId), isNull(knowledgeBase.deletedAt))) |
| 884 | .limit(1) |
| 885 | |
| 886 | if (kb.length === 0) { |
| 887 | throw new Error('Knowledge base not found') |
| 888 | } |
| 889 | |
| 890 | const kbWorkspaceId = kb[0].workspaceId |
| 891 | await assertKnowledgeBaseFileUrlsOwnership( |
| 892 | documents.map((docData) => docData.fileUrl), |
| 893 | kbWorkspaceId, |
| 894 | kb[0].userId, |
| 895 | requestId, |
| 896 | tx |
| 897 | ) |
| 898 | |
| 899 | // One load per batch (was N+1); skip entirely if no doc carries tags. |
| 900 | const hasTaggedDocs = documents.some((d) => d.documentTagsData) |
| 901 | const tagDefinitions = hasTaggedDocs |
| 902 | ? await loadTagDefinitions(knowledgeBaseId, tx) |
| 903 | : (new Map() as TagDefinitionsByName) |
| 904 | |
| 905 | const now = new Date() |
| 906 | const documentRecords = [] |
| 907 | const returnData: DocumentData[] = [] |
| 908 | |
| 909 | for (const docData of documents) { |
| 910 | const documentId = generateId() |
| 911 |
no test coverage detected