|
1 | 1 | import { StatusCodes } from 'http-status-codes'
|
2 |
| -import { In, QueryRunner } from 'typeorm' |
| 2 | +import { EntityManager, In, QueryRunner } from 'typeorm' |
3 | 3 | import { v4 as uuidv4 } from 'uuid'
|
4 | 4 | import { Assistant } from '../../database/entities/Assistant'
|
5 | 5 | import { ChatFlow } from '../../database/entities/ChatFlow'
|
@@ -255,11 +255,15 @@ async function replaceDuplicateIdsForChatMessage(
|
255 | 255 | where: { id: In(ids) }
|
256 | 256 | })
|
257 | 257 | 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 | + }) |
263 | 267 | return originalData
|
264 | 268 | } catch (error) {
|
265 | 269 | throw new InternalFlowiseError(
|
@@ -459,11 +463,15 @@ async function replaceDuplicateIdsForDocumentStoreFileChunk(
|
459 | 463 | where: { id: In(ids) }
|
460 | 464 | })
|
461 | 465 | 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 | + }) |
467 | 475 | return originalData
|
468 | 476 | } catch (error) {
|
469 | 477 | throw new InternalFlowiseError(
|
@@ -550,6 +558,13 @@ function insertWorkspaceId(importedData: any, activeWorkspaceId?: string) {
|
550 | 558 | return importedData
|
551 | 559 | }
|
552 | 560 |
|
| 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 | + |
553 | 568 | const importData = async (importData: ExportData, orgId: string, activeWorkspaceId: string, subscriptionId: string) => {
|
554 | 569 | // Initialize missing properties with empty arrays to avoid "undefined" errors
|
555 | 570 | importData.AgentFlow = importData.AgentFlow || []
|
@@ -705,13 +720,13 @@ const importData = async (importData: ExportData, orgId: string, activeWorkspace
|
705 | 720 | if (importData.AssistantOpenAI.length > 0) await queryRunner.manager.save(Assistant, importData.AssistantOpenAI)
|
706 | 721 | if (importData.AssistantAzure.length > 0) await queryRunner.manager.save(Assistant, importData.AssistantAzure)
|
707 | 722 | 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) |
709 | 724 | if (importData.ChatMessageFeedback.length > 0)
|
710 | 725 | await queryRunner.manager.save(ChatMessageFeedback, importData.ChatMessageFeedback)
|
711 | 726 | if (importData.CustomTemplate.length > 0) await queryRunner.manager.save(CustomTemplate, importData.CustomTemplate)
|
712 | 727 | if (importData.DocumentStore.length > 0) await queryRunner.manager.save(DocumentStore, importData.DocumentStore)
|
713 | 728 | if (importData.DocumentStoreFileChunk.length > 0)
|
714 |
| - await queryRunner.manager.save(DocumentStoreFileChunk, importData.DocumentStoreFileChunk) |
| 729 | + await saveBatch(queryRunner.manager, DocumentStoreFileChunk, importData.DocumentStoreFileChunk) |
715 | 730 | if (importData.Tool.length > 0) await queryRunner.manager.save(Tool, importData.Tool)
|
716 | 731 | if (importData.Execution.length > 0) await queryRunner.manager.save(Execution, importData.Execution)
|
717 | 732 | if (importData.Variable.length > 0) await queryRunner.manager.save(Variable, importData.Variable)
|
|
0 commit comments