mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-21 01:41:31 +00:00
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
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;
|
|
const throttledCount = response.filter((r) => r.statusCode === 429).length;
|
|
|
|
logConsoleInfo(
|
|
`${
|
|
documents.length
|
|
} operations completed for container ${collection.id()}. ${successCount} operations succeeded. ${throttledCount} operations throttled`
|
|
);
|
|
return response;
|
|
} catch (error) {
|
|
handleError(error, "BulkCreateDocument", `Error bulk creating items for container ${collection.id()}`);
|
|
throw error;
|
|
} finally {
|
|
clearMessage();
|
|
}
|
|
};
|