Skip to content

Commit 6baec93

Browse files
authored
Optimize export import (#4795)
* feat: add saveBatch and optimize duplication id handling * feat: improve lookup performance by using Set
1 parent 30e8317 commit 6baec93

File tree

2 files changed

+29
-14
lines changed
  • packages/server/src

2 files changed

+29
-14
lines changed

packages/server/src/controllers/export-import/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const importData = async (req: Request, res: Response, next: NextFunction) => {
3939
}
4040

4141
await exportImportService.importData(importData, orgId, workspaceId, subscriptionId)
42-
return res.json({ message: 'success' })
42+
return res.status(StatusCodes.OK).json({ message: 'success' })
4343
} catch (error) {
4444
next(error)
4545
}

packages/server/src/services/export-import/index.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StatusCodes } from 'http-status-codes'
2-
import { In, QueryRunner } from 'typeorm'
2+
import { EntityManager, In, QueryRunner } from 'typeorm'
33
import { v4 as uuidv4 } from 'uuid'
44
import { Assistant } from '../../database/entities/Assistant'
55
import { ChatFlow } from '../../database/entities/ChatFlow'
@@ -255,11 +255,15 @@ async function replaceDuplicateIdsForChatMessage(
255255
where: { id: In(ids) }
256256
})
257257
if (records.length < 0) return originalData
258-
for (let record of records) {
259-
const oldId = record.id
260-
const newId = uuidv4()
261-
originalData = JSON.parse(JSON.stringify(originalData).replaceAll(oldId, newId))
262-
}
258+
259+
// replace duplicate ids found in db to new id
260+
const dbExistingIds = new Set(records.map((record) => record.id))
261+
originalData.ChatMessage = originalData.ChatMessage.map((item) => {
262+
if (dbExistingIds.has(item.id)) {
263+
return { ...item, id: uuidv4() }
264+
}
265+
return item
266+
})
263267
return originalData
264268
} catch (error) {
265269
throw new InternalFlowiseError(
@@ -459,11 +463,15 @@ async function replaceDuplicateIdsForDocumentStoreFileChunk(
459463
where: { id: In(ids) }
460464
})
461465
if (records.length < 0) return originalData
462-
for (let record of records) {
463-
const oldId = record.id
464-
const newId = uuidv4()
465-
originalData = JSON.parse(JSON.stringify(originalData).replaceAll(oldId, newId))
466-
}
466+
467+
// replace duplicate ids found in db to new id
468+
const dbExistingIds = new Set(records.map((record) => record.id))
469+
originalData.DocumentStoreFileChunk = originalData.DocumentStoreFileChunk.map((item) => {
470+
if (dbExistingIds.has(item.id)) {
471+
return { ...item, id: uuidv4() }
472+
}
473+
return item
474+
})
467475
return originalData
468476
} catch (error) {
469477
throw new InternalFlowiseError(
@@ -550,6 +558,13 @@ function insertWorkspaceId(importedData: any, activeWorkspaceId?: string) {
550558
return importedData
551559
}
552560

561+
async function saveBatch(manager: EntityManager, entity: any, items: any[], batchSize = 900) {
562+
for (let i = 0; i < items.length; i += batchSize) {
563+
const batch = items.slice(i, i + batchSize)
564+
await manager.save(entity, batch)
565+
}
566+
}
567+
553568
const importData = async (importData: ExportData, orgId: string, activeWorkspaceId: string, subscriptionId: string) => {
554569
// Initialize missing properties with empty arrays to avoid "undefined" errors
555570
importData.AgentFlow = importData.AgentFlow || []
@@ -705,13 +720,13 @@ const importData = async (importData: ExportData, orgId: string, activeWorkspace
705720
if (importData.AssistantOpenAI.length > 0) await queryRunner.manager.save(Assistant, importData.AssistantOpenAI)
706721
if (importData.AssistantAzure.length > 0) await queryRunner.manager.save(Assistant, importData.AssistantAzure)
707722
if (importData.ChatFlow.length > 0) await queryRunner.manager.save(ChatFlow, importData.ChatFlow)
708-
if (importData.ChatMessage.length > 0) await queryRunner.manager.save(ChatMessage, importData.ChatMessage)
723+
if (importData.ChatMessage.length > 0) await saveBatch(queryRunner.manager, ChatMessage, importData.ChatMessage)
709724
if (importData.ChatMessageFeedback.length > 0)
710725
await queryRunner.manager.save(ChatMessageFeedback, importData.ChatMessageFeedback)
711726
if (importData.CustomTemplate.length > 0) await queryRunner.manager.save(CustomTemplate, importData.CustomTemplate)
712727
if (importData.DocumentStore.length > 0) await queryRunner.manager.save(DocumentStore, importData.DocumentStore)
713728
if (importData.DocumentStoreFileChunk.length > 0)
714-
await queryRunner.manager.save(DocumentStoreFileChunk, importData.DocumentStoreFileChunk)
729+
await saveBatch(queryRunner.manager, DocumentStoreFileChunk, importData.DocumentStoreFileChunk)
715730
if (importData.Tool.length > 0) await queryRunner.manager.save(Tool, importData.Tool)
716731
if (importData.Execution.length > 0) await queryRunner.manager.save(Execution, importData.Execution)
717732
if (importData.Variable.length > 0) await queryRunner.manager.save(Variable, importData.Variable)

0 commit comments

Comments
 (0)