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
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.
12
12
@@ -54,15 +54,19 @@ Before you start writing code to build the application, you can build the framew
54
54
55
55
1. Open your favorite terminal.
56
56
2. Locate the folder or directory where you'd like to save your Node.js application.
57
-
3. Create two empty JavaScript files with the following commands:
57
+
3. Create empty JavaScript files with the following commands:
4. Create and initialize a `package.json` file. Use the following command:
68
72
*```npm init -y```
@@ -74,31 +78,21 @@ Before you start writing code to build the application, you can build the framew
74
78
75
79
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:
76
80
77
-
1. Open ```config.js``` in your favorite text editor.
81
+
1. Open the *config.js* file in your favorite text editor.
78
82
79
-
1. Copy and paste the code snippet below and set properties ```config.endpoint``` and ```config.key``` to your Azure Cosmos DB endpoint URI and primary key. Both these configurations can be found in the [Azure portal](https://portal.azure.com).
83
+
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**.
80
84
81
-
![Get keys from Azure portal screenshot][keys]
82
-
83
-
```javascript
84
-
// ADD THIS PART TO YOUR CODE
85
-
var config = {}
86
-
87
-
config.endpoint="~your Azure Cosmos DB endpoint uri here~";
1. Copy and paste the ```database```, ```container```, and ```items``` data to your ```config``` object below where you set your ```config.endpoint``` and ```config.key``` properties. If you already have data you'd like to store in your database, you can use the Data Migration tool in Azure Cosmos DB rather than defining the data here. You config.js file should have the following code:
87
+
You can find the endpoint and key details in the **Keys** pane of the [Azure portal](https://portal.azure.com).
JavaScript SDK uses the generic terms *container* and *item*. A container can be a collection, graph, or table. An item can be a document, edge/vertex, or row, and is the content inside a container.
96
-
97
-
`module.exports = config;` code is used to export your ```config``` object, so that you can reference it within the ```app.js``` file.
91
+
The JavaScript SDK uses the generic terms *container* and *item*. A container can be a collection, graph, or table. An item can be a document, edge/vertex, or row, and is the content inside a container. In the previous code snippet, the `module.exports = config;` code is used to export the config object, so that you can reference it within the *app.js* file.
98
92
99
93
## <aid="Connect"></a>Connect to an Azure Cosmos DB account
100
94
101
-
1. Open your empty ```app.js``` file in the text editor. Copy and paste the code below to import the ```@azure/cosmos``` module and your newly created ```config``` module.
95
+
1. Open the *app.js* file in a text editor. Copy and paste the code below to import the ```@azure/cosmos``` module and your newly created ```config``` module.
102
96
103
97
```javascript
104
98
// ADD THIS PART TO YOUR CODE
@@ -120,30 +114,31 @@ Now that your app exists, you need to make sure it can talk to Azure Cosmos DB.
120
114
```
121
115
122
116
> [!Note]
123
-
> If connecting to the **Cosmos DB Emulator**, disable TLS verificationfor your node process:
117
+
> If you are connecting to the **Cosmos DB Emulator** instead of the Azure Cosmos account in Azure portal, make sure to disable the TLS verification. You can disable the TLS verification for your node process with the following code:
124
118
> ```
125
119
> process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
126
120
> const client = new CosmosClient({ endpoint, key });
127
121
> ```
128
122
129
123
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.
130
124
131
-
## Create a database
125
+
## Create a database and a container
126
+
127
+
1. Open the *databaseContext.js* file in your favorite text editor.
128
+
129
+
1. Copy and paste the following code to the *databaseContext.js* file. This code defines a function that creates the "Tasks", "Items" database and the container if they don't already exist in your Azure Cosmos account:
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.
134
+
135
+
> [!WARNING]
136
+
> Creating a container has pricing implications. Visit our [pricing page](https://azure.microsoft.com/pricing/details/cosmos-db/) so you know what to expect.
132
137
133
-
1. Copy and paste the code below to set the database ID, and the container ID. These IDs are how the Azure Cosmos DB client will find the right database and container.
134
138
135
-
```javascript
136
-
const client = new CosmosClient({ endpoint, key });
A database can be created by using either the `createIfNotExists` or create function of the **Databases** class. A database is the logical container of items partitioned across containers.
147
142
148
143
2. Copy and paste the **createDatabase** and **readDatabase** methods into the app.js file under the ```databaseId``` and ```containerId``` definition. The **createDatabase** function will create a new database with ID ```FamilyDatabase```, specified from the ```config``` object if it does not already exist. The **readDatabase** function will read the database's definition to ensure that the database exists.
149
144
@@ -187,58 +182,6 @@ Now that you have the code to initialize the Azure Cosmos DB client, let's take
187
182
.catch((error) => { exit(`Completed with error \${JSON.stringify(error)}`) });
188
183
```
189
184
190
-
At this point, your code in ```app.js``` should now look as following code:
0 commit comments