SqlKorma is a really nice Clojure DSL for handling things like selects, inserts, but not deletes. Ok, so that’s a lie, it can do deletes too. What would be really useful is if I had a project already setup for all to view. Maybe even something up on github. This is totally not a link to it.
For this walk through I used the lobos_example project as a start since it already had the table creation stuff in it. (This is the walkthrough for Lobos, which is also a walk through for setting up Postgresql.)
First off, if you are using the lobos_example project (if not, this post will walk through the important parts in the komra_example project), there is test that needs to be shanked. Â It’s in the test/lobo_example/core_test.clj file:
Also get rid of the old database setting “open-global” in the lobos/config.clj file:
The next step is to add the dependency for SqlKorma to the project.clj file in the project root folder. Â Depending on how old this post is, the version may not be correct.
Now it’s time to create the entities that will match the already existing tables. Â If you’ve never used an ORM before, this is simply spelling out to SqlKorma what the tables are, their columns, and their relationships. Â However, this is not done with any SQL statements. Â In fact, the point of this file, and SqlKorma in general, is to create a high level representation of the persistence layer. Â Whoa, that got serious fast. Â Basically, you are using class like entities so that you can easily query, or persist stuff, without caring about anything database wise.
So anywho, here is the entity file:
A quick note to those new to Clojure, like uuuuhhh me, :use statements can be consolidated to the root namespace. Â So instead of:
[korma.core]
[korma.db]
You can do this:
One other note is the example of a relationship. Â The symbol “belongs-to” is needed if one entity is owned by another. This being a many to one relationship between many posts, and one user:
One file to make. Â Basically it’s a test file that is run to make sure that an insert, select, and delete work:
Nothing all that complicated. Â As you can see, there is an insert, assertion (is (empty?….), and a delete.
One other note for those like me who are just picking up Clojure, there are two :use/:require keywords of note in this other note…note. Â If you look at the :require block you’ll see the :as keyword. Â This is basically an alias to help with keeping typing down. Â There might be other reasons, but I’m not the one to ask. Â So don’t. Â Not kidding.
The second keyword is the : only keyword (So annoying that I can’t put the : near the o since it makes a :o).  This allows you to only use the parts of the namespace you need.  It wasn’t necessary in this project example, but I threw it in anyhow.  So live with it.
Ok so now everything is ready (Pretty easy huh?). Â All that’s left is to prove that I am awesome, and now you have a small bit of said awesome. Â Just open a command prompt (windows key; type in cmd; hit enter), and navigate to the project root directory. Â Then type lein test.
OH EM GEE FAILURE!!! Â That’s right, you just failed to win. Â Or won by failing. Â Or something actually clever.
As you can see, the reason it failed is that I had the test asserting that the select won’t bring back anything. Â However, since I had the insert command, it brought back one user. Â And that’s a working SqlKorma example.