Search⌘ K
AI Features

Integrating a Function to Persist Records

Explore how to incorporate persistence functions in an Elixir OTP project by modifying the API layer and boundary modules. Learn to design flexible persistence strategies that maintain backward compatibility and configure persistence dynamically, enabling you to save responses to databases or files effectively.

For this feature, let’s go from the outside in, addressing the outer API layer first. That way, we can think about the shape of our external APIs.

Pulling a persistence function from the environment

In /lib/mastery.ex, we need to pull a persistence function from the environment to dictate the persistence mechanism, like this:

C++
@persistence_fn Application.get_env(:mastery, :persistence_fn)

That was easy enough. We pass in another argument, a function to optionally persist each new response. The responsibility of the function from Mastery’s perspective is to return a ...