azure table how to create the table and add json data using javascript

I am trying to write a javascript function that will do two things
1st create a table in azure if it doesnt already exist

and 2nd add a row to the table I just created


I dont know how to do the following:
  1 - pass the name of the table to the azure function that creates the table I put the value in a variable TName and passed that to the function , but I could not get this to work and I had to hard code the name as a temp fix

  2 - how to create the azure table with the data that is in the json

I have tried to look for examples and I just need a very simple example that I can then make the appropriate changes ...

below i where I have got to and any help is really appreciated

example of the json data I am passing to the function:
{"PARTITION_KEY":"100","ROW_KEY":"1234","DISTRIBUTION_KEY":"44k2","ACCOUNT_NUMBER":"ABC1","ACCOUNT_DATE":"11/30/2017","ACCOUNT_TIME":"12:00:00","ACCOUNT_COUNT":"100","ACCOUNT_SIZE":"22.60"}

Open in new window


// here is the function I am trying to build
function (context, iJsonData) {
...
    var azureStorage = require('azure-storage');    
    var AzureEnv = process.env.test_storage;

	var tableService = azureStorage.createTableService(process.env.test_storage);

// get the season ticket data to be loaded  
    var ClientData = iJsonData; 

	var TName = "MyTestTable";

// how to pass TName to the function createTableIfNotExists ???
tableService.createTableIfNotExists('TEST1', function(error, result, response) {
  if(!error) { 
        if(result.created=true)
        {
            context.log("Table "+TName+ " created since it did not exist");
        }
        else
        {
            context.log("Table "+TName+ " already exists");
        }


          context.log("result ");
          context.log(result);
          context.log("response ");
          context.log(response);
          
  }
  else
  {
          context.log("Table "+TName+ " had an error while creating the table");
          context.log("error");
          context.log(error);
          context.log("result ");
          context.log(result);
          context.log("response ");
          context.log(response);
          
  }
});

//How to define the structure of the azure table????

// How to insert the data that is in the json to the table ???

    context.done();
};

Open in new window

LVL 2
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
how to pass TName to the function createTableIfNotExists ???
tableService.createTableIfNotExists(TName, function(error, result, response) {

Open in new window


How to define the structure of the azure table????
Table: A table is a collection of entities. Tables don't enforce a schema on entities, which means a single table can contain entities that have different sets of properties. The number of tables that a storage account can contain is limited only by the storage account capacity limit.

How to insert the data that is in the json to the table ???
From what I read from here, the following should work :
var entGen = azure.TableUtilities.entityGenerator;
var task = {
  PartitionKey: entGen.String('hometasks'),
  RowKey: entGen.String('1'),
  description: entGen.String('take out the trash'),
  dueDate: entGen.DateTime(new Date(Date.UTC(2015, 6, 20))),
};
var account = {
 PartitionKey: entGen.String('accounts'),
 RowKey: entGen.String('1234'),
 "DISTRIBUTION_KEY":entGen.String('"44k2"),
 "ACCOUNT_NUMBER":entGen.String('"ABC1"),
 "ACCOUNT_DATE":entGen.String('"11/30/2017"),
 "ACCOUNT_TIME":entGen.String('"12:00:00"),
 "ACCOUNT_COUNT":entGen.String('"100"),
 "ACCOUNT_SIZE":entGen.String('"22.60"
};
tableSvc.insertEntity(TName, account, function (error, result, response) {
  if(!error){
    // Entity inserted
  }
});

Open in new window


please read note about PartitionKey and RowKey on this page :
https://docs.microsoft.com/fr-fr/rest/api/storageservices/Understanding-the-Table-Service-Data-Model?redirectedfrom=MSDN
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.