user230910
user230910

Reputation: 2372

Azure AppService EasyApi - how to access table data (CRUD)

So in the huge flood of azure information online, I'm having trouble doing something simple:

I have an "App Service" on azure, with 2 "easy tables" set up "User" and "Fish".

I can access the 2 tables fine from a client application, but I'd like to do some processing server side. I've studied the documentation, and I have learned that "EasyAPI" is the way to do it.

So I have a working api that can send me back 2+2 or "hello world", but I would like to access and update the data in my 2 tables.

So if someone could please provide just a really basic sample code:

1) Select all records from FISH

2) Update a given FISH by id

3) Delete a given fish by id

4) Insert a new FISH.

Just the basic CRUD operations. Assume all the required data is already in the method.

(this is the autogenerated apimethod.js file)

module.exports = {
    "get": function (req, res, next) {

    var table = azureMobileApps.table();

    table.read().then(function (data)
    {
        console.log("Got data " + data);
    });

    table.insert({id:"1111"}).then(function (data)
    {
        console.log("Added data " + data);
    });

    table.delete({id:"1111"}).then(function ()
    {
        console.log("Deleted data id 1111");
    });

    table.read({id:"1111"}).then(function (data)
    {
        console.log("Got data for id 1111: " + data);
    });


}

Upvotes: 0

Views: 34

Answers (1)

user230910
user230910

Reputation: 2372

Ok so I spent some time on azure revere engineering the available objects, luckily javascript can query object properties and functions etc.

Hopefully this saves someone else from having to do the same:

module.exports = {

  "get": function (req, res, next) {
    console.log("starting...");
    var tableRef = req.azureMobile.tables("Fish");
    console.log("tableRef:");
    console.log(tableRef);

    /* Here are the available operations on the tableref:
    {   read: [Function],
        find: [Function],
        update: [Function],
        insert: [Function],
        delete: [Function],
        undelete: [Function],
        truncate: [Function],
        initialize: [Function],
        schema: [Function] } 
 */

    /* READ ALL DATA */
    var promise = tableRef.read();
    promise.then(function (data) {
        console.log("GotData (all):");
        console.log(data);
    }); 

    /* READ BY ATTR */
    var promise = tableRef.read({fieldName:'valueToSearchFor'});
    promise.then(function (data) {
        console.log("GotData (single):");
        console.log(data);
    }); 

    /* INSERT */
    var promise = tableRef.insert({fieldName: 'FieldValue'});
    promise.then(function (data) {
        console.log("Inserted:");
        console.log(data);
    }); 

    /* UPDATE */
    var promise = tableRef.update({id: 'guid....Id...216523234', FieldToUpdate: 'ValueToChangeTo'});
    promise.then(function (data) {
        console.log("Updated:");
        console.log(data);
    }); 

    /* DELETE */
    var promise = tableRef.delete({FieldToSearchOn: 'ValueToSearchOn'});
    promise.then(function (data) {
        console.log("Deleted:");
        console.log(data);
    }); 

    console.log("fin");
    res.json("Done");


  }
}

Upvotes: 1

Related Questions