Posted Dec 4, 2011 2011-12-04T19:02:00+01:00 by Paulo Suzart
Korma is a clojure framework (by @ibdknox) that provides great SQL abstractions. You can work purely with clojure code without inserting SQL strings into your code.
Consider the following entities:
email is the representation of the email table in a mysql database. Note that you don’t need to specify any fields. It is optional. In this case, both entities will return the column’s name as map keys in a given select. Let’s see:
The result look like a java Resultset. You can choose the original column names to pass around your application. And since clojure is not type safe or compiled, you may find yourself suffering in cases of database refactories.
A nice solution is to combine Korma entities with transform function and clojure defrecord to get rid of this problem, as well as diminish the surface contact of your code concerned with database access. One solution we (me and @rafaelfelini) have found was:
The transform function takes another function that transforms a database record (with the column names as keys) into a clojure’s record. In this case Person is the record - that always holds the e-mail. Korma takes care of the join, assuming that the email table has a column called person_id.
Now your code is a bit more resilient to database changes with this thing layer of transformation. Actually a good practice you’ve done all life as developer.
Korma can do a lot of things to make your life easier, as the abililty to compose queries, and performs really nice. Think I finally found THE framework for relational data access in clojure.