model icon indicating copy to clipboard operation
model copied to clipboard

What's the recommend way to use transactions?

Open ifyouseewendy opened this issue 6 years ago • 3 comments

👋 I've searched around the code base. It seems the transaction support is removed from hanami model. Hanami::Model.container.gateways[:default].connection seems to be a Sequel::Mysql2::Database which I could call transaction on it. May I ask if there is a recommended way to do it in hanami model? Thanks!

ifyouseewendy avatar Jul 10 '19 13:07 ifyouseewendy

👋

class ArticleRepository < Hanami::Repository
  def method
    transaction do
      # ...
    end
  end
end

marfoldi avatar Apr 30 '21 09:04 marfoldi

Thanks @marfoldi . How about if you need to write records to two or more separate tables in a transaction?

For example, you have some User data and some UserAddress data that needs to be persisted in an atomic way.

monicao avatar Dec 30 '21 01:12 monicao

For example, you have some User data and some UserAddress data that needs to be persisted in an atomic way.

Maybe something like:

def update_user(user_repository: UserRepository.new, user_address_repository: UserAddressRepository.new)
  user_repository.transaction do
    user_repository.update(...)
    user_address_repository.update(...)
  end
end

marfoldi avatar Jan 02 '22 12:01 marfoldi