Seeding Data into Azure Mobile Services

Okay, Geeks! I'm writing this post for two reasons. Number one is to get you up and running with Azure Command Line Interface (CLI). And two is to show you how you can seed testing data for your Azure Mobile Service apps. So lets get started,

I'm gonna make a simple nodejs application for this demo. If you are a .Net developer, don't worry! just carry along. Once you get the concept, you can also apply it to your .Net applications

First, let me fire up Visual Studio and create a nodejs console application. I'm using Visual Studio 2013 with all the latest updates installed. I'm giving my app name AzureMobileServiceSeedData

After creating the app if you run it for the first time, you will get Hello World printed on the console.

Okay, Good! Let's create some testing data. My testing data are nothing but some collection of JSON objects. I found this link on the web where you can generate json mock data as much as you need. Here is the link http://www.json-generator.com/

So, I've got some mock data for my two tables which are Teams and Groups. Lets create two JSON file in our project and copy and paste those data. My two json files (group.json, team.json) look like this,

group.json

[
  {
    "id": "5532a92a06230cf4bce5c2d4",
    "name": "Group A"
  },
  {
    "id": "5532a92a9ae9da191efb4d81",
    "name": "Group B"
  }
]

team.json

[
  {
    "id": "5532a690145e8c115f7ba17b",
    "point": 9,
    "name": "Bangladesh",
    "groupId": "5532a92a9ae9da191efb4d81"
  },
  {
    "id": "5532a6903581f4c17785aa2d",
    "point": 3,
    "name": "South Africa",
    "groupId": "5532a92a9ae9da191efb4d81"
  },
  {
    "id": "5532a6906d604e2300af4c95",
    "point": 3,
    "name": "Australia",
    "groupId": "5532a92a9ae9da191efb4d81"
  },
  {
    "id": "5532a690659ce7c636637509",
    "point": 2,
    "name": "England",
    "groupId": "5532a92a06230cf4bce5c2d4"
  },
  {
    "id": "5532a690c2f68a06fd5e7b16",
    "point": 2,
    "name": "Ireland",
    "groupId": "5532a92a06230cf4bce5c2d4"
  },
  {
    "id": "5532a6908e079d1597012953",
    "point": 1,
    "name": "Zimbabwe",
    "groupId": "5532a92a06230cf4bce5c2d4"
  }
]

Let's create a Azure Mobile Service. Login to your azure portal and click on Azure Mobile Services and create a new one. It's just a two step process. First, create a azure mobile service and then configure the database for it. I'm using nodejs so I've selected JavaScript as backend


Before you logout just copy the application key to the clipboard from Manage Access Keys window. We will need this when we will start some coding.

Now, its Azure CLI time! In Visual Studio the required npm azure cli pacakge got installed when I first created the console application. So I didn't had to bother installing it seperately. But you can also install it globally with the npm command

npm install -g azure-cli

Okay, lets work with the CLI. Right click the project and fire up command promt in the specific project folder.

Type azure account download. And it will open your brower automatically and will take you to the azure login page.

After login a file will be downloaed in your PC. I placed the file in my desktop for quick access

Going back to the command promt lets switch the directory to desktop and type azure account import [your_subscription_credentials].publishsettings You are successfully logged into your azure account and can do everything you do in the in UI portal now with the command promt. CRAZY RIGHT!?!?!..:D

Time to create the tables for our app. Just type

call azure mobile table create [app_name] [table_name]

For this app it is

call azure mobile table create mobileserviceseeddemo groups
call azure mobile table create mobileserviceseeddemo teams

And you will have two tables created with the names groups and teams.

The last part is a tiny bit of coding. No fear!!! cause fizz is here...:p . But before that lets install a npm package called request for some http callings.

Now, Copy the code below and paste it into your app.js file.


var request = require('request'),
    groups = require('./group.json'),
    teams = require('./team.json');

var url = 'https://mobileserviceseeddemo.azure-mobile.net/tables';

uploadData(url + '/groups', groups);
uploadData(url + '/teams', teams);

function uploadData(url, data) {

    for (var i = 0; i < data.length; i++) {
        addItem(data[i]);
    }


    function addItem(itemToAdd) {
        var options = {
            url: url,
            headers: {
                'Content-Type': 'application/json',
                'X-ZUMO-APPLICATION': 'fWJfgBHgZupKZarCReBRHHrboPamDW92'
            },
            form: itemToAdd
        };

        request.post(options, function (error, response, body) {
            if (error) {
                return console.error('item posting failed:', error);
            }
            console.log('Data seeding successful!  Server responded with:', body);
        });
    }
}

Read it through for once and you will get the idea. Its nothing but a http posting code. All I did is to loop through the JSON object arrays of my two json files (group.json, team.json) and post a single element at a time in their respective urls (i.e: https://mobileserviceseeddemo.azure-mobile.net/tables/groups, https://mobileserviceseeddemo.azure-mobile.net/tables/teams). By the way paste your application key in the X-ZUMO-APPLICATION. And we are done. Run the application and you will get this successfull command promt entries.

To check that I'm not bluffing login to your azure portal and check your mobile service app's data section


So, that's it. I hope you enjoyed the post. Don't forget to share. ;)