When an HTML POST/GET/PATCH/DELETE message is posted to an Azure Mobile Service Table, the message can be intercepted and modified. Also the response can also be intercepted and modified. This blog outlines how to do this...


CEJSON : The Project Codeplex site


This blog is a rework of the ArdJSON version for Arduino devices.

Ardjson: https://ardjson.codeplex.com    

Previous Blog on this topic: Ardjson-Part9b: Azure Mobile Services Scripts


The AzMS Table Portal


When in the Azure Portal for an Azure Mobile Service (AzMS) Table you have several options:

  • Browse
  • Scripts
  • Columns
  • Permissions.
  • image


Browse allows you to to view the data in the table. Scripts allows to to modify the CRUD (Insert, Update, Read and Delete) scripts for that table. Columns enables you to view the columns in the table and add or delete columns. Permissions allows you to set what sort of permissions that you need for each of the CRUD actions.


Columns allows you to modify the columns used in the table but these will dynamically change depending upon the fields in the POST. Not that with this Version 1 table,the index is an integer, not a GUID.

image


You can set the Permissions individually for each action to

  • Everyone
  • Need the App Key
  • Only Authenticated Users
  • OR
  • Only Scripts and Admin can action them.
  • image


Scripts

image

You can insert code that gets actioned before or after an Insert, Update, Read or Delete action is taken with the table. For example, for an Insert a field value can be modified or added.  To edit a script just choose the Script tab then select the type of action you wish to script. The scripts default to the following code:

request.execute();

For example the Insert script is

function insert(item, user, request) {

    request.execute();

}

For an HTML POST you would just need to modify the item before the request.execute is called.

Assuming the backend is JavSscript (.NET backend* would be similar), you just need to use valid JavaScript code. Generic JavaScript is simplest as it doesn’t need extra libraries.

As an example, this following script append a UTC DateTime to the item submitted for insertion. The DateTime doesn’t need to be in the submitted item:

function insert(item, user, request) {

    var d = new Date();
    item.DateTme= d.toUTCString();

    request.execute();

}

 

By default, when a HTTP GET is sent, the id plus all fields except the system ones are returned unless columns are specified with $Select option. A auto-generated fields as above would then by default be returned. If though the field is specified with a leading underscore it would be treated like the System Properties. So one might use in POST _createdAt and in Update (PATCH)  _updatedAt for the DateTime field names as these are the names used for the auto-generated times with Version 2 of the AzMS tables.


Script Parameters

  • item: POST and PATCH - The item being submitted
  • query:HTTP GET – Can create secondary queries using orderBy, orderByDescending, select, skip, take and where, to modify the query/response.
  • id: HTTP DELETE – The id of the record to be deleted.
  • user: Can be used to validate the user’s credentials when non app-key validation is used.
  • request:The HTTP Request. Has the following :parameters property which is a JSON object. Returns a collection of parameters supplied to the request URI as query parameters. ie. Any thing after ? in the URI such as $Select

 

Script Examples

Modifying the Request

function insert(item, user, request) {

    var d = new Date();
    item._createdAt= d.toUTCString();

    request.execute();

}

 

Modifying the Response

function read(query, user, request) {
    request.execute({
        success: function(results) {
            var now = new Date();
            results.forEach(function(item) {
                item.retrievedAt = now;
            });
            request.respond(); //Writes the response
        }
    });
}

(Source: The reference below).

 

Modifying Update

function insert(item, user, request) {

    var d = new Date();
    item.updatedAt= d.toUTCString();

    request.execute();

}
Assuming (??) that the id (like DELETE) can’t be modified, only fields can be added and field values modified.

Modifying DELETE

You can’t change the id of the record to  be deleted in the script.

 


 

Telemetry AzMS Scripts

The table used with CEJSON, telemetry2 has a script that modifies the GET to restrict the response to records for which complete is false. The table is processed (as is currently done only by the Universal app)  so that only the most recent value of a specific sensor is tagged as incomplete.  GET then only returns current values for sensors.  This is implemented by passing a parameter embedded=1 (apply the filter) or embedded=0 (don't apply the filter).

image

This script also selects the fields for the GET response and specifies the sort order when the filter is applied.

 

The table's POST script adds a datetime field to the post as with Version 1 tables, the creation date is not auto-generated. The script for that is the Modifyng Request script as above.

 


 

 

* Footnote:

.NET AzMS backends are only a work in progress at this stage

 

 


 

 

Conclusion

So we now have a simple manner for including Creation DateTime with a Version 1 Azure Mobile Services table. Modification date would involve a similar script with an addition field for the Update script.

 


A Reference for this content is

:

Mobile Services JavaScript (Node.js) backend library


Next: