Docs‎ > ‎Logic Designer‎ > ‎REST Resources‎ > ‎

Resource Row Events

A Resource Row Event is a piece of JavaScript that gets executed every time a row is retrieved from the database for a specific resource or subresource.

This JavaScript code can then manipulate the row to delete it, or add/drop attributes

The JavaScript code can assume that it will have the following variables defined:

The row variable

row will be the row just retrieved from the database. You retrieve values from it just like any JavaScript object. You can change the values of the object, but this can of course be a dangerous thing to do, since the client will then see data that does not correspond to what is actually in the database.

Note that, if you change the value of an attribute, you will not be able to update this object unless you override the checksum.

You can also add new values, like a computed value. For instance, if you wanted only a specific customer to have an attribute named full_name, composed of the customer's first name and last name, you could add a filter to a resource called (say) Customers:

if (row.custnum === 1234) {
    row.full_name = row.first_name + ' ' + row.last_name;
}

The added attributes can also be a more complicated JavaScript object or array.
if (row.custnum === 1234) {
    row.name_info = {
      'First': row.first_name ,
      'Last' : row.last_name,
      'FullName': row.first_name+ ' ' + row.last_name;
    }
}

You can also filter out the row entirely, if you decide that it should not be sent to the client

if (row.username == 'DoNotShow') {
    row = null;
}

Individual items in a row can be suppressed from the output JSON.
if ('David' == row.username') {
   delete row.last_name;
}

Added attributes will be noted in the @metadata section as 'nonpersistent'.

Removed atrtributes will be noted in the @metadata section as 'removedAttributes'.

The parentRow variable

If the resource is a subresource, and the user is accessing it via its containing resource (i.e. with a URL like /MyParent as opposed to accessing the subresource directly with a URL like /MyParent.MyChild), then the variable parentRow will be defined. It will contain the parent row. You should NOT change anything in this parent row -- it's there only for reference.

// parentRow may be null, need to check first
if (parentRow) {
    row.full_name = parentRow.name + '.' + row.name;
}

The resource variable

If you want to get fancy, you can also use the resource definition, which is passed as the variable resource. This variable has the following methods:

  • getAttributes() returns a list of the attributes defined for this resource. Each attribute has a name, a columnName, and a format (if defined). 
  • getJoinCondition() returns a string, which contains the raw definition of the join to the parent resource, e.g. parent_ident = [ident]
  • getName() returns the name of the resource, e.g. MyChild
  • getNodalName() returns the full name of the resource, e.g. MyParent.MyChild
  • getTableName() returns the name of the table for which the resource was defined.
  • getTopResource() returns the top resource in the resource tree.
  • getParentResource() gets the parent resource (if any) of the resource.

var i;
for (i = 0; i < resource.getAttributes().length(); i++) {
    var attribute = resource.getAttributes().get(i);
}

The req variable

Finally, the req variable will contain an object that describes the request being processed. It will contain the following values:

  • apiKey is the API key object
  • baseURL is the base URL on which the current call was made

Resource Row Events are powerful, but can also be dangerous -- use them with caution.


Example

This example illustrates:
1) skipping row, using aliased name
2) accessing database row before aliasing
3) adding properties to output json
4) unicode usage
5) erasing value (property set to null)
6) using property name that is not valid with dot syntax
7) nonpersistent attributes property added to @metadata section

Create a Customer Resource in the Logic Demo sample as shown below (note the column name alias):



Then paste in the following code as the Row Event:

if (row.myname === 'Alpha and Sons') {
    row = null;
}
else if (row.myname === 'Bravo Hardware') {
    row.foo = 'Hi there!';
    row["2xbalance"] = 2 * tableRow.balance;
    row.credit_limit = null;
}
else if (tableRow.name === 'Charlie\'s Construction') {
    // show some love
    row.myname = '\u2764 ' + tableRow.name + ' \u2764';
}


You can run this in the RESTLab, and observe the additional attributes, foo and 2xbalance and the @metadata changes.