Example To Access Azure Cosmos DB With NodeJS

Go to data explorer from left navigation and click on new container . Give new databaseid and container name. Also provide partition key for container . Configure other settings as per your requirement or leave as default . Then Click on create .

Once the database is created, you need to install azure cosmosDB sdk for nodejs using npm run below command to install the sdk

npm install @azure/cosmos

Now create file called app.js and import sdk then create connection as below . Make sure you replace the endpoint and key with your original values .

Node

const { CosmosClient } = require(“@azure/cosmos”);
const endpoint = “YOUR_COSMOSDB_ENDPOINT”;
const key = “YOUR_COSMOSDB_KEY”;

const client = new CosmosClient({ endpoint, key });

  • In this article we will write code for creating item in azure cosmosDB . Replace database and container ID’s with your original ID’s .The following code will create item with id “gfg1” and name “gfg Item”.

Node

const database = client.database(Your_databaseId);
const container = database.container(Your_containerId);

const newItem = { id: “gfg1”, name: “gfg Item” };

const { resource: createdItem } = await container.items.create(newItem);
console.log(`Created item with id: ${createdItem.id}`);

  • Once you write above code run it using below command .
node app.js
  • You should see similar output as below.

  • You can also see inserted item under data explorer by navigating to your database and container.

How to Setup Azure Cosmos DB?

Azure Cosmos DB is a managed database service that provides high availability and scalability to applications. It provides support for various NoSQL databases. It is available in cross regions hence providing high throughput. In this article, we will set up Azure cosmos DB for NoSQL. So let’s see how to set it up.

Instead of you maintaining the Database in Azure Virtual Machine Azure will create the database and they will maintain it for you by this you can more focus on the data than managing the database.

Similar Reads

Steps to Setup Azure Cosmos DB

Step 1: Navigate to the Azure Cosmos DB page on the Azure portal. Under this page click on Create to create Azure Cosmos DB account....

Example To Access Azure Cosmos DB With NodeJS

Go to data explorer from left navigation and click on new container . Give new databaseid and container name. Also provide partition key for container . Configure other settings as per your requirement or leave as default . Then Click on create ....

FAQ’s On Azure Cosmos DB

...