Triplex v0.3.3 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

Return the current tenant, set by put_current_tenant/1

Sets the current tenant in the current process

Sets the tenant as the prefix for the changeset, schema or anything queryable

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

Link to this function all(repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

Returns all the tenants on the given repo.

If the repo is not given, it uses the one you configured.

Link to this function create(tenant, repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

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.

Link to this function create_schema(tenant, repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo(), func \\ nil) View Source

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.

Link to this function drop(tenant, repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

Drops the given tenant on the given repo.

If the repo is not given, it uses the one you configured.

Link to this function exists?(tenant, repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

Returns if the tenant exists or not on the given repo.

If the repo is not given, it uses the one you configured.

Link to this function migrate(tenant, repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

Migrates the given tenant.

If the repo is not given, it uses the one you configured.

Link to this function migrations_path(repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

Return the path for your tenant migrations.

If the repo is not given, it uses the one you configured.

Link to this function put_current_tenant(value) View Source

Sets the current tenant in the current process.

Link to this function put_tenant(prefixable, map) View Source

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()
Link to this function rename(old_tenant, new_tenant, repo \\ %{__struct__: Triplex.Config, repo: nil, reserved_tenants: [], tenant_field: :id}.repo()) View Source

Renames the given tenant on the given repo.

If the repo is not given, it uses the one you configured.

Link to this function reserved_tenant?(tenant) View Source

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.

Link to this function with_tenant(tenant, func) View Source

Execute the given function with the given tenant set.