0

I used to have an Azure Mobile Service with a JavaScript backend. Now I switched to the new Azure Mobile App with its node.js backend. Since things are handled differently with node.js, I want to ask you how to do some simple things that used to work with the older backend.

The first thing is, how can I modify a "read" or "insert" request on the server side to, for instance, execute a custom query. I used to do that like this:

function read(query, user, request) {
var visitorID = request.parameters.visitor;
var liked = request.parameters.liked;
var link = request.parameters.link;
if (visitorID && !liked && !link) {
    if (visitorID.indexOf(" ") == -1) {
        var sqlUnique = "SELECT TOP 1 * FROM ........;";
        mssql.query(sqlUnique, {
            success: function(results) {
                if(results.length > 0) {
                    request.respond(statusCodes.OK, results);   
                }
                else {
                    request.respond(statusCodes.BAD_REQUEST, {error: 'An error message.'});
                }
            },
            error: function(err) {
                request.respond(statusCodes.BAD_REQUEST, {error: 'There was a problem with the request.'});
            }
        });
    } else {
        request.respond(statusCodes.BAD_REQUEST, {error: 'Invalid username.'});
    }
}
else {
    // no server-side action needed
    request.execute();
}

}

As you can see, whenever a client reads from the server, some other things are getting executed. My question is now, how to do that same thing with Azure Mobile App's node.js backend. Especially when a client passes some parameters within the request. How can I read those parameters in node.js? And how can I execute a custom query before executing the query in the request?

If someone can help me port the above JavaScript code to node.js, I would be very happy!

1 Answer 1

1

If I don't misunderstand, you want to query some custom SQL stmt in table operations in EasyTables scripts in Mobile Apps.

we can leverage "use()" to custom middleware to specify middleware to be executed for every request against the table as the description on the document of azure-mobile-apps sdk at http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_express_tables_table.html#~use.

var queries = require('azure-mobile-apps/src/query');
var readMiddleware = function(req,res,next){
    var table = req.azureMobile.tables('table2'),
    query = queries.create('table2')
            .where({ TestProperty : req.body.testproperty });
    table.read(query).then(function(results) {
        if(results){
            req.someStoreData = somehander(results); //some hander operations here to get what you want to store and will use in next step
            next();
        }else{
            res.send("no data");
        }
    });
};

table.read.use(readMiddleware, table.operation);
table.read(function (context) {
   console.log(context.req.someStoreData);
   return context.execute();
});

You can refer to the similar question on SO, Query other tables in insert function of Azure Table Storage.

And you can get the params in table operations closure via context.req.query.someparams similar with we handling params in expressjs. You can refer to the code sample on GitHub.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer. But where do I have to write the SQL query? "SELECT * FROM table WHERE ...."
Hi @XHotSniperX, actually, there are 2 approaches, one is to use the Query Object, you can refer to azure.github.io/azure-mobile-apps-node/….
And another approach is to use SQL stmt which seem to be your requirement, you can refer to the code sample on GitHub github.com/Azure/azure-mobile-apps-node/blob/master/samples/…, and you can build the similar code snippet in middleware.
Thank you very much. I will try that and let you know.
Ok, so I don't understand this at all. It used to work with the code that I have posted here. Now it's like 10 times more complicated. I don't know what was the reason to change the backend to node.js....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.