elixir - how to use RethinkDB with Phoenixframework? -


having arrived elixir/phoenix want use rethinkdb instead of postgresql find documentation/examples on postgresql (which seems default/official database). there package hamiltop (rethinkdb-elixir) unfortunately documentation in wiki not ready , in readme not enough me. absolutely don't want use sql (i came using meteor/mongodb database not issue). can show me simple example of code need to:

  1. connect rethinkdb;
  2. start server/manage server/connections;
  3. create database/table;
  4. perform basic crud operations.

this sound silly meteor took care of these us, issue me...because i'm not being able properly.thanks!

step 1) generate project without ecto:

mix phoenix.new some_app --no-ecto 

step 2) add rethinkdb dependency in mix.exs

defp deps   [{:phoenix, "~> 0.13.1"},    {:phoenix_html, "~> 1.0"},    {:phoenix_live_reload, "~> 0.4", only: :dev},    {:rethinkdb, "~> 0.0.5"},    {:cowboy, "~> 1.0"}] end 

step 3) run mix deps.get

step 4) create database:

defmodule someapp.database   use rethinkdb.connection end 

step 5) add supervision tree in lib/some_app.ex - name should match database module above (someapp.database)

def start(_type, _args)   import supervisor.spec, warn: false    children = [     # start endpoint when application starts     supervisor(someapp.endpoint, []),     worker(rethinkdb.connection, [[name: someapp.database, host: 'localhost', port: 28015]])      # here define other workers , supervisors children   ]    # see http://elixir-lang.org/docs/stable/elixir/supervisor.html   # other strategies , supported options   opts = [strategy: :one_for_one, name: rethink.supervisor]   supervisor.start_link(children, opts) end 

step 6) execute query:

defmodule rethink.pagecontroller   use rethink.web, :controller   use rethinkdb.query    plug :action    def index(conn, _params)     table_create("people")     |> someapp.database.run     |> io.inspect      table("people")     |> insert(%{first_name: "john", last_name: "smith"})     |> someapp.database.run     |> io.inspect      table("people")     |> someapp.database.run     |> io.inspect     render conn, "index.html"   end end 

please note: have put queries in pagecontroller ease of running. in real example, these in separate module - maybe 1 represents resource.

the other thing note creating table inline on controller. execute command create table in file such priv/migrations/create_people.exs , run mix run priv/migrations/create_people.exs


Comments