Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction support #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add transaction support #31

wants to merge 2 commits into from

Conversation

awm33
Copy link

@awm33 awm33 commented Oct 1, 2018

Adds transaction. With this PR, hooks can optionally make use of the transaction by adding it to the context in a pre-hook and committing in a post-hook.

Addresses #16

@tommybananas
Copy link
Owner

Couple questions:

  1. is this optional? Where is the flag?
  2. Can you provide test cases and documentation?
  3. REST operations are typically one db operation anyway, what use cases are you targeting specifically?

@awm33
Copy link
Author

awm33 commented Oct 1, 2018

@tommybananas

  1. is this optional? Where is the flag?

In the hooks, a user does not have to pass the options.transaction to operations being performed there. However, the rollback on failed promise chain would still be there. If I were to add an option on the resource as transactions or useTransactions true/false suffice?

  1. Can you provide test cases and documentation?

Yes, but I'd like to nail down the approach first.

REST operations are typically one db operation anyway

Many endpoints have to schedule jobs, hit third party services, or alter another part of the db. The specific use case I am looking for is writing an event to another table within the dband hitting a 3rd-party service.

@awm33
Copy link
Author

awm33 commented Oct 1, 2018

"A Note About Transactions" in the sequelize hooks docs (sorry, no anchor tags on the headers) has a more info as well.

@tommybananas
Copy link
Owner

tommybananas commented Oct 1, 2018 via email

@awm33
Copy link
Author

awm33 commented Oct 2, 2018

@tommybananas A simple approach may be that a user can add an unmanaged transaction to the context in one of the pre-write hooks, if the read or write hook sees a transaction on the context, then it passes it down to any db operations. They would have to commit is a post-write hook, but if they are using a transaction, they probably want to control when that happens anyway.

@awm33 awm33 changed the title Add transaction support to create, update, delete Add transaction support Oct 2, 2018
@awm33
Copy link
Author

awm33 commented Oct 2, 2018

@tommybananas I updated the code with the new approach. One concern I have with this, is if an error occurs somewhere along all the hooks, including internal lib hooks, what's the best way to rollback?

@tommybananas
Copy link
Owner

@awm33 couple thoughts.

One is that it might make sense to change default error and final milestone handlers to check for transactions and commit/abort automatically for simple cases and minimizing boilerplate.

I'm not a huge fan of requiring the override of the error handler because they are separate things and making them reimplement the error handler seems like a bad idea. Another option would be a separate milestone for transactions.. just brainstorming.

@tommybananas
Copy link
Owner

but I am open to getting the first run of this feature out asap so long as we document it and write a test or two.

@awm33
Copy link
Author

awm33 commented Oct 6, 2018

@tommybananas I think the approach is close and I'm starting on tests. I think I need to add some error handling, since we can't really handle that by adding a hook. An error can occur anywhere, and would short circuit, and we would probably want to rollback on error.

It looks like the error handling is here? https://github.com/tommybananas/finale/blob/master/lib/Controllers/base.js#L179

It looks like we would want to add transaction error handling to each catch case except for the errors.RequestCompleted handler.

I'm thinking there if context.transaction is set, calling context.transaction.rollback().

If that error handling is in place, I think the rest can be managed using milestone hooks.

Thank you for helping me through this approach.

@tommybananas
Copy link
Owner

hmm.. I guess that makes sense. I'm trying to figure out if there would ever be a reason they would not want to rollback the tx on error. In which case they would have to basically override all the error handlers in order to prevent the automatic rollback which would be a pain. But seeing as you are the only one requesting this functionality.. I'm fine with it. If you can add documentation at the very least I will merge this. Thanks for your contribution

@hiradimir
Copy link

I want this feature...

@tommybananas
Copy link
Owner

tommybananas commented Feb 26, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants