Multiple Databases in Ecto

I have an application that is designed for multiple databases, it looks like an application that monitors applications that report, notify about alerts, run tasks and materials in such a way that it is impossible to control statically (in the configuration file), so I thought this could be done by starting a supervisor that controls several Repo by simply changing their settings in opts (I thought in the repository pool, something like this sounds crazy, I know), but this is not possible, since Ecto.Repo has macro with which binds the connection to the module Repo , so that there is a way to rewrite Ecto.Repo so that I could meet their needs? I looked at the sources of ecto, but I did not find a solution digging around alone.

I also looked at this post , but there is a chance that the application will target multiple instances, with different users, different credentials, etc.

I came up with some solutions using Code.eval to create Repos on the fly, but I don’t know how good it is and if it is encouraged.

+7
elixir ecto
source share
2 answers

There is a library called apartmentex to support several tenants in Phoenix and Ecto https://github.com/Dania02525/apartmentex

0
source share

If you know all the repositories you need to access, I suggest breaking the application into more umbrella applications. Each umbrella application can connect to a different ecto repository.

As a good practice, I always extract Ecto and database-related code to separate domain-based umbrella applications. In this demo application, you can see one example: https://github.com/silviurosu/elixir-umbrella-ddd .

0
source share

All Articles