Update: Hey there, I’ve been thinking a lot about this and I came up with something new! Check it out here!

There are a lot of clojure database libraries considering how small the Clojure community is. And they all have their strengths and weaknesses. One thing I do like about a few of the libraries is that you can hook up a bit of sql to a function and that’s that, no ceremony, no ORM, just data, like how grandpa used to do.

Of course, that opens up new problems, like now I have to update my sql whenever I change the schema even for writes! Killer for productivity when you’re trying to get something out the door with limited time and money like I do. So what do you do? What did I do? I made yet another Clojure sql library of course. It’s called oksql. It aims to be the best of both worlds, dynamic sql generation for writes, and static sql from a sql file for reads all with a pretty consistent API. Given a table with a schema like this

Here’s what a basic sql file looks like. It’s pretty straightforward, WARNING this requires you to know sql. The horror.

Basic sql file

Here’s what clojure looks like calling that sql file. WARNING this requires you to know clojure.

Basic sql file clojure interop

Sucks that it’s spread out over two files, or does it? I mean the technologies are separated, right? Or wait was it a separation of concerns? Who knows anymore. Here’s how you can do inserts, updates and deletes.

The CUD in CRUD

So you can imagine changing the schema and having to go back to this sql file to keep adding columns to the update and insert sql. You’re right, that sucks. Luckily I’ve already used this thing for a while and I’ve come up with an alternative.

That’s a tiny gist!

A little more complex, but it’s worth it.

And that’s it. Nothing else to it. All of the sql stuff you would do you CAN do, joins, complex where clauses, sub queries, it’s crazy.

No googling around for how do I use this ORM or what’s that weird syntax for getting an or in the where clause, or a not in the where clause. It’s just sql, ok?