|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { handleError, finish, logStep } from "./Shared/handleError"; |
| 5 | +import { addEntropy } from "../test/public/common/TestHelpers"; |
| 6 | +import { BulkOperationType } from "../src"; |
| 7 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 8 | +// @ts-ignore |
| 9 | +import { CosmosClient } from "../dist"; |
| 10 | +import { endpoint, masterKey } from "../test/public/common/_testConfig"; |
| 11 | + |
| 12 | +async function run() { |
| 13 | + const containerId = "bulkContainerV2"; |
| 14 | + const client = new CosmosClient({ |
| 15 | + key: masterKey, |
| 16 | + endpoint: endpoint |
| 17 | + }); |
| 18 | + const { database } = await client.databases.create({ id: addEntropy("bulk db") }); |
| 19 | + logStep(`Create multi-partition container '${containerId}' with partition key /key`); |
| 20 | + const { container: v2Container } = await database.containers.create({ |
| 21 | + id: containerId, |
| 22 | + partitionKey: { |
| 23 | + paths: ["/key"], |
| 24 | + version: 2 |
| 25 | + }, |
| 26 | + throughput: 25100 |
| 27 | + }); |
| 28 | + |
| 29 | + const readItemId = addEntropy("item1"); |
| 30 | + const deleteItemId = addEntropy("item2"); |
| 31 | + const replaceItemId = addEntropy("item3"); |
| 32 | + logStep( |
| 33 | + `Create items ${readItemId}, ${deleteItemId}, ${replaceItemId} for reading, deleting and replacing` |
| 34 | + ); |
| 35 | + await v2Container.items.create({ |
| 36 | + id: readItemId, |
| 37 | + key: true, |
| 38 | + class: "2010" |
| 39 | + }); |
| 40 | + await v2Container.items.create({ |
| 41 | + id: deleteItemId, |
| 42 | + key: {}, |
| 43 | + class: "2011" |
| 44 | + }); |
| 45 | + await v2Container.items.create({ |
| 46 | + id: replaceItemId, |
| 47 | + key: 5, |
| 48 | + class: "2012" |
| 49 | + }); |
| 50 | + |
| 51 | + const operations = [ |
| 52 | + { |
| 53 | + operationType: BulkOperationType.Create, |
| 54 | + partitionKey: "A", |
| 55 | + resourceBody: { id: addEntropy("doc1"), name: "sample", key: "A" } |
| 56 | + }, |
| 57 | + { |
| 58 | + operationType: BulkOperationType.Upsert, |
| 59 | + partitionKey: "U", |
| 60 | + resourceBody: { name: "other", toot: "U" } |
| 61 | + }, |
| 62 | + { |
| 63 | + operationType: BulkOperationType.Read, |
| 64 | + id: readItemId, |
| 65 | + partitionKey: true |
| 66 | + }, |
| 67 | + { |
| 68 | + operationType: BulkOperationType.Delete, |
| 69 | + id: deleteItemId, |
| 70 | + partitionKey: {} |
| 71 | + }, |
| 72 | + { |
| 73 | + operationType: BulkOperationType.Replace, |
| 74 | + partitionKey: 5, |
| 75 | + id: replaceItemId, |
| 76 | + resourceBody: { id: replaceItemId, name: "nice", key: 5 } |
| 77 | + } |
| 78 | + ]; |
| 79 | + logStep(`Execute a simple bulk request with 5 operations: Create, Upsert, Read, Delete, Replace`); |
| 80 | + logStep("Bulk Operations Input to 'container.items.bulk(operations):'"); |
| 81 | + console.log(operations); |
| 82 | + const response = await v2Container.items.bulk(operations); |
| 83 | + logStep("Bulk response:"); |
| 84 | + console.log(response); |
| 85 | + await finish(); |
| 86 | +} |
| 87 | + |
| 88 | +run().catch(handleError); |
0 commit comments