Skip to content

Commit 457c1cb

Browse files
authored
Merge pull request #196733 from seesharprun/may2-sql-node-get-started-revamp
Revamp SQL API Node.js "get started" article
2 parents d0435a5 + 25c042a commit 457c1cb

File tree

1 file changed

+86
-52
lines changed

1 file changed

+86
-52
lines changed
Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,107 @@
11
---
22
title: Node.js tutorial for the SQL API for Azure Cosmos DB
33
description: A Node.js tutorial that demonstrates how to connect to and query Azure Cosmos DB using the SQL API
4-
author: deborahc
4+
author: seesharprun
5+
ms.author: sidandrews
56
ms.service: cosmos-db
67
ms.subservice: cosmosdb-sql
78
ms.devlang: javascript
89
ms.topic: tutorial
9-
ms.date: 08/26/2021
10-
ms.author: dech
10+
ms.date: 05/02/2022
1111
ms.custom: devx-track-js
12-
#Customer intent: As a developer, I want to build a Node.js console application to access and manage SQL API account resources in Azure Cosmos DB, so that customers can better use the service.
13-
1412
---
13+
1514
# Tutorial: Build a Node.js console app with the JavaScript SDK to manage Azure Cosmos DB SQL API data
15+
1616
[!INCLUDE[appliesto-sql-api](../includes/appliesto-sql-api.md)]
1717

1818
> [!div class="op_single_selector"]
19+
>
1920
> * [.NET](sql-api-get-started.md)
2021
> * [Java](./create-sql-api-java.md)
2122
> * [Async Java](./create-sql-api-java.md)
2223
> * [Node.js](sql-api-nodejs-get-started.md)
23-
>
24+
>
2425
2526
As a developer, you might have applications that use NoSQL document data. You can use a SQL API account in Azure Cosmos DB to store and access this document data. This tutorial shows you how to build a Node.js console application to create Azure Cosmos DB resources and query them.
2627

2728
In this tutorial, you will:
2829

2930
> [!div class="checklist"]
31+
>
3032
> * Create and connect to an Azure Cosmos DB account.
3133
> * Set up your application.
3234
> * Create a database.
3335
> * Create a container.
3436
> * Add items to the container.
3537
> * Perform basic operations on the items, container, and database.
38+
>
3639
37-
## Prerequisites
40+
## Prerequisites
3841

3942
Make sure you have the following resources:
4043

41-
* An active Azure account. If you don't have one, you can sign up for a [Free Azure Trial](https://azure.microsoft.com/pricing/free-trial/).
44+
* An active Azure account. If you don't have one, you can sign up for a [Free Azure Trial](https://azure.microsoft.com/pricing/free-trial/).
4245

4346
[!INCLUDE [cosmos-db-emulator-docdb-api](../includes/cosmos-db-emulator-docdb-api.md)]
4447

4548
* [Node.js](https://nodejs.org/) v6.0.0 or higher.
4649

4750
## Create Azure Cosmos DB account
4851

49-
Let's create an Azure Cosmos DB account. If you already have an account you want to use, you can skip ahead to [Set up your Node.js application](#SetupNode). If you are using the Azure Cosmos DB Emulator, follow the steps at [Azure Cosmos DB Emulator](../local-emulator.md) to set up the emulator and skip ahead to [Set up your Node.js application](#SetupNode).
52+
Let's create an Azure Cosmos DB account. If you already have an account you want to use, you can skip ahead to [Set up your Node.js application](#set-up-your-nodejs-application). If you're using the Azure Cosmos DB Emulator, follow the steps at [Azure Cosmos DB Emulator](../local-emulator.md) to set up the emulator and skip ahead to [Set up your Node.js application](#set-up-your-nodejs-application).
5053

5154
[!INCLUDE [cosmos-db-create-dbaccount](../includes/cosmos-db-create-dbaccount.md)]
5255

53-
## <a id="SetupNode"></a>Set up your Node.js application
56+
## Set up your Node.js application
57+
58+
Before you start writing code to build the application, you can build the scaffolding for your app. Open your favorite terminal and locate the folder or directory where you'd like to save your Node.js application. Create placeholder JavaScript files with the following commands for your Node.js application:
59+
60+
### [Windows](#tab/windows)
61+
62+
```powershell
63+
fsutil file createnew app.js 0
64+
65+
fsutil file createnew config.js 0
66+
67+
md data
68+
69+
fsutil file createnew data\databaseContext.js 0
70+
```
71+
72+
### [Linux / macOS](#tab/linux+macos)
73+
74+
```bash
75+
touch app.js
5476

55-
Before you start writing code to build the application, you can build the framework for your app. Run the following steps to set up your Node.js application that has the framework code:
77+
touch config.js
5678

57-
1. Open your favorite terminal.
58-
2. Locate the folder or directory where you'd like to save your Node.js application.
59-
3. Create empty JavaScript files with the following commands:
79+
mkdir data
6080

61-
* Windows:
62-
* `fsutil file createnew app.js 0`
63-
* `fsutil file createnew config.js 0`
64-
* `md data`
65-
* `fsutil file createnew data\databaseContext.js 0`
81+
touch data/databaseContext.js
82+
```
83+
84+
---
6685

67-
* Linux/OS X:
68-
* `touch app.js`
69-
* `touch config.js`
70-
* `mkdir data`
71-
* `touch data/databaseContext.js`
86+
1. Create and initialize a `package.json` file. Use the following command:
7287

73-
4. Create and initialize a `package.json` file. Use the following command:
74-
* ```npm init -y```
88+
```bash
89+
npm init -y
90+
```
7591

76-
5. Install the @azure/cosmos module via npm. Use the following command:
77-
* ```npm install @azure/cosmos --save```
92+
1. Install the ``@azure/cosmos`` module via **npm**. Use the following command:
7893

79-
## <a id="Config"></a>Set your app's configurations
94+
```bash
95+
npm install @azure/cosmos --save
96+
```
97+
98+
## Set your app's configurations
8099

81100
Now that your app exists, you need to make sure it can talk to Azure Cosmos DB. By updating a few configuration settings, as shown in the following steps, you can set your app to talk to Azure Cosmos DB:
82101

83102
1. Open the *config.js* file in your favorite text editor.
84103

85-
1. Copy and paste the following code snippet into the *config.js* file and set the properties `endpoint` and `key` to your Azure Cosmos DB endpoint URI and primary key. The database, container names are set to **Tasks** and **Items**. The partition key you will use for this application is **/category**.
104+
1. Copy and paste the following code snippet into the *config.js* file and set the properties `endpoint` and `key` to your Azure Cosmos DB endpoint URI and primary key. The database, container names are set to **Tasks** and **Items**. The partition key you'll use for this application is **/category**.
86105
87106
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/config.js":::
88107
@@ -100,7 +119,7 @@ The JavaScript SDK uses the generic terms *container* and *item*. A container ca
100119

101120
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/data/databaseContext.js" id="createDatabaseAndContainer":::
102121

103-
A database is the logical container of items partitioned across containers. You create a database by using either the `createIfNotExists` or create function of the **Databases** class. A container consists of items which in the case of the SQL API is JSON documents. You create a container by using either the `createIfNotExists` or create function from the **Containers** class. After creating a container, you can store and query the data.
122+
A database is the logical container of items partitioned across containers. You create a database by using either the `createIfNotExists` or create function of the **Databases** class. A container consists of items, which, in the SQL API, are actually JSON documents. You create a container by using either the `createIfNotExists` or create function from the **Containers** class. After creating a container, you can store and query the data.
104123

105124
> [!WARNING]
106125
> Creating a container has pricing implications. Visit our [pricing page](https://azure.microsoft.com/pricing/details/cosmos-db/) so you know what to expect.
@@ -109,70 +128,84 @@ The JavaScript SDK uses the generic terms *container* and *item*. A container ca
109128

110129
1. Open the *app.js* file in your favorite text editor.
111130

112-
1. Copy and paste the code below to import the `@azure/cosmos` module, the configuration, and the databaseContext that you defined in the previous steps.
131+
1. Copy and paste the code below to import the `@azure/cosmos` module, the configuration, and the databaseContext that you defined in the previous steps.
113132

114133
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="ImportConfiguration":::
115134

135+
## Create an asynchronous function
136+
137+
In the *app.js* file, copy and paste the following code to create an asynchronous function named **main** and immediately execute the function.
138+
139+
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" range="18-19,89-91":::
140+
116141
## Connect to the Azure Cosmos account
117142

118-
In the *app.js* file, copy and paste the following code to use the previously saved endpoint and key to create a new CosmosClient object.
143+
Within the **main** method, copy and paste the following code to use the previously saved endpoint and key to create a new CosmosClient object.
119144

120145
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="CreateClientObjectDatabaseContainer":::
121146

122147
> [!Note]
123148
> If connecting to the **Cosmos DB Emulator**, disable TLS verification for your node process:
149+
>
124150
> ```javascript
125151
> process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
126152
> const client = new CosmosClient({ endpoint, key });
127153
> ```
154+
>
155+
156+
Now that you have the code to initialize the Azure Cosmos DB client, add a try/catch block that you'll use for your code performing point operations.
157+
158+
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" range="32,86-88":::
128159
129-
Now that you have the code to initialize the Azure Cosmos DB client, let's take a look at how to work with Azure Cosmos DB resources.
160+
Let's take a look at how to work with Azure Cosmos DB resources.
130161

131-
## <a id="QueryItem"></a>Query items
162+
## Query items
132163

133-
Azure Cosmos DB supports rich queries against JSON items stored in each container. The following sample code shows a query that you can run against the items in your container.You can query the items by using the query function of the `Items` class. Add the following code to the *app.js* file to query the items from your Azure Cosmos account:
164+
Azure Cosmos DB supports rich queries against JSON items stored in each container. The following sample code shows a query that you can run against the items in your container. You can query the items by using the query function of the `Items` class.
165+
166+
Add the following code to the **try** block to query the items from your Azure Cosmos account:
134167

135168
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="QueryItems":::
136169

137-
## <a id="CreateItem"></a>Create an item
170+
## Create an item
138171

139172
An item can be created by using the create function of the `Items` class. When you're using the SQL API, items are projected as documents, which are user-defined (arbitrary) JSON content. In this tutorial, you create a new item within the tasks database.
140173
141-
1. In the app.js file, define the item definition:
174+
1. In the *app.js* file, outside of the **main** method, define the item definition:
142175
143176
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="DefineNewItem":::
144177
145-
1. Add the following code to create the previously defined item:
178+
1. Back within the **try** block of the **main** method, add the following code to create the previously defined item:
146179
147180
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="CreateItem":::
148181
149-
## <a id="ReplaceItem"></a>Update an item
182+
## Update an item
150183
151-
Azure Cosmos DB supports replacing the contents of items. Copy and paste the following code to *app.js* file. This code gets an item from the container and updates the *isComplete* field to true.
184+
Azure Cosmos DB supports replacing the contents of items. Copy and paste the following code to **try** block. This code gets an item from the container and updates the *isComplete* field to true.
152185
153186
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="UpdateItem":::
154187
155-
## <a id="DeleteItem"></a>Delete an item
188+
## Delete an item
156189
157-
Azure Cosmos DB supports deleting JSON items. The following code shows how to get an item by its ID and delete it. Copy and paste the following code to *app.js* file:
190+
Azure Cosmos DB supports deleting JSON items. The following code shows how to get an item by its ID and delete it. Copy and paste the following code to the **try** block:
158191
159192
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js" id="DeleteItem":::
160193
161-
## <a id="Run"></a>Run your Node.js application
194+
## Run your Node.js application
162195
163196
Altogether, your code should look like this:
164197
165198
:::code language="javascript" source="~/cosmosdb-nodejs-get-started/app.js":::
166199
167200
In your terminal, locate your ```app.js``` file and run the command:
168201
169-
```bash
202+
```bash
170203
node app.js
171204
```
172205
173206
You should see the output of your get started app. The output should match the example text below.
174207
175-
```
208+
```bash
176209
Created database:
177210
Tasks
178211
@@ -190,20 +223,20 @@ Updated isComplete to true
190223
Deleted item with id: 3
191224
```
192225
193-
## <a id="GetSolution"></a>Get the complete Node.js tutorial solution
226+
## Get the complete Node.js tutorial solution
194227
195228
If you didn't have time to complete the steps in this tutorial, or just want to download the code, you can get it from [GitHub](https://github.com/Azure-Samples/azure-cosmos-db-sql-api-nodejs-getting-started ).
196229

197-
To run the getting started solution that contains all the code in this article, you will need:
230+
To run the getting started solution that contains all the code in this article, you'll need:
198231
199232
* An [Azure Cosmos DB account][create-account].
200233
* The [Getting Started](https://github.com/Azure-Samples/azure-cosmos-db-sql-api-nodejs-getting-started) solution available on GitHub.
201234
202235
Install the project's dependencies via npm. Use the following command:
203236

204-
* ```npm install```
237+
* ```npm install```
205238

206-
Next, in the ```config.js``` file, update the config.endpoint and config.key values as described in [Step 3: Set your app's configurations](#Config).
239+
Next, in the ```config.js``` file, update the config.endpoint and config.key values as described in [Step 3: Set your app's configurations](#set-your-apps-configurations).
207240
208241
Then in your terminal, locate your ```app.js``` file and run the command:
209242
@@ -218,10 +251,11 @@ When these resources are no longer needed, you can delete the resource group, Az
218251
## Next steps
219252
220253
Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
221-
* If all you know is the number of vcores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md)
254+
255+
* If all you know is the number of vcores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md)
222256
* If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-with-capacity-planner.md)
223257
224258
> [!div class="nextstepaction"]
225259
> [Monitor an Azure Cosmos DB account](../monitor-cosmos-db.md)
226260
227-
[create-account]: create-sql-api-dotnet.md#create-account
261+
[create-account]: create-sql-api-dotnet.md#create-account

0 commit comments

Comments
 (0)