MCPcopy Index your code
hub / github.com/simstudioai/sim / createDocumentRecords

Function createDocumentRecords

apps/sim/lib/knowledge/documents/service.ts:854–987  ·  view source on GitHub ↗
(
  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
)

Source from the content-addressed store, hash-verified

852}
853
854export 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

Callers 2

route.tsFile · 0.90
route.tsFile · 0.90

Calls 12

generateIdFunction · 0.90
loadTagDefinitionsFunction · 0.85
resolveDocumentTagsFunction · 0.85
parseMethod · 0.80
infoMethod · 0.80
executeMethod · 0.65
warnMethod · 0.65
setMethod · 0.65
eqFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected