Ecto.Multi like transactions

__musashi_
2023-06-18

__musashi_:

Hi Ash community,

I’m looking to find some examples around how to bundle together database operations into one transaction. Like in Ecto.Multi.

In practice I would like to do something like create or update operation where entry’s existence is being checked before creation. In one transaction.

Can you please point me to the relevant doc resource or show me an example of something similar?

Thank you in advance!

__musashi_:

Ecto.Multi like transactions

zachdaniel:

You have a few options 🙂

  1. Ash.Flow has a transaction step type
  2. you can add before and after action hooks to an action and those happen in the same transaction.
  3. The operation you’re talking about sounds like it might be an upsert (create or update) which is supported as an option when creating.

__musashi_:

Thank you Zach, I’ll look into these.

zachdaniel:

You can also use ecto.multi with ash if you want

__musashi_:

Thanks for noting. I want to try out the Ash ways first :). I found ash_hq repo has some good examples of flow compositions.

I put a link here. Maybe someone else will also be looking for information about this.

https://github.com/ash-project/ash_hq/blob/3832da271add361eb56debca16e628b013954ee2/lib/ash_hq/docs/flows/search/search.ex#L18