Saturday, September 19, 2020

Save Serverless APIs data in table storage

As you know in ServerlessAPIs with Azure Function post, We had stored azure function API data in-memory (RAM) and if application restart we will lose all our data. That’s why in this article we will save our "ProductsFuncApp” data in Azure Table Storage (NoSQL database). This is a part of azure Storage Account. If you want to know more about storage account. Please read my last post What is Azure Storage Account?

When we are using serverless api (Microservice) then we need to choose serverless database. Table Storage has the advantage of being extremely cheap, with a "consumption-based pricing" model. It's very cost effective and performs well if you have no need for advanced queries.

Another benefit of Table Storage is that if you're using Azure Functions, you've already got a Storage Account (djblogsstorageaccount) that the Azure Functions runtime is making use of, so it's nice and convenient to use that for your tables, especially in the prototyping phase.


How to save serverless API data into table storage?

You need to follow below steps to add table storage as database for your azure function.

1.       Need to install 2 NuGet packages for Table storage.

a.       Microsoft.Azure.Cosmos.Table

b.       Assembly Microsoft.Azure.WebJobs.Extensions


2.       I have added model folder in “ProductsFuncApp” code. Create 3 class files inside it

a.       ProductEntity

b.       ProductModel

c.       Mappings

 


 

3.       “ProductEntity” is entity file. It is mapped with Table storage account. We need to inherit our ProductEntity class from TableEntity so that we have a “RowKey” and “PartitionKey” property (which Table storage requires as it uses them as a combined key and has an “ETag” which is needed when we update rows)

 

4.       “ProductModel” is model class which I have used like data transfer object (DTO) class. It will help me to transfer my “ProductEntity” class into “ProductModel” for end user.  


5.       “Mappings” class will help us to convert our entity “ProductEntity” into model class “ProductModel”. It will be one place for mapping. Which is helpful to minimize our mapping code repetitions in many places.



6.       Once these 3 classes. We need to add our table storage account details in “local.settings.json”. You need to login in azure portal and copy “Connection string” from there.


7.       Just copy “Connection string” in your azure function setting file “local.settings.json”.


8.       Now we will use all these items in our “ProductsFuncApp” azure function "Product" class file. It will look like this.

9.       You can download "ProductsFuncApp” code from my GitHub account.

GitHub: deepakjoshi-info/ProductsFuncApp

10.   Once we update your code as above just publish code in our existing created azure function.


11.   Now code published successfully in our azure function, also we had added API Management (Whatis Azure API management) on this azure functions so you can test it from there as well.

a.       Azure function URL: https://productsfuncapp.azurewebsites.net/  

You can test it with postman and Fiddler

b.       API Management URL: https://productfuncappapi.developer.azure-api.net/

I have made “AddProduct” request with help of API Management as below

Request



Response


12.   Once data saved in Table storage you can “Get Product” with API.

a.       Azure function: https://productsfuncapp.azurewebsites.net/api/Product

b.       API Management: https://productfuncappapi.azure-api.net/ProductsFuncApp/Product

13.   If you want to see the data in table storage account. You need to install “Azure Storage Explorer” in your machine.

Azure Storage Explorer: https://azure.microsoft.com/en-in/features/storage-explorer


 

14.   Once you Install “Azure Storage Explorer” on your machine. You need to login with your azure credentials and need to select your azure subscription.

a.       Select azure subscription

b.       Select azure storage account “djblogsstorageaccount”

c.       Then select “Tables” and “products”

d.       After selecting products, you can see all the saved records

e.       You can update record from here as well.

Hope it will help you to save azure function api data in NoSQL database (Table storage account). Which is very scalable.

Keep learning keep sharing. Cheers

No comments:

Post a Comment