Using AshSqlite as "embedded" database

I am learning AshSqlite and thought there is value in writing a tutorial, where one goes end-to-end in creating the .sqlite, load some data, define the Resources, and finally packaging the application (let’s call it Facts) as a dependency for other applications (let’s call it Consumer). The last step is where I am stuck. (These are probably general Elixir-Ecto questions.)

In Facts, the created sqlite lives inside priv/ as ./priv/facts.sqlite. It is made available to Exqlite inside config.exs. This works fine while developing Facts.

However, when Facts is used as a dependency, its configuration is not brought in.


Question 1: is there a different place inside Facts where the database connections can be configured? Ideally libraries using Facts would be completely oblivious to how the function calls return values, and that it is included the same {:facts, "~> 1.0"} as any other deps.


To get around this temporarily, I pasted the configuration from Facts into Consumer, which I thought was the wrong move. But obviously ./priv/facts.sqlite isn’t available now. So I thought I could point to the database with

"#{:code.priv_dir(:facts)}/priv/facts.sqlite"`

but this doesn’t work because config happens before compile and :code.priv_dir/1 always returns {:error, "bad name"}.


Question 2: how could one point to the innards of a dependency inside config?

1 Like

Question 1

I believe you can put this kind of information in the init/2 callback of the repo module in question:

https://hexdocs.pm/ecto/Ecto.Repo.html#c:init/2

Question 2

This is a good question, and I’m not actually sure that you can? As you said, :code.priv_dir(:facts) doesn’t work in that context. Do you have that same experience if its in runtime.exs?