When I am learning something new, I start from the genuine basics and build up from there. Azure Cosmos DB is no different and as someone just starting out with Azure, I thought it best to start with one of the most fundamental of services: CRUD operations on data. Admittedly, I may write bad code along the way, because I am just trying to get things to work, not developing a long-lived application.
First, there is no need for me to repeat the information available in some basic Microsoft documentation to get you all set up and ready to go, so I will assume you have done the following. Second, I’ll be leaving out irrelevant code that isn’t part of working with the DB.
- Secured an Azure account (free for 1 month or $200 at the time of this writing)
- Created a Cosmos DB using the SQL (DcoumentDB) API.
- Install Microsoft.Azure.DocumentDB into your Visual Studio solution with NuGet.
- Obtained your endpoint URL and primary key from the Azure Cosmos DB dashboard. This is found under the key tab on the left of the Cosmos DB admin page.
Below is the simple entity I am using to work with the Cosmos DB.
https://gist.github.com/dstarr/ebc3d7b31bdacf1a46a7b3019b3008bf
Now that I have a tidy entity to work with, let’s get some of them into the database.
First, let’s make sure there is a DB there to work with, along with a collection. This is the first thing my command line application does. I am going to admit something here, my call to this method is appended by .Wait() which does exactly what it sounds like. It ensures the async operations in my method are not just awaited within the method. They must come back to move forward. To be clear, this is a hack and an example of learning code. I warned you.
https://gist.github.com/dstarr/f91e7a919f78ae1ad5c33e9ce86686f7
With a database and a collection into which we will store our documents, now it’s time to add some sensor data. I’m using a very simple SensorTickerFactory that just generates random values for the property values of the entities it creates.
https://gist.github.com/dstarr/2d871b8185906c9744f96de1c3a34746
Great! We have some data in our Cosmos DB. When I look at it through the admin portal Data Explorer tool (again, just a tab on the left), I see this:
I will leave the addition of the underscore values to our JSON to you, dear reader, so we can keep on task here.
The next step is to modify pre-existing documents.
https://gist.github.com/dstarr/d6d7027de80cc7622ed91d896736d1be
Awesome! We’ve now updated our documents.
Let’s kill them! The next step is to simply get all the documents in the DB and delete them.
https://gist.github.com/dstarr/78bbe1014a9fe38b776cf5eeda35191c
And there you have it. Basic CRUD operations.
I did some funky things because this code is all Async, but in a real application, we can see why these operations would be Async. Often, you want your UI to continue living its life and responding accordingly which you are performing a data operation.
Are the poco’s JsonProperty attributes required? Also is overriding ToString required?
The JSON properties on the POCO are not necessary. I added them because when the POCO is serialized to JSON, it just looks like cleaner JSON.
I don’t have the code in front of me, but I believe if you don’t override the ToString() the way I did you’ll just get the default object serialization rather than JSON.
Thanks David