Skip to content

Inline Update (BackendlessExpression)

Consider a scenario when a numeric property of an object should be updated with an arithmetic operation. For example, the database table below is called Employee and contains employee objects with properties name and hourlyRate.

employees-with-hourlyrate

Suppose you need to modify Sponge Bob's hourlyRate and give him a nice pay bump. The most straight-forward approach would look like this:

  1. Retrieve the object
  2. Modify the hourlyRate property value
  3. Save the object back in the database.

This approach results in two API requests - one to retrieve the object and the other to save it back. This inefficiency can be solved with the "Inline Update" feature discussed in this chapter.

The Inline Update feature is possible with a special class called BackendlessExpression. The example below demonstrates its usage by giving Sponge Bob from the table shown above a raise:

// BLOCKING API
BackendlessExpression expression = new BackendlessExpression( "hourlyRate + 10.00" );
Map<String, Object> entityMap = new HashMap<>();
entityMap.put( "hourlyRate", expression );
entityMap.put( "objectId", "57DEA19E-9CBC-4254-8BD5-8A9F72E57C21" );
Backendless.Persistence.of( "Employee" ).save( entityMap );

// NON-BLOCKING API
BackendlessExpression expression = new BackendlessExpression( "hourlyRate + 10.00" );
Map<String, Object> entityMap = new HashMap<>();
entityMap.put( "hourlyRate", expression );
entityMap.put( "objectId", "57DEA19E-9CBC-4254-8BD5-8A9F72E57C21" );
Backendless.Persistence.of( "Employee" ).save( entityMap, new AsyncCallback<Map>() {
   @Override
   public void handleResponse( Map updatedEntityMap )
   {
      log.info( "Entity map has been updated to: " + updatedEntityMap );
   }
   @Override
   public void handleFault( BackendlessFault fault )
   {
      log.info( "Server reported an error - " + fault );
   }
} );
// BLOCKING API
BackendlessExpression expression = new BackendlessExpression( "hourlyRate + 10.00" );
Employee employee = new Employee();
employee.setObjectId( "57DEA19E-9CBC-4254-8BD5-8A9F72E57C21" );
employee.setHourlyRate( expression );
Employee updatedEmploy = Backendless.Persistence.of( Employee.class ).save( employee );

// NON-BLOCKING API
BackendlessExpression expression = new BackendlessExpression( "hourlyRate + 10.00" );
Employee employee = new Employee();
employee.setObjectId( "57DEA19E-9CBC-4254-8BD5-8A9F72E57C21" );
employee.setHourlyRate( expression );
Backendless.Persistence.of( Employee.class ).save( employee, new AsyncCallback<Employee>() {
  @Override
  public void handleResponse( Employee updatedEntity )
  {
     log.info( "Employee instance has been updated to: " + updatedEntity );
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
     log.info( "Server reported an error - " + fault );
  }
} );

As you can see, the hourlyRate property in the request, while being a column of a numeric data type, has the following value:

BackendlessExpression expression = new BackendlessExpression( "hourlyRate + 10.00" );

Notice the BackendlessExpression value contains an arithmetic expression ("hourlyRate + 10.00"). The database evaluates and executes the expression. The final value in the column will be the result of the expression - 10.00 is added to the value of the hourlyRate column at the time of the API call.

The BackendlessExpression value is always a string. It can reference multiple columns from the same table, but at the present moment is limited only to the basic four arithmetic operations - addition, subtraction, division and multiplication.

The Inline Update (BackendlessExpression) can be used in different APIs:

  • Update single object
  • Update multiple objects
  • Single or Bulk update in transactions

Codeless Reference

The example below demonstrates the BackendlessExpression usage in Codeless logic. The logic updates the Sponge Bob hourly rate by increasing it by 10.00:

backendless-expression-codeless

The Create Expression block is located in the Data API section of the BACKENDLESS group of the Codeless toolbox:

create-expression-codeless