Using RavenDB in Clojure, part one.
For one reason and another I was interested in using RavenDB in a clojure project I was working on. I've used RavenDB for a while and had a bit of experience with both the .NET client and the HTTP api.
While there is currently a Java RavenDB client I didnt want to use this for two reasons.
- This is about improving my knowledge of clojure
- I would like to compile the client as clojurescript eventually
So, I figured it would be fun to create a clojure client that didn't just wrap the Java client but instead consumed the HTTP api and I started building clj-ravendb.
The source is here if you want to have a look.
Getting Started
I'm assuming a familiarity with clojure and leiningen but if not check out the getting started guides.
Ok, so, lets create a new clojure project.
lein new ravendbtest
Then we can cd into the project.
cd ravendbtest
I'm using vim but feel free to use whatever makes you happy.
Lets add clj-ravendb to our new project.
vim project.clj
At the time of this post the most recent version of clj-ravendb is 0.4.0. Modify project.clj to add the dependency.
Note. The most recent version of clj-ravendb is currently:
Then run ```lein compile :all``` to compile and download the project dependencies.
Now we've created a project and referenced clj-ravendb we can try it out.
First, while we are still cd'd into the ravendbtest directory lets start a clojure REPL. I am using vim-fireplace to send expressions to the REPL from vim but you can type directly into the REPL if you wish.
lein repl
Putting Documents
The first thing we need to do is require the ravendb client.
Note. The rest of the post will assume there is an instance of RavenDB running at http://localhost:8080 and that the instance contains the sample northwind database.
The sample northwind database is available here.
Next we create a client we can use to consume the northwind database.
You can have a peek at the client by evaluating it in the REPL, type ```northwind``` and hit enter. You will see the client is represented by a map.
Now that we have a client we can start using it, type the following into the REPL to create a new document.
When we put a new document we need to give it a key, which is represented with a string and the actual document, which we represent with a map.
This should return a map that contains a HTTP status, in this case we should get a ```{:status 200}``` indicating the resource was created.
Loading Documents
Lets try getting the document we just created back out.
When we load documents we need to provide a sequence of document ids to load. In this case we just load one, "Orders/10000".
This should give us back the following.
Note that we get a ```:status``` that represents the HTTP status code and we get some ```:results```. The ```:results``` are a sequence that contain all the documents we tried to load.
In the next post I'll cover creating and querying indexes and then we will talk about how the client makes use of the replication functionality provided by RavenDB.
Go to part 2