Working With the JavaScript SDK

The Javascript SDK is a package that can be installed via npm to create,read,update and delete items in an Azure Cosmos DB container. It provides features and options to perform optimized queries to fetch data from documents in your database.

Install the required dependency for the Azure Cosmos DB for NoSQL JavaScript SDK using the below bash command.

npm install @azure/cosmos

Write the Javascript SDK scripts for connecting and working with Cosmos DB from Visual Studio Code Editor in a file called ‘App.js’.Initially, CosmosClient class need to be created to interact with CosmosDB and then the required connection string details are added to access the Azure Cosmos DB. Once the connection is established, the script to fetch data and write it to console log is done using the iteration statement.

Node

const { CosmosClient } = require(“@azure/cosmos”);
const endpoint = “https://cosmosjohndatabase.documents.azure.com:443”; //Add the URI value
const masterkey = ““; // add the key value for the end point
const client = new CosmosClient({ endpoint, auth: {masterkey } });
const databaseId = “cosmosjohndatabase”;
const containerId=”cosmosjohncollection”;

// Below is the code to connect to Cosmos DB and fetch data
async function main() {
const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec, { enableCrossPartitionQuery: true }).toArray();
//looping through data and writing to console
for (var queryResult of results){
let resultString = JSON.stringify(queryResult);
console.log(‘\tQuery returned ${resultString}\n);
}

}
main().catch((error) => {
console.error(error);
});

Once this code is run the resuts are printed in the console using the below command from Windows Powershell:

node App.js

Output

Working with Azure Cosmos DB and JavaScript SDK

Azure Cosmos DB is a No SQL database service that supports flexible schemas and hierarchical data for modern applications. It is a globally distributed, multi-model database service that supports document, key-value, wide-column, and graph databases. Azure Cosmos DB service takes care of database administration, updates, patching, and scaling. It also provides cost-effective serverless and automatic scaling options that match capacity with demand. Cosmos DB integrates with other Azure services and tools, such as Azure Functions, IoT Hub, AKS (Azure Kubernetes Service), App Service, and more. The Azure SDK for JavaScript is a collection of libraries that you can use to interact with various Azure services from your JavaScript applications. It supports both Node.js and browser environments.

In this article, we will learn about working with ‘Azure Cosmos DB and the JavaScript SDK’. CRUD operations and other common operations on Azure Cosmos DB resources can be done using the JavaScript SDK. The Cosmos DB JavaScript SDK is primarily meant for use in Node.js applications.

To work with Azure Cosmos DB, the user needs to create the Cosmos DB by configuring it from the Azure Portal. If you already have an Azure Cosmos DB you can use it by following the steps after the ‘creation of Azure Cosmos DB’. As a prerequisite, users need to have an Azure subscription.

Similar Reads

Steps to Create Azure Azure Cosmos DB

Step 1: Login to Azure Portal Select the Create Cosmos DB option and select the ‘Create’ option...

Working With the JavaScript SDK

The Javascript SDK is a package that can be installed via npm to create,read,update and delete items in an Azure Cosmos DB container. It provides features and options to perform optimized queries to fetch data from documents in your database....

Best Practices For Working With Azure Cosmos Db

...

Troubleshooting Common Azure Cosmos Db Problems

1. Choose The Appropriate Data Model And Right Api...

FAQ’s on Working with Azure Cosmos DB and the JavaScript SDK

Here are some common issues you might encounter and steps to help resolve them:...

Contact Us