README.md

Triplex

Triplex leverages database data segregation techniques (such as Postgres schemas) to keep tenant-specific data separated, while allowing you to continue using the Ecto functions you are familiar with.

Quick Start

Add triplex to your list of dependencies in mix.exs:

defdepsdo
[
{:triplex, "~> 1.1.5"},
]
end

Run in your shell:

mix deps.get

Configuration

Configure the Repo you will use to execute the database commands with:

config :triplex, repo: ExampleApp.Repo

Additional configuration for MySQL

In MySQL, each tenant will have its own MySQL database.
Triplex uses a table called tenants in the main Repo to keep track of the different tenants.
Generate the migration that will create the table by running:

mix triplex.mysql.install

And then create the table:

mix ecto.migrate

Usage

Here is a quick overview of what you can do with triplex!

Creating, renaming and droping tenants

To create a new tenant:

Triplex.create("your_tenant")

This will create a new database schema and run your migrations—which may take a while depending on your application.

Fetching the tenant with Plug

Triplex includes configurable plugs that you can use to load the current tenant in your application.

Here is an example loading the tenant from the current subdomain:

plug Triplex.SubdomainPlug, endpoint:MyApp.Endpoint

For more information, check the Triplex.Plug documentation for an overview of our plugs.

Thanks

This lib is inspired by the gem apartment, which does the same thing in Ruby on Rails world. We also give credit (and a lot of thanks) to @Dania02525 for the work on apartmentex. A lot of the work here is based on what she has done there. And also to @jeffdeville, who forked (tenantex) taking a different approach, which gave us additional ideas.