sql

2025-02-28T17:51:43.962589Z

Say I wanted to convince my coworker that we should move parts of our app's functionality to clojure. Our app is a classic Django monolith. Now, one of the first things he would ask me would probably be: I see there's no ORM in there. So do we have to write raw SQL? That's never going to work! What would you answer?

isak 2025-03-05T00:12:36.439389Z

Here is a library to check out if you are into ORMs: https://github.com/camsaul/toucan2 This might be the closest we have in Clojure.

lukasz 2025-02-28T18:03:08.908349Z

ORMs and Clojure are not a great fit, since you don't really deal with objects-as-db-entities - I migrated a pretty sizable Rails app to Clojure in the past. Most folks use HoneySQL (SQL-as-data) or HugSQL (just raw SQL with convenience features). The closes thing you find to an ORM is Toucan2 from the Metabase team

seancorfield 2025-02-28T18:24:28.410179Z

And bear in mind that next.jdbc offers an API where you can just (sql/insert! db :table hash-map) where that hash map is your row of data. Similarly, (sql/find-by-keys db :table example-hash-map) returns a vector of hash maps, that match equality on all the keys/values in the example hash map.

seancorfield 2025-02-28T18:24:56.135689Z

There's also (sql/get-by-id db :table pk) to get an individual row as a hash map.

seancorfield 2025-02-28T18:26:09.411589Z

Relations are not handled -- since it's generally better to craft an explicit join (with HoneySQL, for example) than let some magic library mistakenly perform N+1 queries to get your data as "objects"...

➕ 2
2025-02-28T19:17:56.575569Z

Cool, thanks, that makes a lot of sense :)

valerauko 2025-03-01T03:39:31.803839Z

I've had the "pleasure" of working with rails and as soon as you're dealing with nontrivial amount of data the overhead of the "object mapping" part becomes prohibitive and we'd need to .pluck plain data. Still writing the query with arel, but at that point it's not significantly different from for example honey.sql's helper function syntax

➕ 1