mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-20 01:11:25 +00:00
Implement bulk delete for nosql
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { BulkOperationType, OperationInput, PartitionKey } from "@azure/cosmos";
|
||||
import { CollectionBase } from "../../Contracts/ViewModels";
|
||||
import DocumentId from "../../Explorer/Tree/DocumentId";
|
||||
import { logConsoleInfo, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
|
||||
@@ -24,3 +25,42 @@ export const deleteDocument = async (collection: CollectionBase, documentId: Doc
|
||||
clearMessage();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Bulk delete documents
|
||||
* @param collection
|
||||
* @param documentId
|
||||
* @returns array of ids that were successfully deleted
|
||||
*/
|
||||
export const deleteDocuments = async (
|
||||
collection: CollectionBase,
|
||||
documentIds: {
|
||||
id: string;
|
||||
partitionKey?: PartitionKey;
|
||||
}[],
|
||||
): Promise<string[]> => {
|
||||
const clearMessage = logConsoleProgress(`Deleting ${documentIds.length} ${getEntityName(true)}`);
|
||||
try {
|
||||
const v2Container = await client().database(collection.databaseId).container(collection.id());
|
||||
|
||||
const operations: OperationInput[] = documentIds.map((documentId) => ({
|
||||
...documentId,
|
||||
operationType: BulkOperationType.Delete,
|
||||
}));
|
||||
const bulkResult = await v2Container.items.bulk(operations);
|
||||
const result: string[] = [];
|
||||
documentIds.forEach((documentId, index) => {
|
||||
if (bulkResult[index].statusCode === 204) {
|
||||
result.push(documentId.id);
|
||||
}
|
||||
});
|
||||
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${result.length} out of ${documentIds.length}`);
|
||||
// TODO: handle case result.length != documentIds.length
|
||||
return result;
|
||||
} catch (error) {
|
||||
handleError(error, "DeleteDocuments", `Error while deleting ${documentIds.length} ${getEntityName(true)}`);
|
||||
throw error;
|
||||
} finally {
|
||||
clearMessage();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user