feranto
feranto

Reputation: 497

How to Access tables from scripts in Azure Javascript App Services

I'm working in an rest API, using azure mobile services with a javascript backend. Azure is telling me to migrate from mobile services to the newest App Services. I have read these updates from the official azure blog, that there are some breaking changes in rest api functionality:

https://azure.microsoft.com/en-us/blog/azure-mobile-apps-november-2015-update/

The issue is that I haven't found in the new documentation, how to access tables from scripts in Javascript App Services.

In Mobile Services you do the following if you want to access a table's crud:

exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
//   var tables = request.service.tables;

//accesing sample table
     var sampleTable = request.service.tables.getTable('sample');

// then you can do an insert, update, delete and select through sampleTable ...
...    
}

in App Service you have something like:

module.exports = {
"post": function (req, res, next) {
 //there is no req.service.tables.getTable('TABLE_NAME');
    ...
}

So my question is where is the method tables.getTable() in App Services and how to access it. The official documentation still has the object definitions of Mobile Services, where you can get it trough request.service.tables.getTable('TABLE_NAME').

https://msdn.microsoft.com/en-us/library/azure/jj554218.aspx

Has microsoft not updated it's documentation yet?

Upvotes: 1

Views: 1370

Answers (3)

feranto
feranto

Reputation: 497

Based on an answer in microsoft forums, given by the guys at Microsoft the right way to do it in app services would be something like:

var queries = require('azure-mobile-apps/src/query'),
bodyParser = require('body-parser');


function replyWithError(response) {
    return function(err) {
        response.status(500).json({ error: err });
    }
}


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

 //this is how we access the table   
 var userTable = req.azureMobile.tables('user'),
     query = queries.create('user');

   userTable.read(query).then(function(results) {
     res.status(200).json({ resultados: results });
 }).catch(replyWithError(res));

}

};

I've tested and works like a charm!

Upvotes: 1

For future reference: the Updated docs are in Mobile App API Doc

To access the code behind table structure and CRUD and custom API you must go to "Easy Tables and Easy APIs" options.

How to see/edit the code?

  1. go to "Easy Tables" in your Mobile App in Azure Portal
  2. if you dont have any table create one and select it, or select any existent
  3. click in the Visual Studio Online option "Edit Script" and you will see the code whole service code structures and definitions, if you table is called Example you will se a Example.js with all the start code ready to CRUD

How to get Table Reference?

// get current table

var table = azureMobileApps.table();

// get other table reference

var othertable = azureMobileApps.tables.table("othertable");

Upvotes: 2

Fabio Cavalcante
Fabio Cavalcante

Reputation: 12538

There's a significant amount of documentation work happening as the Node.js Mobile Apps SDK moves towards GA, but I would recommend starting at the GitHub project page here https://github.com/Azure/azure-mobile-apps-node, from there, not only you have access to the code but links to detailed documentation on how to perform the tasks you're asking about.

One thing to keep in mind is that migration, which is what is being recommended, and upgrade are two different things. You can migrate your Mobile Services application to the App Service environment without upgrading to the Mobile Apps Node.js SDK.

Upvotes: 3

Related Questions