how to use RethinkDB with Phoenixframework?
Asked Answered
S

1

18

Having just arrived to Elixir/Phoenix I want to use RethinkDB instead of PostgreSQL but I only find documentation/examples on PostgreSQL (which seems to be the default/official database). There is a very good package from Hamiltop (Rethinkdb-elixir) but unfortunately the documentation in the Wiki is not ready and in the Readme is not enough for me. I absolutely don't want to use SQL (I came from using Meteor/MongoDB where database was not as issue). Can anyone show me a simple example of the code I need to:

  1. Connect to RethinkDB;
  2. Start the server/manage the server/connections;
  3. Create a database/table;
  4. Perform basic CRUD operations.

This could sound silly but as Meteor took care of these for us, now this is an issue for me...because I'm not being able to do it properly.Thanks!

Smaragdite answered 16/7, 2015 at 15:5 Comment(5)
I don't think there is currently an adapter for RethinkDB, so you'd have to either write, manually write queries filling in Ecto models or stop using Ecto altogether. Anyway I think this type of question where you ask for basically a tutorial on the subject is a bad fit for SO, so I'm voting to close - please do come back with any specific problems you encounter while researching this!Stannfield
@Pawel Obrock I understand your view on this question but it sums several issues (that could raise specific questions) I had when trying to make all this work. I'm not using Ecto and I was able to solve some issues others I wasn't. I was expecting to raise more specific questions through comments in below answers...Smaragdite
I think a better approach would be to create many questions with specific problems and code samples that you attempted - that way people having those problems in the future will be better able to find and use this knowledge.Stannfield
I will take that advice in the future. Thank you!Smaragdite
@PawełObrok I understand your point. But I have to disagree. I have a very similar problem at the moment regarding Elixir/Phoenix and OrientDB and finding this question with an answer helped me a lot.Flub
H
23

Step 1) Generate project without ecto:

mix phoenix.new some_app --no-ecto

Step 2) Add rethinkdb as a dependency in mix.exs

defp deps do
  [{: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 a database:

defmodule SomeApp.Database do
  use RethinkDB.Connection
end

Step 5) Add it to your supervision tree in lib/some_app.ex - the name should match your database module above (SomeApp.Database)

def start(_type, _args) do
  import Supervisor.Spec, warn: false

  children = [
    # Start the endpoint when the application starts
    supervisor(SomeApp.Endpoint, []),
    worker(RethinkDB.Connection, [[name: SomeApp.Database, host: 'localhost', port: 28015]]) 
    # Here you could define other workers and supervisors as children
  ]

  # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
  # for other strategies and supported options
  opts = [strategy: :one_for_one, name: Rethink.Supervisor]
  Supervisor.start_link(children, opts)
end

Step 6) Execute a query:

defmodule Rethink.PageController do
  use Rethink.Web, :controller
  use RethinkDB.Query

  plug :action

  def index(conn, _params) do
    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: I have put the queries in the PageController just for the ease of running. In a real example, these would be in separate module - maybe one that represents your resource.

The other thing to note is that I am creating the table inline on the controller. You could execute the command to create a table in a file such as priv/migrations/create_people.exs and run it with mix run priv/migrations/create_people.exs

Hedelman answered 16/7, 2015 at 15:33 Comment(9)
Clear! But two questions: 1) I assume (in step 4) that the name of the new database is SomeApp. If I had created that database using RethinkDB directly it would work the same? 2) When creating a connection I have two scenarios: local database & Phoenix in Windows & RethinkDB on a Linux virtual machine. Do I need to do anything in the first scenario? (because in the second I need to do conn = RethinkDB.connect([host: "10.0.0.17", port: 28015])Smaragdite
@PauloJaneiro considering moving your host and port into config values if you wish to change them easily between environments: elixir-lang.org/docs/v1.0/mix/Mix.Config.htmlHedelman
what's the difference between (step 5) adding to the supervision tree in lib/some_app.rb file and lib/name_of_the_app.ex that is already create by default?Smaragdite
@PauloJaneiro I was using some_app because that was the name of my app. Replace some_app with whatever your app is called. ` worker(RethinkDB.Connection, [[name: SomeApp.Database, host: 'localhost', port: 28015]])` was the line I added.Hedelman
Sorry, that's not my issue (that's OK). My issue is about the file extension itself - .rb Why not .ex?Smaragdite
@PauloJaneiro Wow - good spot. Sorry - I must have been in ruby mode - that should definitely be the .ex fileHedelman
Thanks from the rethinkdb-elixir author here. Can we capture this example in the Wiki on Github?Stets
@Stets I don't have a problem with that at all. I'd just advise that you follow whatever Stack Overflow's rules are on the issue. I found this link: meta.stackexchange.com/questions/24611/…Hedelman
To update this i've open sourced an example app using the code above and updated for changes: github.com/AdamBrodzinski/phoenix-rethinkdb-exampleHalloween

© 2022 - 2024 — McMap. All rights reserved.