Skip to content

Commit

Permalink
fix sample errors and update release CHANGELOG.md (#18768)
Browse files Browse the repository at this point in the history
* fix sample errors and update release CHANGELOG.md

* Ran experimental sample generation.

* update CHANGELOG.md and package.json

Co-authored-by: Will Temple <[email protected]>
  • Loading branch information
sajeetharan and witemple-msft authored Nov 23, 2021
1 parent 1a294f8 commit 8ac6801
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 114 deletions.
11 changes: 6 additions & 5 deletions sdk/cosmosdb/cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Release History

## 3.14.2 (Unreleased)
## 3.15.0 (2021-11-22)

### Features Added

### Breaking Changes

### Bugs Fixed
- _GA_ Adds `container.item(itemId).patch()`. `patch()` is an alternative to `replace()` for item updates. https://github.com/Azure/azure-sdk-for-js/pull/16264/files#diff-7caca690c469e2025576523c0377ac71815f001024fde7c48b20cd24adaa6977R561
- _GA_ support for Bulk operation PATCH.
- _GA_ support for Batch operation PATCH.
- Added the `SasTokenProperties` type and a `createAuthorizationSasToken` function to enable scoped access to Cosmos resources with SAS tokens. For an example that demonstrates creating a SAS token and using it to authenticate a `CosmosClient`, see [the `SasTokenAuth` sample](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cosmosdb/cosmos/samples/v3/typescript/src/SasTokenAuth.ts).

### Other Changes
- Made several changes to the sample programs to improve code quality and compatibility with Node 12, and upgraded the sample programs' dependencies.

## 3.14.1 (2021-09-02)

Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/cosmos",
"version": "3.14.2",
"version": "3.15.0",
"description": "Microsoft Azure Cosmos DB Service Node.js SDK for SQL API",
"sdk-type": "client",
"keywords": [
Expand Down
6 changes: 5 additions & 1 deletion sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ async function updateOfferForCollection(
);

if (container) {
const offer = client.offer(oldOfferDefinition!.id);
const id = oldOfferDefinition!.id;
if (typeof id === "undefined") {
throw new Error("ID for old offer is undefined");
}
const offer = client.offer(id);
logStep("replace old offer with new offer");
await offer.replace(newOfferDefinition);
}
Expand Down
5 changes: 3 additions & 2 deletions sdk/cosmosdb/cosmos/samples-dev/Bulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as dotenv from "dotenv";
dotenv.config({ path: path.resolve(__dirname, "../sample.env") });

import { handleError, finish, logStep } from "./Shared/handleError";
import { BulkOperationType, CosmosClient } from "@azure/cosmos";
import { BulkOperationType, CosmosClient, OperationInput } from "@azure/cosmos";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const key = process.env.COSMOS_KEY || "<cosmos key>";
Expand Down Expand Up @@ -51,6 +51,7 @@ async function run() {
key: true,
class: "2010"
});

await v2Container.items.create({
id: deleteItemId,
key: {},
Expand All @@ -62,7 +63,7 @@ async function run() {
class: "2012"
});

const operations = [
const operations: OperationInput[] = [
{
operationType: BulkOperationType.Create,
partitionKey: "A",
Expand Down
6 changes: 5 additions & 1 deletion sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ async function run(): Promise<void> {
fromNowResults2.map((v) => parseInt(v.id))
);
} catch (err) {
handleError(err);
if (err && err.code !== undefined) {
console.log("Threw, as expected");
} else {
throw err;
}
} finally {
await finish();
}
Expand Down
5 changes: 3 additions & 2 deletions sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ async function run(): Promise<void> {
assert.equal(dbDef && dbDef.id, alsoDbDef && alsoDbDef.id); // The bodies will also almost be equal, _ts will defer based on the read time
// This applies for all response types, not just DatabaseResponse.

console.log("Database with id of " + dbDef && dbDef.id + "' was found");

if (dbDef) {
console.log(`Database with id of ${dbDef.id}' was found`);
}
logStep("delete database with id '" + databaseId + "'");
await finish();
}
Expand Down
28 changes: 20 additions & 8 deletions sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ async function run(): Promise<void> {
{ id: "item1", foo: "bar" },
{ indexingDirective: "exclude" }
);
console.log("Item with id '" + itemDef && itemDef.id + "' created");

if (itemDef) {
console.log(`Item with id ${itemDef.id} 'created`);
}

const querySpec = {
query: "SELECT * FROM root r WHERE r.foo=@foo",
Expand Down Expand Up @@ -90,7 +93,9 @@ async function run(): Promise<void> {
{ id: "item2", foo: "bar" },
{ indexingDirective: "include" }
);
console.log("Item with id '" + itemDef && itemDef2.id + "' created");
if (itemDef) {
console.log(`Item with id ${itemDef.id} 'created`);
}

console.log("Querying all items for a given item should find a result as it was indexed");
const { resources: results2 } = await container.items.query(querySpec).fetchAll();
Expand Down Expand Up @@ -128,7 +133,9 @@ async function run(): Promise<void> {
}
});

console.log("Container '" + containerDef && containerDef.id + "' updated with new index policy");
if (containerDef) {
console.log(`Container ${containerDef.id} 'updated with new index policy`);
}

// create an item
console.log("Creating item");
Expand Down Expand Up @@ -183,7 +190,10 @@ async function run(): Promise<void> {
}
});

console.log("Container '" + containerDef && containerDef.id + "' updated with excludedPaths");
if (containerDef) {
console.log(`Container ${containerDef.id} 'updated with excludedPaths`);
}

// create an item
console.log("Creating item");
const { item: item4 } = await container.items.create({
Expand Down Expand Up @@ -214,10 +224,12 @@ async function run(): Promise<void> {
console.log(result.resources);
throw new Error("Should've produced an error");
} catch (err) {
if (err.code !== undefined) {
console.log("Threw, as expected");
} else {
throw err;
if (err instanceof Error) {
if (err && err.message !== undefined) {
console.log("Threw, as expected");
} else {
throw err;
}
}
}

Expand Down
43 changes: 22 additions & 21 deletions sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ async function run(): Promise<void> {
for (const itemDef of itemDefList) {
console.log(itemDef.id);
}

const item = container.item(itemDefList[0]!.id, undefined);
const id = itemDefList[0]!.id;
if (typeof id === "undefined") {
throw new Error("Id is undefined");
}
const item = container.item(id, undefined);
logStep("Read item '" + item.id + "'");
const { resource: readDoc } = await item.read();
console.log("item with id '" + item.id + "' found");
Expand Down Expand Up @@ -109,13 +112,12 @@ async function run(): Promise<void> {
logStep("Replace item with id '" + item.id + "'");
const { resource: updatedPerson } = await container.items.upsert(person);

console.log(
"The '" + person.id + "' family has lastName '" + updatedPerson && updatedPerson.lastName + "'"
);
console.log(
"The '" + person.id + "' family has " + updatedPerson &&
updatedPerson.children.length + " children '"
);
if (person && updatedPerson) {
console.log("The '" + person.id + "' family has lastName '" + updatedPerson.lastName + "'");
console.log(
"The '" + person.id + "' family has " + updatedPerson.children.length + " children '"
);
}

logStep("Trying to replace item when item has changed in the database");
// The replace item above will work even if there's a new version of item on the server from what you originally read
Expand All @@ -133,7 +135,7 @@ async function run(): Promise<void> {
await item.replace(person, { accessCondition: { type: "IfMatch", condition: person._etag } });
throw new Error("This should have failed!");
} catch (err) {
if (err.code === 412) {
if (err && err.code === 412) {
console.log("As expected, the replace item failed with a pre-condition failure");
} else {
throw err;
Expand All @@ -149,21 +151,20 @@ async function run(): Promise<void> {
// a non-identity change will cause an update on upsert
upsertSource.foo = "baz";
const { resource: upsertedPerson1 } = await container.items.upsert(upsertSource);
console.log(
`Upserted ${upsertedPerson1 && upsertedPerson1.id} to id ${upsertedPerson1 &&
upsertedPerson1.id}.`
);

if (upsertedPerson1) {
console.log(`Upserted ${upsertedPerson1.id} to id ${upsertedPerson1.id}.`);
}
// an identity change will cause an insert on upsert
upsertSource.id = "HazzardFamily";
const { resource: upsertedPerson2 } = await container.items.upsert(upsertSource);
console.log(
`Upserted ${upsertedPerson2 && upsertedPerson2.id} to id ${upsertedPerson2 &&
upsertedPerson2.id}.`
);
if (upsertedPerson2) {
console.log(`Upserted ${upsertedPerson2.id} to id ${upsertedPerson2.id}.`);
}

if (upsertedPerson1.id === upsertedPerson2.id) {
throw new Error("These two upserted records should have different resource IDs.");
if (upsertedPerson1 && upsertedPerson2) {
if (upsertedPerson1.id === upsertedPerson2.id) {
throw new Error("These two upserted records should have different resource IDs.");
}
}

logStep("Delete item '" + item.id + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ async function updateOfferForCollection(newRups, dbName, collectionName, oldOffe
);

if (container) {
const offer = client.offer(oldOfferDefinition.id);
const id = oldOfferDefinition.id;
if (typeof id === "undefined") {
throw new Error("ID for old offer is undefined");
}
const offer = client.offer(id);
logStep("replace old offer with new offer");
await offer.replace(newOfferDefinition);
}
Expand Down
1 change: 1 addition & 0 deletions sdk/cosmosdb/cosmos/samples/v3/javascript/Bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async function run() {
key: true,
class: "2010",
});

await v2Container.items.create({
id: deleteItemId,
key: {},
Expand Down
6 changes: 5 additions & 1 deletion sdk/cosmosdb/cosmos/samples/v3/javascript/ChangeFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ async function run() {
fromNowResults2.map((v) => parseInt(v.id))
);
} catch (err) {
handleError(err);
if (err && err.code !== undefined) {
console.log("Threw, as expected");
} else {
throw err;
}
} finally {
await finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ async function run() {
assert.equal(dbDef && dbDef.id, alsoDbDef && alsoDbDef.id); // The bodies will also almost be equal, _ts will defer based on the read time
// This applies for all response types, not just DatabaseResponse.

console.log("Database with id of " + dbDef && dbDef.id + "' was found");

if (dbDef) {
console.log(`Database with id of ${dbDef.id}' was found`);
}
logStep("delete database with id '" + databaseId + "'");
await finish();
}
Expand Down
28 changes: 20 additions & 8 deletions sdk/cosmosdb/cosmos/samples/v3/javascript/IndexManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ async function run() {
{ id: "item1", foo: "bar" },
{ indexingDirective: "exclude" }
);
console.log("Item with id '" + itemDef && itemDef.id + "' created");

if (itemDef) {
console.log(`Item with id ${itemDef.id} 'created`);
}

const querySpec = {
query: "SELECT * FROM root r WHERE r.foo=@foo",
Expand Down Expand Up @@ -89,7 +92,9 @@ async function run() {
{ id: "item2", foo: "bar" },
{ indexingDirective: "include" }
);
console.log("Item with id '" + itemDef && itemDef2.id + "' created");
if (itemDef) {
console.log(`Item with id ${itemDef.id} 'created`);
}

console.log("Querying all items for a given item should find a result as it was indexed");
const { resources: results2 } = await container.items.query(querySpec).fetchAll();
Expand Down Expand Up @@ -127,7 +132,9 @@ async function run() {
},
});

console.log("Container '" + containerDef && containerDef.id + "' updated with new index policy");
if (containerDef) {
console.log(`Container ${containerDef.id} 'updated with new index policy`);
}

// create an item
console.log("Creating item");
Expand Down Expand Up @@ -182,7 +189,10 @@ async function run() {
},
});

console.log("Container '" + containerDef && containerDef.id + "' updated with excludedPaths");
if (containerDef) {
console.log(`Container ${containerDef.id} 'updated with excludedPaths`);
}

// create an item
console.log("Creating item");
const { item: item4 } = await container.items.create({
Expand Down Expand Up @@ -213,10 +223,12 @@ async function run() {
console.log(result.resources);
throw new Error("Should've produced an error");
} catch (err) {
if (err.code !== undefined) {
console.log("Threw, as expected");
} else {
throw err;
if (err instanceof Error) {
if (err && err.message !== undefined) {
console.log("Threw, as expected");
} else {
throw err;
}
}
}

Expand Down
Loading

0 comments on commit 8ac6801

Please sign in to comment.