...

/

Integrating a Function to Persist Records

Integrating a Function to Persist Records

Let’s learn to write a function that we can optionally use to save records to a database or file.

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:

Press + to interact
@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 response. ...