Ardjson: https://ardjson.codeplex.com    

Previous: Ardjson-Part 9- Azure Mobile Service Table id options


.In Part 9 of this series, it was shown how to create a Version 1 table such that the id field is an auto-incremented integer which saves storage space on a small device. Version 1 tables don't automatically save a creation and modification date though. This blog covers how to do it with a Script..


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

  • Browse
  • Scripts
  • Columns
  • Permissions.

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.


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.


Scripts

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.

 


 

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: