Skip to content

Setup A RethinkDB Elixir Project

Nick edited this page May 15, 2015 · 5 revisions

Use The Mix Utility

Elixir, Like other languages, can be used interactively from a shell environment, or with scripts in a similar fashion to Python, Ruby, and others. However, it's most often used in a complete project using the "Mix" utility. It's important to be familiar with using Mix, as it will be the foundation of the majority of your Elixir projects.

In a nutshell, Mix is a build tool that provides tasks for creating, compiling, testing, handling dependencies, and more.

An Example Project

Lets create an example project that gets us ready to use the ExrethinkDB driver. Naturally, you need to have Erlang, and Elixir installed and working properly before proceeding. Keep in mind that the exact version number of the libraries used in these examples may vary from the most current versions. This guide also assumes that you are using a *nix based operating system like Ubuntu Linux.

  • Open a terminal window (ctrl + alt + t).

  • Create a directory for your project:

    $mkdir example1

  • Change into the project directory:

    $cd example1/

  • Generate the project with Mix:

example1$ mix new ex1 --module EX1
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/ex1.ex
* creating test
* creating test/test_helper.exs
* creating test/ex1_test.exs

Your mix project was created successfully.
You can use mix to compile it, test it, and more:

    cd ex1
    mix test

Run `mix help` for more commands.

  • As noted at the end of the generated project console log, you should change into the project directory, and run the mix text command just to verify that the project is setup and working properly.
$ cd ex1
$ mix test
Compiled lib/ex1.ex
Generated ex1 app
.

Finished in 0.03 seconds (0.03s on load, 0.00s on tests)
1 tests, 0 failures

Randomized with seed 216553

Note: If everything is working properly, you should have similar results.

  • Edit the mix.exs file so that it includes the RethinkDB-Elixir driver as project dependency. This can be done with any text editor you have available. Once you have the file open in the text editor, add the following line in the "Dependencies" section, and save the file.
  defp deps do
    [
     {:rethinkdb, github: "hamiltop/rethinkdb-elixir"}
    ]
  end
  • Now we are ready to use the Mix utility to get the RethinkDB-Elixir driver, and get it ready for use in the project. From the project directory execute the following commands:
  • Use Mix to check for dependencies.
example1/ex1$ mix deps
* rethinkdb (git://github.com/hamiltop/rethinkdb-elixir.git)
  the dependency is not available, run `mix deps.get`

  • You will notice that the rethinkDB-elixir driver dependency is not not available, so lets get it:
example1/ex1$ mix deps.get
* Getting rethinkdb (git://github.com/hamiltop/rethinkdb-elixir.git)
Cloning into '/home/youraccount/example1/ex1/deps/rethinkdb'...
remote: Counting objects: 484, done.        
remote: Compressing objects: 100% (132/132), done.        
remote: Total 484 (delta 69), reused 0 (delta 0), pack-reused 352        
Receiving objects: 100% (484/484), 98.32 KiB | 0 bytes/s, done.
Resolving deltas: 100% (224/224), done.
Checking connectivity... done.
Running dependency resolution
Dependency resolution completed successfully
  poison: v1.4.0
* Getting poison (Hex package)
Checking package (https://s3.amazonaws.com/s3.hex.pm/tarballs/poison-1.4.0.tar)
Using locally cached package
Unpacked package tarball (/home/youraccount/.hex/packages/poison-1.4.0.tar)
  • When we execute the "$mix deps" command again we notice that the dependency build is outdated.
 example1/ex1$ mix deps
* poison (Hex package)
  locked at 1.4.0 (poison)
  the dependency build is outdated, please run `mix deps.compile`
* rethinkdb (git://github.com/hamiltop/rethinkdb-elixir.git)
  locked at df14543
  the dependency build is outdated, please run `mix deps.compile`
  • To resolve the outdated build issue, all we need to do is follow the instructions at the end of the last commands output. You will notice that the next command in this process is usually there to guide us; a nice touch from the great people working on Elixir. Another nice touch, is that fact that Mix is also resolving the poison dependency that's required by RethinkDB-Elixir with out our intervention.
example1/ex1$ mix deps.compile
==> poison
Compiled lib/poison.ex
Compiled lib/poison/parser.ex
Compiled lib/poison/decoder.ex
Compiled lib/poison/encoder.ex
Generated poison app
==> rethinkdb
Compiled lib/rethinkdb/lambda.ex
Compiled lib/rethinkdb.ex
Compiled lib/rethinkdb/pseudotypes.ex
Compiled lib/rethinkdb/connection.ex
Compiled lib/rethinkdb/query/macros.ex
Compiled lib/rethinkdb/prepare.ex
Compiled lib/rethinkdb/query/string_manipulation.ex
Compiled lib/rethinkdb/query/database.ex
Compiled lib/rethinkdb/query/joins.ex
Compiled lib/rethinkdb/query/writing_data.ex
Compiled lib/rethinkdb/query/control_structures.ex
Compiled lib/rethinkdb/query.ex
Compiled lib/rethinkdb/response.ex
Compiled lib/rethinkdb/query/aggregation.ex
Compiled lib/rethinkdb/query/math_logic.ex
Generated rethinkdb app
  • When we execute the mix deps command again, we find that RethinkDB-Elixir and Poison are ready to go.
example1/ex1$ mix deps
* poison 1.4.0 (Hex package)
  locked at 1.4.0 (poison)
  ok
* rethinkdb 0.0.5 (git://github.com/hamiltop/rethinkdb-elixir.git)
  locked at df14543
  ok

Updating ExrethinkDB and other dependancies.

  • As RethinkDB-Elixir, and other libraries evolve they need to be updated to the newest version. One way to do this is as follows:
example1/ex1$ mix deps.update --all
* Updating rethinkdb (git://github.com/hamiltop/rethinkdb-elixir.git)
Running dependency resolution
Dependency resolution completed successfully
  poison: v1.4.0
example1/ex1$ mix deps
* poison 1.4.0 (Hex package)
  locked at 1.4.0 (poison)
  ok
* rethinkdb (git://github.com/hamiltop/rethinkdb-elixir.git)
  locked at df14543
  the dependency build is outdated, please run `mix deps.compile`
example1/ex1$ mix deps.compile
example1/ex1$ mix deps
* poison 1.4.0 (Hex package)
  locked at 1.4.0 (poison)
  ok
* rethinkdb 0.0.5 (git://github.com/hamiltop/rethinkdb-elixir.git)
  locked at df14543
  ok
  • The final mix deps command indicates that everything is up to date and ready to use.

Compiling the example module, EX1, from the Elixir interactive shell.

  • You can compile the example module from the interactive shell $iex with these simple commands.

  • From the project directory start the interactive shell:

example1/ex1$ iex
Erlang/OTP 17 [erts-6.4] [source-2e19e2f] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 
  • Once $iex is up and running, you can compile the module.
iex(1)> c "ex1.ex","lib"
[EX1]
  • If we examine the compilation command we can break it down into it's individual components:
  • The "c" in the above command stands for compile.
  • "ex1.ex" is the file name of the module that was auto generated by the "Mix" command.
  • "lib" is the directory that the module file is located at in the project.
  • You can start adding your code to the ex1.ex file. Just remember to re-compile the module after each change.

Next Steps