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 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
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.
function insert(item, user, request) { var d = new Date(); item._createdAt= d.toUTCString(); request.execute(); }
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).
function insert(item, user, request) { var d = new Date(); item.updatedAt= d.toUTCString(); request.execute(); }
You can’t change the id of the record to be deleted in the script.
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.