title | titleSuffix | description | author | ms.author | ms.reviewer | ms.service | ms.subservice | ms.devlang | ms.custom | ms.topic | ms.date | zone_pivot_groups |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Quickstart - Node.js client library |
Azure Cosmos DB for NoSQL |
Deploy a Node.js Express web application that uses the client library to interact with Azure Cosmos DB for NoSQL data in this quickstart. |
seesharprun |
sidandrews |
mjbrown |
cosmos-db |
nosql |
javascript |
devx-track-js, devx-track-extended-azdevcli |
quickstart-sdk |
06/14/2024 |
azure-cosmos-db-quickstart-env |
[!INCLUDENoSQL]
[!INCLUDEDeveloper Quickstart selector]
Get started with the Azure Cosmos DB for NoSQL client library for Node.js to query data in your containers and perform common operations on individual items. Follow these steps to deploy a minimal solution to your environment using the Azure Developer CLI.
API reference documentation | Library source code | Package (npm) | Azure Developer CLI
[!INCLUDEDeveloper Quickstart prerequisites]
Deploy this project's development container to your environment. Then, use the Azure Developer CLI (azd
) to create an Azure Cosmos DB for NoSQL account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
::: zone pivot="devcontainer-codespace"
::: zone-end
::: zone pivot="devcontainer-vscode"
::: zone-end
::: zone pivot="devcontainer-codespace"
Important
GitHub accounts include an entitlement of storage and core hours at no cost. For more information, see included storage and core hours for GitHub accounts.
::: zone-end
::: zone pivot="devcontainer-vscode"
::: zone-end
-
Open a terminal in the root directory of the project.
-
Authenticate to the Azure Developer CLI using
azd auth login
. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.azd auth login
-
Use
azd init
to initialize the project.azd init --template cosmos-db-nosql-nodejs-quickstart
[!NOTE] This quickstart uses the azure-samples/cosmos-db-nosql-nodejs-quickstart template GitHub repository. The Azure Developer CLI will automatically clone this project to your machine if it is not already there.
-
During initialization, configure a unique environment name.
[!TIP] The environment name will also be used as the target resource group name. For this quickstart, consider using
msdocs-cosmos-db
. -
Deploy the Azure Cosmos DB account using
azd up
. The Bicep templates also deploy a sample web application.azd up
-
During the provisioning process, select your subscription and desired location. Wait for the provisioning process to complete. The process can take approximately five minutes.
-
Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
Deploying services (azd deploy) (✓) Done: Deploying service web - Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io> SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
-
Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.
:::image type="content" source="media/quickstart/dev-web-application.png" alt-text="Screenshot of the running web application.":::
The client library is available through the Node Package Manager, as the @azure/cosmos
package.
-
Open a terminal and navigate to the
/src
folder.cd ./src
-
If not already installed, install the
@azure/cosmos
package usingnpm install
.npm install --save @azure/cosmos
-
Also, install the
@azure/identity
package if not already installed.npm install --save @azure/identity
-
Open and review the src/package.json file to validate that the
azure-cosmos
andazure-identity
entries both exist.
Name | Description |
---|---|
CosmosClient |
This class is the primary client class and is used to manage account-wide metadata or databases. |
Database |
This class represents a database within the account. |
Container |
This class is primarily used to perform read, update, and delete operations on either the container or the items stored within the container. |
PartitionKey |
This class represents a logical partition key. This class is required for many common operations and queries. |
SqlQuerySpec |
This interface represents a SQL query and any query parameters. |
[!INCLUDEDeveloper Quickstart sample explanation]
[!INCLUDEDeveloper Quickstart authentication explanation]
This sample creates a new instance of the CosmosClient
type and authenticates using a DefaultAzureCredential
instance.
:::code language="javascript" source="~/cosmos-db-nosql-nodejs-quickstart/src/cosmos.js" id="create_client" highlight="1,3":::
Use client.database
to retrieve the existing database named cosmicworks
.
:::code language="javascript" source="~/cosmos-db-nosql-nodejs-quickstart/src/cosmos.js" id="get_database":::
Retrieve the existing products
container using database.container
.
:::code language="javascript" source="~/cosmos-db-nosql-nodejs-quickstart/src/cosmos.js" id="get_container":::
Build a new object with all of the members you want to serialize into JSON. In this example, the type has a unique identifier, and fields for category, name, quantity, price, and sale. Create an item in the container using container.items.upsert
. This method "upserts" the item effectively replacing the item if it already exists.
:::code language="javascript" source="~/cosmos-db-nosql-nodejs-quickstart/src/cosmos.js" id="create_item" highlight="10":::
Perform a point read operation by using both the unique identifier (id
) and partition key fields. Use container.item
to get a pointer to an item and item.read
to efficiently retrieve the specific item.
:::code language="javascript" source="~/cosmos-db-nosql-nodejs-quickstart/src/cosmos.js" id="read_item" highlight="4":::
Perform a query over multiple items in a container using container.items.query
. Find all items within a specified category using this parameterized query:
SELECT * FROM products p WHERE p.category = @category
Fetch all of the results of the query using query.fetchAll
. Loop through the results of the query.
:::code language="javascript" source="~/cosmos-db-nosql-nodejs-quickstart/src/cosmos.js" id="query_items" highlight="2,11":::
[!div class="nextstepaction"] Tutorial: Build a Node.js web app