Question

I'd like to override delete operation on my Azure Mobile Services table to make it more like update then real delete. I have additional column named IsDeleted and I'd like to set it's value to true when delete operation is executed.

I figured out, that what I need is:

  • fire my own 'update' inside del function,
  • delete current request.execute()
  • prepare and sent response by myself

That meens my del function should look like that:

function del(id, user, request) {
    // execute update query to set 'isDeleted' - true

    // return standard response
    request.respond();
}

As you can see I'm missing the first part of the function - the update one. Could you help me writing it? I read Mobile Services server script reference but there is no info about making additional queries inside a server script function.

Was it helpful?

Solution

There are basically two ways to do that - using the tables object, and using the mssql object. The links point to the appropriate reference.

Using mssql (I didn't try it, you may need to update your SQL statement):

function del(id, user, request) {
    var sql = 'UPDATE <yourTableName> SET isDeleted = true WHERE id = ?';
    mssql.query(sql, [id], {
        success: function() {
            request.respond(statusCodes.OK);
        }
    });
}

Using tables (again, only tested in notepad):

function del(id, user, request) {
    var table = tables.getTable('YourTableName');
    table.where({ id: id }).read({
        success: function(items) {
            if (items.length === 0) {
                request.respond(statusCodes.NOT_FOUND);
            } else {
                var item = items[0];
                item.isDeleted = true;
                table.update(item, {
                    success: function() {
                        request.respond(statusCodes.OK, item);
                    }
                });
            }
        }
    });
}

OTHER TIPS

There is a Node.js driver for SQL Server that you might want to check out.

The script component of Mobile Services uses node.js. You might want to check out the session from AzureConf called Javascript, meet cloud

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top