20.Nov.17

Milan Nankov

Milan Nankov

Code Bites: Azure Table Storage - Query by Reverse Chronological Order with JavaScript

Azure Table storage is very efficient NoSQL store offered as an Azure Service. Many times you would want to retrieve the latest n number of entries from your data store. In order for this operation to be efficient, your entries should be stored in reverse chronological order. In Azure Table storage land this means creating an appropriate RowKey for your data entries.

If you are new to Table storage, you could check out this fabulous article on the internals of this service and how it operates. 

So, back to the ordering. Table storage entities are guaranteed to be stored in lexical order and we can use this to our advantage. Querying the most recent entries is called Log tail pattern and is described in detail here. Unfortunately you are out of luck if using JavaScript because the example provided uses C# to create/store entries. Let's rectify this and take a look at a solution using JavaScript.

The following example showcases how you could store an entry in Table storage in the context of Azure Functions but the same concept can be applied when working with Table storage JavaScript client library.

The important piece here is the GenerateKey() method. It returns a RowKey in the format of "reverse-time:guid" which will automatically make the newest entry to be sorted at the top. Once you have this in place, you could easily query your table store for the latest items. 

Cheers.


Need consulting on this topic?

Yes No