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:

Employee *employee = [Employee new];
employee.objectId = @"B43DAC90-1B78-41C9-AD11-43029034A748";

BackendlessExpression *expression = [[BackendlessExpression alloc] initWithValue:@"hourlyRate + 10.00"];

[[[Backendless.shared data] of:[Employee class]] saveWithEntity:employee expression:@{@"hourlyRate": expression} responseHandler:^(Employee *updatedObject) {
    // handle response
} errorHandler:^(Fault *fault) {
    // handle error
}];
let employee = Employee()
employee.objectId = "B43DAC90-1B78-41C9-AD11-43029034A748"

let expression = BackendlessExpression(value: "hourlyRate + 10.00")

Backendless.shared.data.of(Employee.self).save(entity: employee, expression: ["hourlyRate": expression], responseHandler: { updatedObject in
    // handle response
}, errorHandler: { fault in
    // handle error
})
BackendlessExpression *expression = [[BackendlessExpression alloc] initWithValue:@"hourlyRate + 10.00"];

[[[Backendless.shared data] ofTable:@"Employee"] saveWithEntity:@{@"objectId": @"B43DAC90-1B78-41C9-AD11-43029034A748"} expression:@{@"hourlyRate": expression} responseHandler:^(NSDictionary *updatedObject) {
    // handle response
} errorHandler:^(Fault *fault) {
    // handle error
}];
let expression = BackendlessExpression(value: "hourlyRate + 10.00")

Backendless.shared.data.ofTable("Employee").save(entity: ["objectId": "B43DAC90-1B78-41C9-AD11-43029034A748"], expression: ["hourlyRate": expression], responseHandler: { updatedObject in
    // handle response
}, errorHandler: { fault in
    // handle error
})

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

let expression = BackendlessExpression(value: "hourlyRate + 10.00")
BackendlessExpression *expression = [[BackendlessExpression alloc] initWithValue:@"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