Using Azure Cosmos DB JavaScript sdk

 Azure cosmos DB is Microsoft's proprietary globally distributed, multi-model database service "for managing data at planet-scale" launched in May 2017.[1] It is schema-agnostic, horizontally scalable, and generally classified as a NoSQL database.

We have discussed about setting up a cosmos DB client from Azure CLI in our last post: https://thenoobsoftwareengineerblog.blogspot.com/2022/09/setting-up-cosmos-db.html

In this post we would discuss about setting up database using one of their most popular sdk which is Java script one.

Let's start with it, upon searching google this is the first link which comes up:


This is from the Microsoft (creator of cosmos DB) and by going through it i found it to be quite useful document. Let me follow it for connecting it my account which I created in my last post. Or maybe before that let's try creating a Database and container associated. 

This doc also says about some of the prerequisites, let look into it whether we satisfy these prerequisites or not. It says you must have an azure subscription and yeah, we do have it for those who are not having it follow: Create Your Azure Free Account Today | Microsoft Azure you would get 200 dollars of free credits.
Then it talks about creating cosmos dB SQL Api account we have already done it in our previous post: https://thenoobsoftwareengineerblog.blogspot.com/2022/09/setting-up-cosmos-db.html

  • Then this document talks about setting up some CORS (Cross Origin resource sharing) rules... This seems to be bit unclear they have attached another link to same document let's check it.

    After going through that document, it seems that CORS support is required to make direct changes from browser to cosmos db. 
    Hmmm.... the description mentioned inside the azure portal makes things clearer than what is mentioned inside this document.
    As of now I not aware which domains should be allowed for now, maybe in future when I am doing it for specific website or domain, I would put only that name for now I am going with * wildcard i.e., allow every domain.

  • Install cosmos package

     
  • Then it asks about getting account credentials, I generally get it from azure portal and search key inside the search bar, you can extract host endpoint and access key from here. This doc which I am referring doesn't talks much about how to get a key from azure portal maybe it's inclined more towards using azure cli.


  • Now, let's try connecting to our dB from our local client. Replace the credential fetched from Azure portal here.


  • Once our client is initialized, we can interact with database, container, items provided.

Creating Database, container and adding items

We have already created up Database and container using azure portal let's try adding up an item to our container.
Here, is my snippet of code for adding up item with id 2,3 and 4



Pro Tip: Make sure to save your file before executing it.

Hurray, our new items are added to our dB using node JS client.
Pro Tip:  entities with same id's are not allowed to be entered, it may result in error.   

Troubleshooting:

  • Upon error dB client uses same HTTP error code conventions.
  • If you try to create an item with same id, then it would result in a conflict and error 409 would be returned (I have seen this error in the sample example).
  • This document also mentions that while accessing cosmos there can be some more transient issues for this follow Retry pattern in the Cloud Design Patterns guide, and the related Circuit Breaker pattern.







Comments

Popular posts from this blog

understanding index.d.ts file

Understanding VS code launch.json file

How I learned Asnyc c#