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

Using the tenant

This module has useful functions to manage your tenants, like create/1, rename/2 and drop/1, but if you’re trying to apply the tenant to a query, changeset or schema, stay with your application Repo, sending the prefix. Like this:

Repo.all(User, prefix: Triplex.to_prefix("my_tenant"))

It’s a good idea to call Triplex.to_prefix on your tenant name, altough is not required. Because, if you configured a tenant_prefix, this function will return the prefixed one.

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

This function has been deprecated

Renames the given tenant on the given repo

Returns if the given tenant is reserved or not

Returns the list of reserverd tenants

Returns the value of the configured tenant field on the given map

Returns the tenant name with the given prefix

Execute the given function with the given tenant set

Link to this section Functions

Link to this function all(repo \\ config().repo()) View Source

Returns all the tenants on the given repo.

If the given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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

Link to this function create(tenant, repo \\ config().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 given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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

Link to this function create_schema(tenant, repo \\ config().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 given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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 \\ config().repo()) View Source

Drops the given tenant on the given repo.

If the given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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

Link to this function exists?(tenant, repo \\ config().repo()) View Source

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

If the given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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

Link to this function migrate(tenant, repo \\ config().repo()) View Source

Migrates the given tenant.

If the given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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

Link to this function migrations_path(repo \\ config().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

This function has been deprecated.

Use the prefix key in your Repo calls opts instead.

For example, instead of:

User |> Triplex.put_tenant("my_tenant") |> Repo.all()

User:

Repo.all(User, prefix: "my_tenant")

Don’t forget to apply the Triplex.to_prefix/1 to transform your tenant to a prefix if you configured a tenant_prefix on your triplex configuration.

By default, you will not need it.

Link to this function rename(old_tenant, new_tenant, repo \\ config().repo()) View Source

Renames the given tenant on the given repo.

If any given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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.

If the given tenant is a map, it will apply tenant_field/1 to it and get the prefix from the field.

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.

Returns the value of the configured tenant field on the given map.

Link to this function to_prefix(tenant, prefix \\ config().tenant_prefix()) View Source

Returns the tenant name with the given prefix.

If the prefix is omitted, the tenant_prefix configuration will be used.

The tenant can be a string, a map or a struct. For a string it will be used as the tenant name to concat the prefix. For a map or a struct, it will get the tenant_field/0 from it to concat the prefix.

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

Execute the given function with the given tenant set.