mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-02-18 10:17:16 +00:00
Handle >100 bulk delete operations
This commit is contained in:
parent
6010881530
commit
4310020da5
@ -36,7 +36,14 @@ export const deleteDocuments = async (collection: CollectionBase, documentIds: D
|
|||||||
const clearMessage = logConsoleProgress(`Deleting ${documentIds.length} ${getEntityName(true)}`);
|
const clearMessage = logConsoleProgress(`Deleting ${documentIds.length} ${getEntityName(true)}`);
|
||||||
try {
|
try {
|
||||||
const v2Container = await client().database(collection.databaseId).container(collection.id());
|
const v2Container = await client().database(collection.databaseId).container(collection.id());
|
||||||
const operations: OperationInput[] = documentIds.map((documentId) => ({
|
|
||||||
|
// Bulk can only delete 100 documents at a time
|
||||||
|
const BULK_DELETE_LIMIT = 100;
|
||||||
|
const promiseArray = [];
|
||||||
|
|
||||||
|
while (documentIds.length > 0) {
|
||||||
|
const documentIdsChunk = documentIds.splice(0, BULK_DELETE_LIMIT);
|
||||||
|
const operations: OperationInput[] = documentIdsChunk.map((documentId) => ({
|
||||||
id: documentId.id(),
|
id: documentId.id(),
|
||||||
// bulk delete: if not partition key is specified, do not pass empty array, but undefined
|
// bulk delete: if not partition key is specified, do not pass empty array, but undefined
|
||||||
partitionKey:
|
partitionKey:
|
||||||
@ -47,16 +54,24 @@ export const deleteDocuments = async (collection: CollectionBase, documentIds: D
|
|||||||
: documentId.partitionKeyValue,
|
: documentId.partitionKeyValue,
|
||||||
operationType: BulkOperationType.Delete,
|
operationType: BulkOperationType.Delete,
|
||||||
}));
|
}));
|
||||||
const bulkResult = await v2Container.items.bulk(operations);
|
|
||||||
|
const promise = v2Container.items.bulk(operations).then((bulkResult) => {
|
||||||
const result: DocumentId[] = [];
|
const result: DocumentId[] = [];
|
||||||
documentIds.forEach((documentId, index) => {
|
documentIdsChunk.forEach((documentId, index) => {
|
||||||
if (bulkResult[index].statusCode === 204) {
|
if (bulkResult[index].statusCode === 204) {
|
||||||
result.push(documentId);
|
result.push(documentId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${result.length} out of ${documentIds.length}`);
|
|
||||||
// TODO: handle case result.length != documentIds.length
|
|
||||||
return result;
|
return result;
|
||||||
|
});
|
||||||
|
promiseArray.push(promise);
|
||||||
|
}
|
||||||
|
|
||||||
|
const allResult = await Promise.all(promiseArray);
|
||||||
|
const flatAllResult = Array.prototype.concat.apply([], allResult);
|
||||||
|
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${flatAllResult.length} out of ${documentIds.length}`);
|
||||||
|
// TODO: handle case result.length != documentIds.length
|
||||||
|
return flatAllResult;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error, "DeleteDocuments", `Error while deleting ${documentIds.length} ${getEntityName(true)}`);
|
handleError(error, "DeleteDocuments", `Error while deleting ${documentIds.length} ${getEntityName(true)}`);
|
||||||
throw error;
|
throw error;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user