Working with Azure Cosmos DB using Visual Studio Code

Working with Azure Cosmos DB using Visual Studio Code

Azure Cosmos DB extension for Visual Studio Code gives Azure developers set of powerful commands to working with Cosmos DB. With the help of Azure Cosmos DB extension, from Visual Studio Code, you can easily create and delete databases, collections, and view, modify and delete documents. With the help of this extension, you can connect with Cosmos DB very easily and manage the entities like adding or deleting collections and databases. The overall hierarchical representation gives a straightforward way to understand the overall structure of your Cosmos DB.

To start with, first, you must install the extension from the marketplace. To do that, search for Azure Cosmos DB extension in the marketplace and Click on Install.

Cosmos DB MarketPlace installation
Cosmos DB MarketPlace installation

After installation of the extension, you will find Azure Cosmos DB section of Explore section of Visual Studio Code.

Azure Cosmos DB Extension for Visual Studio Code Installed
Azure Cosmos DB Extension for Visual Studio Code Installed

If you want to take a quick look at all the different types of commands for Azure Cosmos DB, open the Show All Command plate and search for Cosmos. This will give you quick glance at different features.

Azure Cosmos DB Commands
Azure Cosmos DB Commands

Move back to Azure Cosmos DB Extension section in the Explorer panel. From there you can sign in to Azure Account to view your Cosmos DB Accounts inside Visual Studio Code or select “Attach Database Account” to attach any database. When you select “Attach Database Account”, VS Code will show you the list of Database Account API – and you need to select specific to your database.

Attach to Cosmos DB
Attach to Cosmos DB

Once you select the specific Database Account API, Visual Studio Code will ask for the Connection String.

Connect Cosmos DB using Connection String
Connect Cosmos DB using Connection String

 

You can obtain the connection string from the Azure Portal, Navigate to the respective Cosmos DB Resource, and from the left side panel SettingsConnection String Copy the Primary Connection String.

Past the connection string and press Enter. Visual Studio Code now loads and attach the selected Cosmos DB.

Connected Azure Cosmos DB in Visual Studio Code
Connected Azure Cosmos DB in Visual Studio Code

Now you can Add, Modify Database, collection, and documents within Visual Studio Code. Play around with all the commands and features of the extension.

 

Visual Studio Code & Azure Cosmos DB Emulator

The Azure Cosmos DB Emulator provides a local environment that emulates the Azure Cosmos DB service for your development. Using the Azure Cosmos DB Emulator, you can develop and test your application locally, without creating an Azure subscription. From Visual Studio Code, using the Azure Cosmos DB Extension, you can connect with Local Emulator as well.

First, start the local Azure Cosmos DB Emulator, and make sure it’s running (You can check this from system tray ) and verify the access by exploring the local emulator (https://localhost:<running port>/_explorer/index.html )

 

Azure Cosmos DB Emulator - Local Data Explorer
Azure Cosmos DB Emulator – Local Data Explorer

 

Once you verify your Azure Cosmos DB Emulator is running, you can go back to Visual Studio Code and try to attach the emulator by selecting Connected with Azure Cosmos DB Emulator” option

Attach Cosmos DB Emulator
Attach Cosmos DB Emulator

Wait for a while, you will find your local Cosmos DB data also mapped in Visual Studio Code

Connected with Azure Cosmos DB Emulator
Connected with Azure Cosmos DB Emulator

That’s it. This extension is very powerful and if you are developing Azure based solution with Visual Studio code, you must start exploring this.

Hope this helps.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

2 Comments to “Working with Azure Cosmos DB using Visual Studio Code”

Comments are closed.