Partial Updates

The Baqend SDK provides the ability to send partial updates to your app’s data model.
They allow you to update an object without reading it before.

Use Cases

Partial updates help you to improve the concurrency of your app.
Typical situations when you need that are:

Your app is under high load and needs a way to speed up writes.

Your app does not need to load the object and just wants to increase a counter for example.

Take the following example.
You are counting visits of a page in your app and you have a counter field saving that value in the database.
Normally, you would be implementing that the following way:

But then you would have the problem that both users read the old counter state 42, increase it, and write 43 to the database.
Of course, Baqend detects such undesired behavior and rejects one of the write operations.
For both users to be counted, the second user than has to retry her operation though.

Now take the example using partial updates:

In a partial update, you specify operations to perform on the data without reading it first.
So now, we just tell the database to increment the counter field for us and we will get an up-to-date version of the counter field counting both user’s accesses.

Using Partial Updates

To use partial updates, you take an entity (which needs not to be loaded) and call the partialUpdate() method on it.
Then, you can use a fluent interface to call the below operations on the object.

Node:
You cannot perform multiple updates on the same field!
You will have to execute those updates one after another (like in the examples below).

Here is an overview of the supported operations.
Click on the name to see an example and have more detail on their usage.

Minimum and Maximum

Further numerical operations you can use for Double and Integer-typed values are min and max.
With min, you update the field to the lower value compared to the one you give and the one stored in the database.

Date and Time Update Operations

Date, DateTime, and Time fields have special update methods.
Use toNow to update a field to the current date and time in case of a DateTime, to the current time in case of Time, or to the current day in case of a Date field.