Getting started using RethinkDB with Phoenix (rethinkdb_ecto)

Phoenix is Elixir's main web framework.
But the officially supported databases do not include RethinkDB.

Here is a guide to making a new Phoenix project that uses RethinkDB as its database.

WARNING: rethinkdb_ecto support for Ecto 2.0 is not finished, everything might break at any point.

From 0 to a Phoenix app

I assume you have Elixir and mix installed, if not, follow this tutorial.

First we'll install the latest version of the Phoenix plugin for mix:

mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Then we create a new Phoenix app:

mix phoenix.new rethinkdb_phoenix

answer yes to Fetch and install dependencies? [Yn] n. This will install node on your machine, if you haven't already. If you want to avoid that, pass --no-brunch to phoenix.new.

So now you have a new Phoenix project that uses Postgres.

From a Phoenix app to a RethinkDB Phoenix app

Let's get rid of Postgres first:

  • remove {:postgrex, "~> 0.6.0"} from deps in mix.exs
  • remove :postgrex from application in mix.exs

Then let's add rethinkdb_ecto to the project:

Add this to deps in mix.exs:

{:rethinkdb_ecto, github: "almightycouch/rethinkdb_ecto"}

it's important to use the github version instead of the hex version, because the hex version is still on ecto 1.0.

Add to application in mix.exs:

:rethinkdb_ecto

Try if the deps are working with:

$ mix do deps.get, compile

Your mix.exs should look similar to this:

  def application do
    [mod: {RdbPhoenix, []},
     applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext, :phoenix_ecto, :rethinkdb_ecto]]
  end

  defp deps do
    [{:phoenix, "~> 1.2.1"},
     {:phoenix_pubsub, "~> 1.0"},
     {:phoenix_ecto, "~> 3.0"},
     {:phoenix_html, "~> 2.6"},
     {:phoenix_live_reload, "~> 1.0", only: :dev},
     {:gettext, "~> 0.11"},
     {:cowboy, "~> 1.0"},
     {:rethinkdb_ecto, github: "almightycouch/rethinkdb_ecto"}
    ]
  end

Now let's complete the configuration.

Under config/ modify the database configuration in the same way in test.exs, prod.secret.exs and dev.exs:

config :rdb_phoenix, RdbPhoenix.Repo,
  adapter: RethinkDB.Ecto,
  database: "test",
  pool_size: 10,
  hostname: "localhost",
  port: 28015

Note that you should change hostname and port according to your db url, but the database name can only be "test" for the time being.

Once you've completed that, it's time to try for real:

$ mix phoenix.server

and Phoenix should start.

No guarantees on anything else working, but so far it worked for me :)

The code is here.

References