Execute Non-Database Operations with Multi
Explore how to use the Multi.run function in Ecto to execute any Elixir code as part of a transaction, including non-database operations and unsupported database tasks. Understand how to add custom functions within a transaction and inspect Multi operations to simplify testing and optimize performance.
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.
We ...