Martin Andersen
Martin Andersen

Reputation: 51

Azure mobile apps CRUD operations on SQL table (node.js backend)

This is my first post here so please don't get mad if my formatting is a bit off ;-) I'm trying to develop a backend solution using Azure mobile apps and node.js for server side scripts. It is a steep curve as I am new to javaScript and node.js coming from the embedded world. What I have made is a custom API that can add users to a MSSQL table, which is working fine using the tables object. However, I also need to be able to delete users from the same table. My code for adding a user is:

var userTable = req.azureMobile.tables('MyfUserInfo');
item.id = uuid.v4();

userTable.insert(item).then( function (){
    console.log("inserted data");
    res.status(200).send(item);
});

It works. The Azure node.js documentation is really not in good shape and I keep searching for good example on how to do simple things. Pretty annoying and time consuming. The SDK documentation on delete operations says it works the same way as read, but that is not true. Or I am dumb as a wet door. My code for deleting looks like this - it results in exception

query = queries.create('MyfUserInfo')
        .where({ id: results[i].id });

userTable.delete(query).then( function(delet){
console.log("deleted id ", delet);
});

I have also tried this and no success either

userTable.where({ id: item.id }).read()
        .then( function(results) {
        if (results.length > 0) 
        {
            for (var i = 0; i < results.length; i++)
            {
                userTable.delete(results[i].id);
                });
            }
        }

Can somebody please point me in the right direction on the correct syntax for this and explain why it has to be so difficult doing basic stuff here ;-) It seems like there are many ways of doing the exact same thing, which really confuses me. Thanks alot Martin

Upvotes: 3

Views: 963

Answers (2)

Adrian Hall
Adrian Hall

Reputation: 8035

Why are you doing a custom API for a table? Just define the table within the tables directory and add any custom authorization / authentication.

Upvotes: 1

jdruid
jdruid

Reputation: 613

You could issue SQL in your api

var api = {
get: (request, response, next) => {
    var query = {
        sql: 'UPDATE TodoItem SET complete=@completed',
        parameters: [
            { name: 'completed', value: request.params.completed }
        ]
    };

    request.azureMobile.data.execute(query)
    .then(function (results) {
        response.json(results);
    });
}};
module.exports = api;

That is from their sample on GitHub Here is the full list of samples to take a look at

Upvotes: 2

Related Questions