What I need to do: Update a column in on one entit...
# help
m
What I need to do: Update a column in on one entity based on another entities column value when adding or updating the second entity Entity one is an account record with a balance The new or updated entity is a transaction record against that account I want to update the balance on the account record based on the value of the new transaction, or an update on the value of the transaction I saw TransactWriteItems as a possible solution, but this appears to update records with the same key values? So my question is, should I be doing this via consecutive calls in the front end, ie add or update the transaction, and on “success” make a second call to update the account balance, or is there something available to update both records in a single call?
I should add: This is a dynamoDb table. Partition and Sort keys are PK and SK. Record being added/updated: PK: ACCT#{accountId} SK: TRANS#{transactionId} Account record to be updated based on above: PK: USER#{userId} SK: ACCT#{accountId}
Im not looking for the whole solution, but in a nudge in the right direction of how I should be approaching this. Thank you!
r
You could make just make two update calls from the backend lambda
m
So on doing that, If the first call succeeds, and the second fails, add a rollback type call in a catch to revert the first put/update?
Either way, I will hash that out. Thank you. That seems the sensible approach
r
Potentially, if you truly need a transaction you could use TransactWriteItems (I've not familiar with it myself but is intended for your use case) There's an example here
m
Nice. I was looking at that feature, but did not see an example like the one you posted. That is exactly what I need, since it will automatically handle a fail all case. I will keep digging into that. Thank you, so much
r
Excellent, you're welcome
Hazzah! it worked In case anyone is wondering: You need to add the function to the util