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
.
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:
- Retrieve the object
- Modify the
hourlyRate
property value - 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 construct called BackendlessExpression
. The example below demonstrates its usage by giving Sponge Bob from the table shown above a raise:
curl -X PUT -H 'Content-Type:application/json' 'https://myapp.backendless.app/data/Employee' -d '{
"hourlyRate" : {
"___class" : "BackendlessExpression",
"value" : "hourlyRate + 10.00"
},
"objectId": "B43DAC90-1B78-41C9-AD11-43029034A748"
}'
As you can see, the hourlyRate
property in the request, while being a column of a numeric data type, has the following value:
{
"___class" : "BackendlessExpression",
"value" : "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:
The Create Expression
block is located in the Data API section of the BACKENDLESS group of the Codeless toolbox: