Skip to content

Commit

Permalink
[cosmos] Computed Property V4sample (Azure#30649)
Browse files Browse the repository at this point in the history
### 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 . <adlnu@microsoft.com>
  • Loading branch information
aditishree1 and Aditishree . authored Aug 5, 2024
1 parent 0ee273f commit 9646966
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
20 changes: 19 additions & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ async function run(): Promise<void> {
.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: [
Expand Down Expand Up @@ -155,7 +173,7 @@ async function run(): Promise<void> {
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();
}

Expand Down
20 changes: 19 additions & 1 deletion sdk/cosmosdb/cosmos/samples/v4/javascript/ContainerManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ async function run(): Promise<void> {
.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: [
Expand Down Expand Up @@ -155,7 +173,7 @@ async function run(): Promise<void> {
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();
}

Expand Down

0 comments on commit 9646966

Please sign in to comment.