Execute Non-Database Operations with Multi
Learn how to execute non-database operations with Multi.
We'll cover the following...
We'll cover the following...
Using functions with Multi
Based on what we’ve seen of Multi so far, it might appear that executing transaction with functions has one clear advantage—functions allow us to run any Elixir code within the transaction. Recall our earlier example of updating a search engine within a transaction call. Fortunately, Multi offers this functionality as well. The run function allows us to add any named or anonymous function as part of the Multi. Here’s how we might add the search engine update logic we talked about earlier in this chapter.
alias Ecto.Multiartist = %Artist{name: "Toshiko Akiyoshi"}multi =Multi.new()\|> Multi.insert(:artist, artist)\|> Multi.insert(:log, Log.changeset_for_insert(artist))\|> Multi.run(:search, fn _repo, changes ->SearchEngine.update(changes[:artist])end)Repo.transaction(multi)
We ...