mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-19 08:51:24 +00:00
Fix Upload Items (#682)
* Fix Upload Items * Remove debugger * Switch to bulk APIs * Address TODO Co-authored-by: Jordi Bunster <jbunster@microsoft.com>
This commit is contained in:
36
src/Common/dataAccess/bulkCreateDocument.ts
Normal file
36
src/Common/dataAccess/bulkCreateDocument.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { JSONObject, OperationResponse } from "@azure/cosmos";
|
||||
import { CollectionBase } from "../../Contracts/ViewModels";
|
||||
import { logConsoleInfo, logConsoleProgress } from "../../Utils/NotificationConsoleUtils";
|
||||
import { client } from "../CosmosClient";
|
||||
import { handleError } from "../ErrorHandlingUtils";
|
||||
|
||||
export const bulkCreateDocument = async (
|
||||
collection: CollectionBase,
|
||||
documents: JSONObject[]
|
||||
): Promise<OperationResponse[]> => {
|
||||
const clearMessage = logConsoleProgress(
|
||||
`Executing ${documents.length} bulk operations for container ${collection.id()}`
|
||||
);
|
||||
|
||||
try {
|
||||
const response = await client()
|
||||
.database(collection.databaseId)
|
||||
.container(collection.id())
|
||||
.items.bulk(
|
||||
documents.map((doc) => ({ operationType: "Create", resourceBody: doc })),
|
||||
{ continueOnError: true }
|
||||
);
|
||||
|
||||
const successCount = response.filter((r) => r.statusCode === 201).length;
|
||||
|
||||
logConsoleInfo(
|
||||
`${documents.length} operations completed for container ${collection.id()}. ${successCount} operations succeeded`
|
||||
);
|
||||
return response;
|
||||
} catch (error) {
|
||||
handleError(error, "BulkCreateDocument", `Error bulk creating items for container ${collection.id()}`);
|
||||
throw error;
|
||||
} finally {
|
||||
clearMessage();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user