Setting up SqlKorma with Postgresql on Windows 7

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.