onyxframework / onyx

Powerful framework for modern applications 💪
https://api.onyxframework.com/onyx
MIT License
78 stars 5 forks source link

Add `Onyx::SQL.transaction` method #14

Closed vladfaust closed 5 years ago

vladfaust commented 5 years ago
Onyx::SQL.transaction do |tx|
  Onyx::SQL.exec(foo) # Repo uses transaction connection
  tx.commit
end

Should expand to:

Onyx.db.transaction do |tx|
  Onyx::SQL.repo.db = tx.connection
  yield(tx)
  Onyx::SQL.repo.db = Onyx.db
end

Edit: updated with #16 proposals.

aemadrid commented 5 years ago

I'd say go one step further and auto-commit. We might need to add a way to cancel the transaction though:

Onyx::SQL.transaction do |tx|
  res = Onyx::SQL.exec(foo) # Repo uses transaction connection
  tx.cancel if res.something?
end
vladfaust commented 5 years ago

@aemadrid, makes sense. We could then check if tx is closed with closed? method and commit it if it's not closed yet.