Multiple databases in Ecto
Asked Answered
D

2

9

I have an application which targets multiple databases, it's like an application that watch applications which make reports, mediate notifications, run tasks and stuffs, in a way that is impossible to be controlled statically(in the configuration file), so i thought that it could be done starting a supervisor that supervises multiple Repos just changing its settings in the opts(i was thinking in a pool of repos, something like this, sounds crazy, i know), but it's impossible since Ecto.Repo has a macro which makes connection bound to the Repo's module, so, there's a way to rewrite the Ecto.Repo in a way that i can achieve my needs? I looked at ecto's sources, but i've found no solution digging alone.

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

I've reached some solutions using Code.eval for create Repos on-the-fly, but i don't know how "good" is it, and if it's encouraged.

Dowager answered 22/8, 2016 at 2:13 Comment(0)
A
1

In case you know all the repositories you need to access I suggest splitting the application in more umbrella apps. Each umbrella app can connect to a different ecto repository.

As a good practice I always extract Ecto and database related code to separate umbrella applications separated by domains. You can see one example in this demo application: https://github.com/silviurosu/elixir-umbrella-ddd.

Audio answered 27/8, 2017 at 16:59 Comment(0)
B
0

There's a library called apartmentex for multi tenant support in Phoenix and Ecto https://github.com/Dania02525/apartmentex

Besprent answered 11/10, 2016 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.