# Triplex

[![Build Status](](
[![Hex pm](](
[![Coverage Status](](
[![Code Climate](](
[![Inline docs](](

An [apartment]( for succesfull Phoenix

## Installation

The package can be installed as:

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

def deps do
  [{:triplex, "~> 1.1.3"}]

2. Run in your shell:

mix deps.get

## Configuration

All you need to do in your project to start using it is to configure the Repo
you will use to execute the database commands with:

    config :triplex, repo: ExampleApp.Repo

## Usage

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

### Creating, renaming and droping tenants

To create a new tenant:


PS.: this will run the migrations, so it's a function that takes a while to
complete depending on how much migrations you have.

If you change your mind and want to rename te tenant:

Triplex.rename("your_tenant", "my_tenant")

PS.: we encourage you to use an unchangable thing as your tenant name, that
way you will not need to rename your tenant when changing the field.

And if you're sick of it and want to drop:


More information on `Triplex` module documentation.

### Creating tables inside tenant

To create a table inside your tenant you can use:

mix triplex.gen.migration your_migration_name

Also, you can move a normally generated migrations from inside
`priv/YOUR_REPO/migrations` to the `priv/YOUR_REPO/tenant_migrations`

The schemas look the same way, nothing to change.

To run the tenant migrations, just run:

mix triplex.migrate

This task will migrate all your existent tenants, one by one. If it
fail in any tenant, the next time you run it will continue from where
it stoped.

If you need more information, check the `Mix.Triplex` documentation, where
you can find the list of tasks and their descriptions.

### Querying, updating and inserting data inside tenants

To make queries, updates and inserts inside your tenant is quite easy.
Just send a `prefix` opt on your repo call with your current tenant name.
Like this:

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

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

### Loading the current tenant to your `Plug.Conn`

We have some basic and configurable plugs you can use to load the current
tenant on your web app. Here is an example loading it from the subdomain:

plug Triplex.SubdomainPlug, endpoint: MyApp.Endpoint

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