Skip to content

Commit

Permalink
changed to await
Browse files Browse the repository at this point in the history
  • Loading branch information
malgamves committed Jan 23, 2025
1 parent 64343fa commit 27a0ed3
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions _includes/code/howto/manage-data.migrate.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function migrateData(collection_src: Collection, collection_tgt: Collectio
}

counter++;
if (counter % 1000 == 0)
if (counter % 500 == 0)
console.log(`Import: ${counter}`)

let objectToInsert = {
Expand All @@ -149,26 +149,22 @@ async function migrateData(collection_src: Collection, collection_tgt: Collectio
// Add object to batching array
itemsToInsert.push(objectToInsert)

if (itemsToInsert.length == 1000 || counter == maxItems) {

const promise = collection_tgt.data.insertMany(itemsToInsert)
.then((response) => {
console.log(`Successfully imported batch of ${Object.keys(response.uuids).length} items`);
if (response.hasErrors) {
throw new Error("Error in batch import!");
}
})
.catch((error) => {
console.error('Error importing batch:', error);
})

promises.push(promise)
itemsToInsert = [];

if (itemsToInsert.length == 500 || counter == maxItems) {
try {
const response = await collection_tgt.data.insertMany(itemsToInsert);

if (response.hasErrors) {
throw new Error("Error in batch import!");
}

console.log(`Successfully imported batch of ${itemsToInsert.length} items`);
itemsToInsert = [];
} catch (error) {
console.error('Error importing batch:', error);
}
}
}
// Runs all promises
await Promise.all(promises)

}
// END CollectionToCollection // END TenantToCollection // END CollectionToTenant // END TenantToTenant

Expand Down

0 comments on commit 27a0ed3

Please sign in to comment.