ArangoDB and Elm

Part 1: Arango

With so many of these great technologies out there, it is hard to find a combination that fits your needs and taste. There are plenty of safe options, but for the adventurous, a journey off the beaten path is usually more appealing.

Considering myself adventurous, Elm piqued my interest over a year ago. The mantra is usually: find the best tool for the job, but in this case I was looking for a job to fit Elm.

In a recent project of mine, we wanted to create a repository for information related to buildings, BIM (Building Information Modeling). Because items in a building have all kinds of relations, a graph database is a good option for this domain. The usual suspects are Neo4J, Virtuoso, to name a couple. Fortunately for me, there is also a new kid on the block: ArangoDB. It's easy to use, fast, and free to get started with.

Another interesting feature of ArangoDB, is that it exposes a friendly REST API, making it suitable as a backend for a simple Single Page Application (SPA). Of course, any front-end framework, like Ember, Angular or React can be hooked up to this kind of API, but I chose to find out how far I could get with my limited knowledge of Elm.

Yes, there you have it. I have only limited knowledge of Elm, and I don't understand half of the code I'm writing in it. That's bad news for you, the reader, because how can I explain something that I don't understand myself? On the other hand: it didn't keep me from trying to write software, and somehow I got a few working applications. So, I encourage you to give Elm a try. Even if you don't understand what you're writing, the Elm compiler does. Or if it doesn't, it will tell you. Often, at least in my case, I also don't understand what the compiler is trying to tell me, but with some help from Google, SO, Slack and forums, I manage to take baby steps forward.

Often, this 'help' from the compiler feels like this pesky know-it-all colleague, who is telling me that I'm stupid and doing it all wrong. It makes me want to yell: "If you know everything so well, why don't you write my freaking program?!" Of course, that's not gonna happen.

So, why go through all this pain and frustration? I guess it's the promising labels on the package: "performance" and "no runtime exceptions".

OK, after this long introduction, let's start with installing and configuring Arango and Elm. Just go thr0ugh the installation instructions on both sites. If you, like me, downloaded and installed it, you need to make the following changes. Maybe "docker run arangodb" is easier, I didn't try.

For Arango, open up an editor to expose the service to the outside world, e.g.

sudo vim /etc/arangodb/arangod.conf
endpoint = tcp://0.0.0.0:8529

I ran into CORS problems, and managed to fix these by changing the startup command in /etc/init.d/arangodb3. It's probably not the most elegant solution, but it worked for me. I added --http.trusted-origin "*" to the last section in the start function. I had to run systemctl daemon-reload and sudo service arangodb3 restart to bring the changes into effect.

Now you should be able to go to http://localhost:8529

Next step is to create a database named "test" with two collections: "assemblies" and "contains". Together they will form the graph. Import this json file into “assemblies”.

Then execute this AQL:

FOR assy IN assemblies
FOR parent IN assemblies
FILTER parent.id == assy.parent_id
INSERT {_from: parent._id, _to: assy._id} IN contains

Coming from SQL, Arango's Query Language takes some getting used to. It looks a bit like Linq and Ecto queries. Basically, you first define which collection(s) you want to query, then comes your filtering, sorting and limiting, and then you define what to return, or to process.

In the query above, I am using the implicit tree definition "parent_id" to create "contains" documents, each of which defines the relationship between two "assemblies" documents. With this in place, we can show the graph with a special query:

FOR v, e, path IN 1..3 ANY
‘assemblies/3210973’ contains
RETURN path

Which gives us this pretty picture:

assemblies graph

In the next part, I will show how to login to Arango from Elm.