You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Node.js tutorial for the SQL API for Azure Cosmos DB
3
3
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
5
6
ms.service: cosmos-db
6
7
ms.subservice: cosmosdb-sql
7
8
ms.devlang: javascript
8
9
ms.topic: tutorial
9
-
ms.date: 08/26/2021
10
-
ms.author: dech
10
+
ms.date: 05/02/2022
11
11
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
-
14
12
---
13
+
15
14
# Tutorial: Build a Node.js console app with the JavaScript SDK to manage Azure Cosmos DB SQL API data
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.
26
27
27
28
In this tutorial, you will:
28
29
29
30
> [!div class="checklist"]
31
+
>
30
32
> * Create and connect to an Azure Cosmos DB account.
31
33
> * Set up your application.
32
34
> * Create a database.
33
35
> * Create a container.
34
36
> * Add items to the container.
35
37
> * Perform basic operations on the items, container, and database.
38
+
>
36
39
37
-
## Prerequisites
40
+
## Prerequisites
38
41
39
42
Make sure you have the following resources:
40
43
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/).
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).
## <aid="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
54
76
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
56
78
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:
1. Create and initialize a `package.json` file. Use the following command:
72
87
73
-
4. Create and initialize a `package.json` file. Use the following command:
74
-
*```npm init -y```
88
+
```bash
89
+
npm init -y
90
+
```
75
91
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:
78
93
79
-
## <aid="Config"></a>Set your app's configurations
94
+
```bash
95
+
npm install @azure/cosmos --save
96
+
```
97
+
98
+
## Set your app's configurations
80
99
81
100
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:
82
101
83
102
1. Open the *config.js* file in your favorite text editor.
84
103
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**.
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 functionof 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 functionfrom the **Containers** class. After creating a container, you can store and query the data.
104
123
105
124
> [!WARNING]
106
125
> 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
109
128
110
129
1. Open the *app.js* file in your favorite text editor.
111
130
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.
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.
130
161
131
-
## <a id="QueryItem"></a>Query items
162
+
## Query items
132
163
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 functionof the `Items` class.
165
+
166
+
Add the following code to the **try** block to query the items from your Azure Cosmos account:
An item can be created by using the create functionof 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.
140
173
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:
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.
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:
In your terminal, locate your ```app.js``` file and run the command:
168
201
169
-
```bash
202
+
```bash
170
203
node app.js
171
204
```
172
205
173
206
You should see the output of your get started app. The output should match the example text below.
174
207
175
-
```
208
+
```bash
176
209
Created database:
177
210
Tasks
178
211
@@ -190,20 +223,20 @@ Updated isComplete to true
190
223
Deleted item with id: 3
191
224
```
192
225
193
-
## <aid="GetSolution"></a>Get the complete Node.js tutorial solution
226
+
## Get the complete Node.js tutorial solution
194
227
195
228
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 ).
196
229
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:
198
231
199
232
* An [Azure Cosmos DB account][create-account].
200
233
* The [Getting Started](https://github.com/Azure-Samples/azure-cosmos-db-sql-api-nodejs-getting-started) solution available on GitHub.
201
234
202
235
Install the project's dependencies via npm. Use the following command:
203
236
204
-
*```npm install```
237
+
*```npm install```
205
238
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).
207
240
208
241
Then in your terminal, locate your ```app.js``` file and run the command:
209
242
@@ -218,10 +251,11 @@ When these resources are no longer needed, you can delete the resource group, Az
218
251
## Next steps
219
252
220
253
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)
222
256
* 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)
223
257
224
258
> [!div class="nextstepaction"]
225
259
> [Monitor an Azure Cosmos DB account](../monitor-cosmos-db.md)
0 commit comments