Ecto Plugin — Uni

if Mix.env() == :prod do UniEcto.Plugin.set_tenant_prefix("public") end If you are searching for "uni ecto plugin," you might actually be looking for a specific adapter. Here is the landscape:

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

def call(conn, _opts) do # Extract subdomain or API key tenant = get_tenant_from_subdomain(conn) uni ecto plugin

def get_orders_with_global_settings do query = from o in Order, join: s in Setting, on: true, # Global join select: o, s Repo.all(query, prefix: UniEcto.Plugin.get_tenant_prefix()) end Handling Migrations with The Plugin The trickiest part of multi-tenancy is database schema updates. You cannot just run mix ecto.migrate .

Alternatively, use the plugin's CLI:

mix uni_ecto.gen.migration create_tenants_table This creates a migration that tracks tenants in a central tenants meta-table. Add the Plug to your router:

defmodule MyApp.Repo.Migrations.AddNotesToOrders do use Ecto.Migration import UniEcto.MigrationHelpers def up do # Runs on all existing tenants + public for tenant <- UniEcto.Plugin.all_tenants() do execute("SET search_path TO #tenant") alter table(:orders) do add :notes, :text end end end end if Mix

In the modern landscape of Software as a Service (SaaS), multi-tenancy is no longer a luxury—it’s a necessity. Whether you are building a white-label CRM, an enterprise ERP, or a simple API for startups, you need a way to isolate customer data securely.