0

I have a file uploader using Hasura and AWS S3. Is there a way for me to upload a file and insert into the database, and if one of the operations fail, rollback the change?

Here is some pseudocode to demonstrate what I mean:

query('... hasura query string ...').then(err => {
  if (!err) {
    s3.upload('my file').then(upload_err => {
      if (!upload_err) {
        // Commit database changes
      } else {
        // Rollback database changes
      }
    })
  }
})

The only thing I can think of for doing this, is capture the insert id, then delete the record if the file upload fails. This doesn't seem like a good approach though.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • We first upload to blob storage (Azure in our case) and only then call mutation in hasura. I suppose that you need some kind of `id` returned by hasura? If so: "then delete the record if the file upload fails. This doesn't seem like a good approach though." - I see nothing wrong with approach – Alex Yu Dec 14 '20 at 13:33
  • If you have a way of sending some unique ID as metadata (or use the key) you can have a field 'upload_complete' or something and mark it true in a s3 lambda trigger. That won't delete the record on fail, but it will give a way to query the completed ones only... – Abraham Labkovsky Dec 14 '20 at 17:26

0 Answers0