Skip to content

Commit 29737bc

Browse files
authored
[Cosmos] Adds sample for simple Bulk request (Azure#14128)
* Adds bulk sample * prettier * Fix lint
1 parent 7eab864 commit 29737bc

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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);

sdk/cosmosdb/cosmos/samples/BulkUpdateWithSproc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError";
4+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5+
// @ts-ignore
46
import { CosmosClient } from "../dist";
57
import { endpoint, key, database as databaseId, container as containerId } from "./Shared/config";
68
import { v4 as uuid } from "uuid";

sdk/cosmosdb/cosmos/samples/Shared/handleError.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
import { database, key, endpoint } from "./config";
4+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5+
// @ts-ignore
46
import { CosmosClient } from "../../dist";
57

68
const client = new CosmosClient({ endpoint, key });

sdk/cosmosdb/cosmos/src/utils/batch.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export const BulkOperationType = {
4949
Replace: "Replace"
5050
} as const;
5151

52-
// TODO Make operationInput CreateOperationInput | ...
5352
export type OperationInput =
5453
| CreateOperationInput
5554
| UpsertOperationInput

0 commit comments

Comments
 (0)