uni ecto plugin

Plugin — Uni Ecto

defp deps do [ :ecto_sql, "~> 3.0", :uni_ecto_plugin, "~> 0.5.0", # Hypothetical version :postgrex, ">= 0.0.0" ] end Run mix deps.get . The plugin requires you to use its TenantRepo behaviour. Modify your lib/my_app/repo.ex :

The uni_ecto_plugin provides a migration helper: uni ecto plugin

def create_new_tenant(subdomain, company_name) do # 1. Create schema in DB prefix = "tenant_#subdomain" Ecto.Adapters.SQL.query!(MyApp.Repo, "CREATE SCHEMA IF NOT EXISTS #prefix") UniEcto.Plugin.run_migrations_for(prefix) 3. Insert tenant record into public tenants table %Tenantprefix: prefix, name: company_name |> Repo.insert!() # Runs in 'public' schema defp deps do [ :ecto_sql, "~> 3

Unlike basic foreign key scoping ( WHERE tenant_id = ? ), the uni_ecto_plugin often supports (separate schemas or separate databases). It seamlessly switches between tenants at the connection level. Create schema in DB prefix = "tenant_#subdomain" Ecto

:ok, prefix end The uni_ecto_plugin often bundles a caching mechanism. Setting a tenant prefix usually involves a database lookup to validate the tenant exists. This adds latency.