From 8ac6801201062ae5596249fe9ababf429e2f55a8 Mon Sep 17 00:00:00 2001 From: Sajeetharan Date: Tue, 23 Nov 2021 20:55:13 +0530 Subject: [PATCH] fix sample errors and update release CHANGELOG.md (#18768) * fix sample errors and update release CHANGELOG.md * Ran experimental sample generation. * update CHANGELOG.md and package.json Co-authored-by: Will Temple --- sdk/cosmosdb/cosmos/CHANGELOG.md | 11 +++-- sdk/cosmosdb/cosmos/package.json | 2 +- .../samples-dev/AlterQueryThroughput.ts | 6 ++- sdk/cosmosdb/cosmos/samples-dev/Bulk.ts | 5 +- sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts | 6 ++- .../cosmos/samples-dev/DatabaseManagement.ts | 5 +- .../cosmos/samples-dev/IndexManagement.ts | 28 +++++++---- .../cosmos/samples-dev/ItemManagement.ts | 43 ++++++++--------- .../v3/javascript/AlterQueryThroughput.js | 6 ++- .../cosmos/samples/v3/javascript/Bulk.js | 1 + .../samples/v3/javascript/ChangeFeed.js | 6 ++- .../v3/javascript/DatabaseManagement.js | 5 +- .../samples/v3/javascript/IndexManagement.js | 28 +++++++---- .../samples/v3/javascript/ItemManagement.js | 45 +++++++++--------- .../v3/typescript/src/AlterQueryThroughput.ts | 6 ++- .../cosmos/samples/v3/typescript/src/Bulk.ts | 5 +- .../samples/v3/typescript/src/ChangeFeed.ts | 8 +++- .../v3/typescript/src/DatabaseManagement.ts | 5 +- .../v3/typescript/src/IndexManagement.ts | 28 +++++++---- .../v3/typescript/src/ItemManagement.ts | 46 +++++++++---------- 20 files changed, 181 insertions(+), 114 deletions(-) diff --git a/sdk/cosmosdb/cosmos/CHANGELOG.md b/sdk/cosmosdb/cosmos/CHANGELOG.md index 9757110cd21c..1f9d6538cb17 100644 --- a/sdk/cosmosdb/cosmos/CHANGELOG.md +++ b/sdk/cosmosdb/cosmos/CHANGELOG.md @@ -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) diff --git a/sdk/cosmosdb/cosmos/package.json b/sdk/cosmosdb/cosmos/package.json index 85563e4b65e2..1cd4a3d2cfac 100644 --- a/sdk/cosmosdb/cosmos/package.json +++ b/sdk/cosmosdb/cosmos/package.json @@ -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": [ diff --git a/sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts b/sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts index a76bdb067b23..29387832d083 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts @@ -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); } diff --git a/sdk/cosmosdb/cosmos/samples-dev/Bulk.ts b/sdk/cosmosdb/cosmos/samples-dev/Bulk.ts index 66a7925df0c4..03b8eb5c0888 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/Bulk.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/Bulk.ts @@ -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 || ""; @@ -51,6 +51,7 @@ async function run() { key: true, class: "2010" }); + await v2Container.items.create({ id: deleteItemId, key: {}, @@ -62,7 +63,7 @@ async function run() { class: "2012" }); - const operations = [ + const operations: OperationInput[] = [ { operationType: BulkOperationType.Create, partitionKey: "A", diff --git a/sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts b/sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts index 969992640264..834e41455a2f 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts @@ -160,7 +160,11 @@ async function run(): Promise { 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(); } diff --git a/sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts b/sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts index b13243051ffd..e80684652caa 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts @@ -40,8 +40,9 @@ async function run(): Promise { 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(); } diff --git a/sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts b/sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts index fc8dfb58d955..a568cfbbda8b 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts @@ -43,7 +43,10 @@ async function run(): Promise { { 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", @@ -90,7 +93,9 @@ async function run(): Promise { { 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(); @@ -128,7 +133,9 @@ async function run(): Promise { } }); - 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"); @@ -183,7 +190,10 @@ async function run(): Promise { } }); - 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({ @@ -214,10 +224,12 @@ async function run(): Promise { 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; + } } } diff --git a/sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts b/sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts index b1d393177187..1f339c71f4a1 100644 --- a/sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts +++ b/sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts @@ -41,8 +41,11 @@ async function run(): Promise { 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"); @@ -109,13 +112,12 @@ async function run(): Promise { 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 @@ -133,7 +135,7 @@ async function run(): Promise { 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; @@ -149,21 +151,20 @@ async function run(): Promise { // 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 + "'"); diff --git a/sdk/cosmosdb/cosmos/samples/v3/javascript/AlterQueryThroughput.js b/sdk/cosmosdb/cosmos/samples/v3/javascript/AlterQueryThroughput.js index 9f181b24bc6c..0cbc367732e7 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/javascript/AlterQueryThroughput.js +++ b/sdk/cosmosdb/cosmos/samples/v3/javascript/AlterQueryThroughput.js @@ -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); } diff --git a/sdk/cosmosdb/cosmos/samples/v3/javascript/Bulk.js b/sdk/cosmosdb/cosmos/samples/v3/javascript/Bulk.js index f9e8e88aaf10..7d21c66ef2c7 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/javascript/Bulk.js +++ b/sdk/cosmosdb/cosmos/samples/v3/javascript/Bulk.js @@ -50,6 +50,7 @@ async function run() { key: true, class: "2010", }); + await v2Container.items.create({ id: deleteItemId, key: {}, diff --git a/sdk/cosmosdb/cosmos/samples/v3/javascript/ChangeFeed.js b/sdk/cosmosdb/cosmos/samples/v3/javascript/ChangeFeed.js index 345f97bb3632..ee00cf8f131f 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/javascript/ChangeFeed.js +++ b/sdk/cosmosdb/cosmos/samples/v3/javascript/ChangeFeed.js @@ -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(); } diff --git a/sdk/cosmosdb/cosmos/samples/v3/javascript/DatabaseManagement.js b/sdk/cosmosdb/cosmos/samples/v3/javascript/DatabaseManagement.js index 884e3027fdb9..ab7789ad2689 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/javascript/DatabaseManagement.js +++ b/sdk/cosmosdb/cosmos/samples/v3/javascript/DatabaseManagement.js @@ -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(); } diff --git a/sdk/cosmosdb/cosmos/samples/v3/javascript/IndexManagement.js b/sdk/cosmosdb/cosmos/samples/v3/javascript/IndexManagement.js index aba3e8d871c0..21ad3b1b3cdf 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/javascript/IndexManagement.js +++ b/sdk/cosmosdb/cosmos/samples/v3/javascript/IndexManagement.js @@ -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", @@ -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(); @@ -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"); @@ -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({ @@ -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; + } } } diff --git a/sdk/cosmosdb/cosmos/samples/v3/javascript/ItemManagement.js b/sdk/cosmosdb/cosmos/samples/v3/javascript/ItemManagement.js index b79572c5a50f..cb5e7ca28773 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/javascript/ItemManagement.js +++ b/sdk/cosmosdb/cosmos/samples/v3/javascript/ItemManagement.js @@ -40,8 +40,11 @@ async function run() { 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"); @@ -108,13 +111,12 @@ async function run() { 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 @@ -132,7 +134,7 @@ async function run() { 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; @@ -149,23 +151,20 @@ async function run() { // 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 + "'"); diff --git a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/AlterQueryThroughput.ts b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/AlterQueryThroughput.ts index a76bdb067b23..29387832d083 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/AlterQueryThroughput.ts +++ b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/AlterQueryThroughput.ts @@ -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); } diff --git a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/Bulk.ts b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/Bulk.ts index 66a7925df0c4..03b8eb5c0888 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/Bulk.ts +++ b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/Bulk.ts @@ -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 || ""; @@ -51,6 +51,7 @@ async function run() { key: true, class: "2010" }); + await v2Container.items.create({ id: deleteItemId, key: {}, @@ -62,7 +63,7 @@ async function run() { class: "2012" }); - const operations = [ + const operations: OperationInput[] = [ { operationType: BulkOperationType.Create, partitionKey: "A", diff --git a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ChangeFeed.ts b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ChangeFeed.ts index 969992640264..2a2f3c2e8cb1 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ChangeFeed.ts +++ b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ChangeFeed.ts @@ -159,8 +159,12 @@ async function run(): Promise { [4], fromNowResults2.map((v) => parseInt(v.id)) ); - } catch (err) { - handleError(err); + } catch (err: any) { + if (err && err.code !== undefined) { + console.log("Threw, as expected"); + } else { + throw err; + } } finally { await finish(); } diff --git a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/DatabaseManagement.ts b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/DatabaseManagement.ts index b13243051ffd..e80684652caa 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/DatabaseManagement.ts +++ b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/DatabaseManagement.ts @@ -40,8 +40,9 @@ async function run(): Promise { 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(); } diff --git a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/IndexManagement.ts b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/IndexManagement.ts index fc8dfb58d955..a568cfbbda8b 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/IndexManagement.ts +++ b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/IndexManagement.ts @@ -43,7 +43,10 @@ async function run(): Promise { { 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", @@ -90,7 +93,9 @@ async function run(): Promise { { 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(); @@ -128,7 +133,9 @@ async function run(): Promise { } }); - 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"); @@ -183,7 +190,10 @@ async function run(): Promise { } }); - 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({ @@ -214,10 +224,12 @@ async function run(): Promise { 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; + } } } diff --git a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ItemManagement.ts b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ItemManagement.ts index 3328b966f14b..1ae1acfa5e63 100644 --- a/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ItemManagement.ts +++ b/sdk/cosmosdb/cosmos/samples/v3/typescript/src/ItemManagement.ts @@ -11,7 +11,6 @@ import * as dotenv from "dotenv"; dotenv.config({ path: path.resolve(__dirname, "../sample.env") }); import { logSampleHeader, handleError, finish, logStep } from "./Shared/handleError"; -import { readFileSync } from "fs"; import { CosmosClient } from "@azure/cosmos"; import { Families } from "./Data/Families.json"; @@ -42,8 +41,11 @@ async function run(): Promise { 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"); @@ -110,13 +112,12 @@ async function run(): Promise { 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 @@ -133,8 +134,8 @@ async function run(): Promise { try { await item.replace(person, { accessCondition: { type: "IfMatch", condition: person._etag } }); throw new Error("This should have failed!"); - } catch (err) { - if (err.code === 412) { + } catch (err: any) { + if (err && err.code === 412) { console.log("As expected, the replace item failed with a pre-condition failure"); } else { throw err; @@ -150,21 +151,20 @@ async function run(): Promise { // 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 + "'");