diff --git a/src/Common/dataAccess/deleteDocument.ts b/src/Common/dataAccess/deleteDocument.ts index e1612d92d..535dea0a8 100644 --- a/src/Common/dataAccess/deleteDocument.ts +++ b/src/Common/dataAccess/deleteDocument.ts @@ -1,4 +1,4 @@ -import { BulkOperationType, OperationInput, PartitionKey } from "@azure/cosmos"; +import { BulkOperationType, OperationInput } from "@azure/cosmos"; import { CollectionBase } from "../../Contracts/ViewModels"; import DocumentId from "../../Explorer/Tree/DocumentId"; import { logConsoleInfo, logConsoleProgress } from "../../Utils/NotificationConsoleUtils"; @@ -32,26 +32,26 @@ export const deleteDocument = async (collection: CollectionBase, documentId: Doc * @param documentId * @returns array of ids that were successfully deleted */ -export const deleteDocuments = async ( - collection: CollectionBase, - documentIds: { - id: string; - partitionKey?: PartitionKey; - }[], -): Promise => { +export const deleteDocuments = async (collection: CollectionBase, documentIds: DocumentId[]): Promise => { 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, + id: documentId.id(), + // bulk delete: if not partition key is specified, do not pass empty array, but undefined + partitionKey: + documentId.partitionKeyValue && + Array.isArray(documentId.partitionKeyValue) && + documentId.partitionKeyValue.length === 0 + ? undefined + : documentId.partitionKeyValue, operationType: BulkOperationType.Delete, })); const bulkResult = await v2Container.items.bulk(operations); - const result: string[] = []; + const result: DocumentId[] = []; documentIds.forEach((documentId, index) => { if (bulkResult[index].statusCode === 204) { - result.push(documentId.id); + result.push(documentId); } }); logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${result.length} out of ${documentIds.length}`); diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx index 0d0cb178c..9e3834bd7 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx @@ -802,24 +802,14 @@ const DocumentsTabComponent: React.FunctionComponent<{ * Implementation using bulk delete */ let _deleteDocuments = useCallback( - async (toDeleteDocumentIds: DocumentId[]): Promise => { + async (toDeleteDocumentIds: DocumentId[]): Promise => { onExecutionErrorChange(false); const startKey: number = TelemetryProcessor.traceStart(Action.DeleteDocuments, { dataExplorerArea: Constants.Areas.Tab, tabTitle, }); setIsExecuting(true); - return deleteNoSqlDocuments( - _collection, - toDeleteDocumentIds.map((id) => ({ - id: id.id(), - // bulk delete: if not partition key is specified, do not pass empty array, but undefined - partitionKey: - id.partitionKeyValue && Array.isArray(id.partitionKeyValue) && id.partitionKeyValue.length === 0 - ? undefined - : id.partitionKeyValue, - })), - ) + return deleteNoSqlDocuments(_collection, toDeleteDocumentIds) .then( (deletedIds) => { TelemetryProcessor.traceSuccess( @@ -858,9 +848,9 @@ const DocumentsTabComponent: React.FunctionComponent<{ onExecutionErrorChange(false); setIsExecuting(true); _deleteDocuments(toDeleteDocumentIds) - .then((deletedIds: string[]) => { - // This could be optimized by using Set.has instead of array.includes - const newDocumentIds = [...documentIds.filter((documentId) => !deletedIds.includes(documentId.id()))]; + .then((deletedIds: DocumentId[]) => { + const deletedRids = new Set(deletedIds.map((documentId) => documentId.rid)); + const newDocumentIds = [...documentIds.filter((documentId) => !deletedRids.has(documentId.rid))]; setDocumentIds(newDocumentIds); setSelectedDocumentContent(undefined); @@ -1348,16 +1338,18 @@ const DocumentsTabComponent: React.FunctionComponent<{ * Mongo implementation * TODO: update proxy to use mongo driver deleteMany */ - _deleteDocuments = (toDeleteDocumentIds: DocumentId[]): Promise => { + _deleteDocuments = (toDeleteDocumentIds: DocumentId[]): Promise => { const promises = toDeleteDocumentIds.map((documentId) => _deleteDocument(documentId)); return Promise.all(promises); }; - const __deleteDocument = (documentId: DocumentId): Promise => - MongoProxyClient.deleteDocument(_collection.databaseId, _collection as ViewModels.Collection, documentId); + const __deleteDocument = async (documentId: DocumentId): Promise => { + await MongoProxyClient.deleteDocument(_collection.databaseId, _collection as ViewModels.Collection, documentId); + return documentId; + }; const _deleteDocument = useCallback( - (documentId: DocumentId): Promise => { + (documentId: DocumentId): Promise => { onExecutionErrorChange(false); const startKey: number = TelemetryProcessor.traceStart(Action.DeleteDocument, { dataExplorerArea: Constants.Areas.Tab,