From 9646966abc28ec80af7c3dddbeaea0f05307095f Mon Sep 17 00:00:00 2001 From: aditishree1 <141712869+aditishree1@users.noreply.github.com> Date: Tue, 6 Aug 2024 00:02:02 +0530 Subject: [PATCH] [cosmos] Computed Property V4sample (#30649) ### Packages impacted by this PR @azure/cosmos ### Issues associated with this PR This PR adds sample for updating computed properties of an existing container ### Describe the problem that is addressed by this PR ### 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 . --- .../cosmos/samples-dev/ContainerManagement.ts | 20 ++++++++++++++++++- .../v4/javascript/ContainerManagement.js | 20 ++++++++++++++++++- .../v4/typescript/src/ContainerManagement.ts | 20 ++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts b/sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts index d93510042280..e0dbf1a2401b 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts @@ -107,6 +107,24 @@ async function run(): Promise { .fetchAll(); console.log("computed property query results: ", response.resources); + logStep("Update computed properties on an existing container"); + // read current container definition + const { resource: contDefinition } = await containerWithComputedProperty.read(); + const upperName = { + name: "upperLastName", + query: + "SELECT VALUE UPPER(IS_DEFINED(c.lastName) ? c.lastName : c.parents[0].familyName) FROM c", + }; + if (contDefinition) { + // update computed properties + contDefinition.computedProperties = [upperName]; + // replace container definition with updated computed properties + await containerWithComputedProperty.replace(contDefinition); + console.log("Computed properties updated"); + } else { + console.log("Container definition is undefined."); + } + logStep("Create container with vector embedding and indexing policies"); const vectorEmbeddingPolicy = { vectorEmbeddings: [ @@ -155,7 +173,7 @@ async function run(): Promise { vectorEmbeddingPolicy: vectorEmbeddingPolicy, }; await database.containers.createIfNotExists(containerDefinition); - logStep("Container with vector embedding and indexing policies created"); + console.log("Container with vector embedding and indexing policies created"); await finish(); } diff --git a/sdk/cosmosdb/cosmos/samples/v4/javascript/ContainerManagement.js b/sdk/cosmosdb/cosmos/samples/v4/javascript/ContainerManagement.js index 2bb8ce77e1e9..eb68ea4ea43e 100644 --- a/sdk/cosmosdb/cosmos/samples/v4/javascript/ContainerManagement.js +++ b/sdk/cosmosdb/cosmos/samples/v4/javascript/ContainerManagement.js @@ -103,6 +103,24 @@ async function run() { .fetchAll(); console.log("computed property query results: ", response.resources); + logStep("Update computed properties on an existing container"); + // read current container definition + const { resource: contDefinition } = await containerWithComputedProperty.read(); + const upperName = { + name: "upperLastName", + query: + "SELECT VALUE UPPER(IS_DEFINED(c.lastName) ? c.lastName : c.parents[0].familyName) FROM c", + }; + if (contDefinition) { + // update computed properties + contDefinition.computedProperties = [upperName]; + // replace container definition with updated computed properties + await containerWithComputedProperty.replace(contDefinition); + console.log("Computed properties updated"); + } else { + console.log("Container definition is undefined."); + } + logStep("Create container with vector embedding and indexing policies"); const vectorEmbeddingPolicy = { vectorEmbeddings: [ @@ -151,7 +169,7 @@ async function run() { vectorEmbeddingPolicy: vectorEmbeddingPolicy, }; await database.containers.createIfNotExists(containerDefinition); - logStep("Container with vector embedding and indexing policies created"); + console.log("Container with vector embedding and indexing policies created"); await finish(); } diff --git a/sdk/cosmosdb/cosmos/samples/v4/typescript/src/ContainerManagement.ts b/sdk/cosmosdb/cosmos/samples/v4/typescript/src/ContainerManagement.ts index d93510042280..e0dbf1a2401b 100644 --- a/sdk/cosmosdb/cosmos/samples/v4/typescript/src/ContainerManagement.ts +++ b/sdk/cosmosdb/cosmos/samples/v4/typescript/src/ContainerManagement.ts @@ -107,6 +107,24 @@ async function run(): Promise { .fetchAll(); console.log("computed property query results: ", response.resources); + logStep("Update computed properties on an existing container"); + // read current container definition + const { resource: contDefinition } = await containerWithComputedProperty.read(); + const upperName = { + name: "upperLastName", + query: + "SELECT VALUE UPPER(IS_DEFINED(c.lastName) ? c.lastName : c.parents[0].familyName) FROM c", + }; + if (contDefinition) { + // update computed properties + contDefinition.computedProperties = [upperName]; + // replace container definition with updated computed properties + await containerWithComputedProperty.replace(contDefinition); + console.log("Computed properties updated"); + } else { + console.log("Container definition is undefined."); + } + logStep("Create container with vector embedding and indexing policies"); const vectorEmbeddingPolicy = { vectorEmbeddings: [ @@ -155,7 +173,7 @@ async function run(): Promise { vectorEmbeddingPolicy: vectorEmbeddingPolicy, }; await database.containers.createIfNotExists(containerDefinition); - logStep("Container with vector embedding and indexing policies created"); + console.log("Container with vector embedding and indexing policies created"); await finish(); }