Transactions in beforeSave hook

So I was trying the new transaction feature added in the REST API. It works great but it seems if we have other operations (like update another collection in our object’s beforeSave), these operations are not committed to the transaction.

Thoughts?

Can you write down an example and mention how you expected it to work?

Parse.Cloud.beforeSave('MyObject', async (req) => {
  const obj = new Parse.Object('OtherObject')
  // the operation below is not being considered in the transaction
  await obj.save()
})

This can be solved by adding a way to start transactions programmatically. For example imagine you want to process and wait for multiple operations on multiple documents:


// New Parse.Transaction ?
const transaction = new Parse.Transaction

const myTransaction = new Parse.Object('Transaction')
myTransaction.set('amount', 50)
 
// Add operation to transaction
await transaction.add(myTransaction.save())

const balance = new Parse.Object('Balance')
balance.increment('amount', myTransaction.get('amount'))

// Add operation to transaction
await transaction.add(balance.save())

// Commit transaction

await transaction.commit()

The transaction is now only supported through the /batch endpoint and there is no support for using it through the SDK yet. I think that the next step could be to improve the Parse.Object.saveAll() method to accept something like: Parse.Object.saveAll([obj1, obj2], { transaction: true }). Thoughts?

Is that something that you would be willed to tackle?

Sure. I’ll try to find some time to work on adding support for transactions in the Javascript SDK.

After that I would be interested in working on improve transactions support. Currently the /batch endpoint is not very useful for use cases like transfer points between users and update their balance.

1 Like