Skip to content

Commit d971f49

Browse files
aditishree1Aditishree .Copilot
authored
Add deleteAllItemsForPartitionKey samples and add script for generating sample (#36315)
### Packages impacted by this PR @azure/cosmos ### Issues associated with this PR ### Describe the problem that is addressed by this PR This PR adds sample for deleteAllItemsForPartitionKey API and add a script in `package.json` to generate samples. ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) --------- Co-authored-by: Aditishree . <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 2d3ddf5 commit d971f49

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

sdk/cosmosdb/cosmos/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"test:node": "npm run test:node:unit",
5555
"test:node:integration": "cross-env NODE_OPTIONS='--dns-result-order=ipv4first' dev-tool run test:vitest --no-test-proxy -- -c vitest.int.config.ts",
5656
"test:node:unit": "cross-env NODE_OPTIONS='--dns-result-order=ipv4first' dev-tool run test:vitest --no-test-proxy -- -c vitest.unit.config.ts",
57-
"update-snippets": "dev-tool run update-snippets"
57+
"update-snippets": "dev-tool run update-snippets",
58+
"generate-samples": "npm run build && npx dev-tool samples publish"
5859
},
5960
"repository": "github:Azure/azure-sdk-for-js",
6061
"license": "MIT",

sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,40 @@ async function run(): Promise<void> {
209209
});
210210
console.log("Container with full text search policy created");
211211

212+
logStep("Delete all items for a specific partition key");
213+
214+
// Create a container with partition key on 'state' for the deleteAllItemsForPartitionKey demo
215+
const { container: containerForDeletion } = await database.containers.createIfNotExists({
216+
id: "ContainerForDeletion",
217+
partitionKey: { paths: ["/state"] },
218+
});
219+
220+
// Create some sample items with different partition key values
221+
const cities = [
222+
{ id: "1", name: "Olympia", state: "WA", isCapital: true },
223+
{ id: "2", name: "Redmond", state: "WA", isCapital: false },
224+
{ id: "3", name: "Seattle", state: "WA", isCapital: false },
225+
{ id: "4", name: "Springfield", state: "IL", isCapital: true },
226+
{ id: "5", name: "Chicago", state: "IL", isCapital: false },
227+
];
228+
229+
console.log("Creating sample cities...");
230+
for (const city of cities) {
231+
await containerForDeletion.items.create(city);
232+
}
233+
234+
// Delete all items for partition key 'WA'
235+
await containerForDeletion.deleteAllItemsForPartitionKey("WA");
236+
console.log("Deleted all items for partition key 'WA'");
237+
238+
// Query to verify items after deletion
239+
const queryToVerify = "SELECT c.id, c.name, c.state FROM c WHERE c.state = 'WA'";
240+
const { resources: waItems } = await containerForDeletion.items.query(queryToVerify).fetchAll();
241+
console.log(`Items in WA after deletion: ${waItems.length}`);
242+
243+
// Clean up the container
244+
await containerForDeletion.delete();
245+
212246
await finish();
213247
}
214248

sdk/cosmosdb/cosmos/samples/v4/javascript/ContainerManagement.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,40 @@ async function run() {
208208
});
209209
console.log("Container with full text search policy created");
210210

211+
logStep("Delete all items for a specific partition key");
212+
213+
// Create a container with partition key on 'state' for the deleteAllItemsForPartitionKey demo
214+
const { container: containerForDeletion } = await database.containers.createIfNotExists({
215+
id: "ContainerForDeletion",
216+
partitionKey: { paths: ["/state"] },
217+
});
218+
219+
// Create some sample items with different partition key values
220+
const cities = [
221+
{ id: "1", name: "Olympia", state: "WA", isCapital: true },
222+
{ id: "2", name: "Redmond", state: "WA", isCapital: false },
223+
{ id: "3", name: "Seattle", state: "WA", isCapital: false },
224+
{ id: "4", name: "Springfield", state: "IL", isCapital: true },
225+
{ id: "5", name: "Chicago", state: "IL", isCapital: false },
226+
];
227+
228+
console.log("Creating sample cities...");
229+
for (const city of cities) {
230+
await containerForDeletion.items.create(city);
231+
}
232+
233+
// Delete all items for partition key 'WA'
234+
await containerForDeletion.deleteAllItemsForPartitionKey("WA");
235+
console.log("Deleted all items for partition key 'WA'");
236+
237+
// Query to verify items after deletion
238+
const queryToVerify = "SELECT c.id, c.name, c.state FROM c WHERE c.state = 'WA'";
239+
const { resources: waItems } = await containerForDeletion.items.query(queryToVerify).fetchAll();
240+
console.log(`Items in WA after deletion: ${waItems.length}`);
241+
242+
// Clean up the container
243+
await containerForDeletion.delete();
244+
211245
await finish();
212246
}
213247

sdk/cosmosdb/cosmos/samples/v4/typescript/src/ContainerManagement.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,42 @@ async function run(): Promise<void> {
209209
});
210210
console.log("Container with full text search policy created");
211211

212+
logStep("Delete all items for a specific partition key");
213+
214+
// Create a container with partition key on 'state' for the deleteAllItemsForPartitionKey demo
215+
const { container: containerForDeletion } = await database.containers.createIfNotExists({
216+
id: "ContainerForDeletion",
217+
partitionKey: { paths: ["/state"] },
218+
});
219+
220+
// Create some sample items with different partition key values
221+
const cities = [
222+
{ id: "1", name: "Olympia", state: "WA", isCapital: true },
223+
{ id: "2", name: "Redmond", state: "WA", isCapital: false },
224+
{ id: "3", name: "Seattle", state: "WA", isCapital: false },
225+
{ id: "4", name: "Springfield", state: "IL", isCapital: true },
226+
{ id: "5", name: "Chicago", state: "IL", isCapital: false },
227+
];
228+
229+
console.log("Creating sample cities...");
230+
for (const city of cities) {
231+
await containerForDeletion.items.create(city);
232+
}
233+
234+
// Delete all items for partition key 'WA'
235+
await containerForDeletion.deleteAllItemsForPartitionKey("WA");
236+
console.log("Deleted all items for partition key 'WA'");
237+
238+
// Query to verify items after deletion
239+
const queryToVerify = "SELECT c.id, c.name, c.state FROM c WHERE c.state = 'WA'";
240+
const { resources: waItems } = await containerForDeletion.items
241+
.query(queryToVerify)
242+
.fetchAll();
243+
console.log(`Items in WA after deletion: ${waItems.length}`);
244+
245+
// Clean up the container
246+
await containerForDeletion.delete();
247+
212248
await finish();
213249
}
214250

0 commit comments

Comments
 (0)