Triplex v0.3.1 Triplex View Source
This is the main module of Triplex.
The main objetive of it is to make a little bit easier to manage tenants through postgres db schemas or equivalents, executing queries and commands inside and outside the tenant without much boilerplate code.
Link to this section Summary
Functions
Returns all the tenants on the given repo
Creates the given tenant on the given repo
Creates the tenant schema/database on the given repo
Return the current tenant, set by put_current_tenant/1
Drops the given tenant on the given repo
Returns if the tenant exists or not on the given repo
Migrates the given tenant
Return the path for your tenant migrations
Sets the current tenant in the current process
Sets the tenant as the prefix for the changeset, schema or anything queryable
Renames the given tenant on the given repo
Returns if the given tenant is reserved or not
Returns the list of reserverd tenants
Execute the given function with the given tenant set
Link to this section Functions
Returns all the tenants on the given repo.
If the repo is not given, it uses the one you configured.
Creates the given tenant on the given repo.
Besides creating the database itself, this function also loads their
structure executing all migrations from inside
priv/YOUR_REPO/tenant_migrations
folder.
If the repo is not given, it uses the one you configured.
Creates the tenant schema/database on the given repo.
After creating it successfully, the given function callback is called with the tenant and the repo as arguments.
If the repo is not given, it uses the one you configured.
Return the current tenant, set by put_current_tenant/1
.
Drops the given tenant on the given repo.
If the repo is not given, it uses the one you configured.
Returns if the tenant exists or not on the given repo.
If the repo is not given, it uses the one you configured.
Migrates the given tenant.
If the repo is not given, it uses the one you configured.
Return the path for your tenant migrations.
If the repo is not given, it uses the one you configured.
Sets the current tenant in the current process.
Sets the tenant as the prefix for the changeset, schema or anything queryable.
Examples
defmodule User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :name, :string
end
def changeset(user, params) do
cast(user, params, [:name])
end
end
import Ecto.Query
# For the changeset
%User{}
|> User.changeset(%{name: "John"})
|> Triplex.put_tenant("my_tenant")
|> Repo.insert!()
# For the schema
User
|> Triplex.put_tenant("my_tenant")
|> Repo.all()
# For the queries
from(u in User, select: count(id))
|> Triplex.put_tenant("my_tenant")
|> Repo.all()
Renames the given tenant on the given repo.
If the repo is not given, it uses the one you configured.
Returns if the given tenant is reserved or not.
Returns the list of reserverd tenants.
By default, there are some limitations for the name of a tenant depending on the database, like “public” or anything that start with “pg_”.
You also can configure your own reserved tenant names if you want with:
config :triplex, reserved_tenants: ["www", "api", ~r/^db+$/]
Notice that you can use regexes, and they will be applied to the tenant names.
Execute the given function with the given tenant set.