{"id":13491567,"url":"https://github.com/ateliware/triplex","last_synced_at":"2025-05-15T12:04:26.791Z","repository":{"id":41523098,"uuid":"95725056","full_name":"ateliware/triplex","owner":"ateliware","description":"Database multitenancy for Elixir applications!","archived":false,"fork":false,"pushed_at":"2024-07-09T22:09:23.000Z","size":152,"stargazers_count":488,"open_issues_count":21,"forks_count":50,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-11T19:13:29.719Z","etag":null,"topics":["apartment","ecto","elixir","multitenant","saas","tenant"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ateliware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-29T01:10:10.000Z","updated_at":"2025-04-10T12:42:47.000Z","dependencies_parsed_at":"2024-05-01T17:21:08.376Z","dependency_job_id":"7a177958-64db-4217-99ee-a03ef5026d6d","html_url":"https://github.com/ateliware/triplex","commit_stats":{"total_commits":87,"total_committers":11,"mean_commits":7.909090909090909,"dds":"0.14942528735632188","last_synced_commit":"a08ef5166a52c85a094230c735495c22ebde1e40"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ateliware%2Ftriplex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ateliware%2Ftriplex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ateliware%2Ftriplex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ateliware%2Ftriplex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ateliware","download_url":"https://codeload.github.com/ateliware/triplex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337612,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apartment","ecto","elixir","multitenant","saas","tenant"],"created_at":"2024-07-31T19:00:58.222Z","updated_at":"2025-05-15T12:04:21.780Z","avatar_url":"https://github.com/ateliware.png","language":"Elixir","readme":"# Triplex\n\n[![Build Status](https://travis-ci.org/ateliware/triplex.svg?branch=master)](https://travis-ci.org/ateliware/triplex)\n[![Version](http://img.shields.io/hexpm/v/triplex.svg?style=flat)](https://hex.pm/packages/triplex)\n[![Downloads](https://img.shields.io/hexpm/dt/triplex.svg)](https://hex.pm/packages/triplex)\n[![Coverage Status](https://coveralls.io/repos/github/ateliware/triplex/badge.svg?branch=master)](https://coveralls.io/github/ateliware/triplex?branch=master)\n[![Code Climate](https://img.shields.io/codeclimate/github/ateliware/triplex.svg)](https://codeclimate.com/github/ateliware/triplex)\n[![Inline docs](http://inch-ci.org/github/ateliware/triplex.svg?branch=master\u0026style=flat)](http://inch-ci.org/github/ateliware/triplex)\n\nA simple and effective way to build multitenant applications on top of Ecto.\n\n[Documentation](https://hexdocs.pm/triplex/readme.html)\n\nTriplex leverages database data segregation techniques (such as [Postgres schemas](https://www.postgresql.org/docs/current/static/ddl-schemas.html)) to keep tenant-specific data separated, while allowing you to continue using the Ecto functions you are familiar with.\n\n\n\n## Quick Start\n\n1. Add `triplex` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:triplex, \"~\u003e 1.3.0\"},\n  ]\nend\n```\n\n2. Run in your shell:\n\n```bash\nmix deps.get\n```\n\n\n## Configuration\n\nConfigure the Repo you will use to execute the database commands with:\n\n    config :triplex, repo: ExampleApp.Repo\n\n### Additional configuration for MySQL\n\nIn MySQL, each tenant will have its own MySQL database.\nTriplex uses a table called `tenants` in the main Repo to keep track of the different tenants.\nGenerate the migration that will create the table by running:\n\n    mix triplex.mysql.install\n\nAnd then create the table:\n\n    mix ecto.migrate\n\nOtherwise, if you wish to skip this behavior, configure Triplex to use the default `information_schema.schemata` table:\n\n    config :triplex, tenant_table: :\"information_schema.schemata\"\n\n## Usage\n\nHere is a quick overview of what you can do with triplex!\n\n\n### Creating, renaming and dropping tenants\n\n\n#### To create a new tenant:\n\n```elixir\nTriplex.create(\"your_tenant\")\n```\n\nThis will create a new database schema and run your migrations—which may take a while depending on your application.\n\n\n#### Rename a tenant:\n\n```elixir\nTriplex.rename(\"your_tenant\", \"my_tenant\")\n```\n\nThis is not something you should need to do often. :-)\n\n\n#### Delete a tenant:\n\n```elixir\nTriplex.drop(\"my_tenant\")\n```\n\nMore information on the API can be found in [documentation](https://hexdocs.pm/triplex/Triplex.html#content).\n\n\n### Creating tenant migrations\n\nTo create a migration to run across tenant schemas:\n\n```bash\nmix triplex.gen.migration your_migration_name\n```\n\nIf migrating an existing project to use Triplex, you can move some or all of your existing migrations from `priv/YOUR_REPO/migrations` to  `priv/YOUR_REPO/tenant_migrations`.\n\nTriplex and Ecto will automatically add prefixes to standard migration functions.  If you have _custom_ SQL in your migrations, you will need to use the [`prefix`](https://hexdocs.pm/ecto/Ecto.Migration.html#prefix/0) function provided by Ecto. e.g.\n\n```elixir\ndef up do\n  execute \"CREATE INDEX name_trgm_index ON #{prefix()}.users USING gin (nam gin_trgm_ops);\"\nend\n```\n\n\n### Running tenant migrations:\n\n```bash\nmix triplex.migrate\n```\n\nThis will migrate all of your existing tenants, one by one.  In the case of failure, the next run will continue from where it stopped.\n\n\n### Using Ecto\n\nYour Ecto usage only needs the `prefix` option.  Triplex provides a helper to coerce the tenant value into the proper format, e.g.:\n\n```elixir\nRepo.all(User, prefix: Triplex.to_prefix(\"my_tenant\"))\nRepo.get!(User, 123, prefix: Triplex.to_prefix(\"my_tenant\"))\n```\n\n\n### Fetching the tenant with Plug\n\nTriplex includes configurable plugs that you can use to load the current tenant in your application.\n\nHere is an example loading the tenant from the current subdomain:\n\n```elixir\nplug Triplex.SubdomainPlug, endpoint: MyApp.Endpoint\n```\n\nFor more information, check the `Triplex.Plug` documentation for an overview of our plugs.\n\n\n## Thanks\n\nThis lib is inspired by the gem [apartment](https://github.com/influitive/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](https://github.com/Dania02525/apartmentex).  A lot of the work here is based on what she has done there.  And also to @jeffdeville, who forked ([tenantex](https://github.com/jeffdeville/tenantex)) taking a different approach, which gave us additional ideas.\n","funding_links":[],"categories":["Elixir","ORM and Datamapping","\u003ca name=\"Elixir\"\u003e\u003c/a\u003eElixir"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fateliware%2Ftriplex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fateliware%2Ftriplex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fateliware%2Ftriplex/lists"}