← Changelog
NewApps

Transactions across records, and an increment that stops at a min or max

Booking the last seat or selling the last unit meant reading a count, checking it and writing, and two people doing that at once could both get through. Now api.$transaction reads records, collects the writes that depend on them, and makes them together or not at all, only if nothing it read has changed; when something has, your function runs again with fresh reads. For a single counter, increment(-1, { min: 0 }) takes one only while there's one to take.

Inside a transaction, tx reads like api and its create, update and delete are collected rather than sent. They are written in one go when the function returns, so a booking and its class's count land together, and a refused write refuses them all. The function may run up to four times, so emails and charges belong outside it. Writes are direct: the models' own actions don't run. Up to 250 writes.

An increment with a min or max is checked on the stored value under the record's lock. One that would cross the bound answers 409 out_of_bounds and writes nothing. It works wherever an increment does: update, bulk updates, save in an action, and the REST API as { "_increment": -1, "min": 0 }.

Both need SDK 0.16.9 or later. On the REST API a transaction is POST …/transactions, and the test harness runs them too.

More in Transactions.