Toucan library for database access


#1

Has someone used this library https://github.com/metabase/toucan seems pretty coold and DRY and has been used a lot in metabase which is a great tool I’ve used in production and uses this ORM, would like to have comments of people that have used this library and what are the pros and cons of it?


#2

I’ve been using it for a couple of months in a greenfield web application project. I like how easy it makes the basic database operations – in a previous project, I used HugSQL and writing database queries by hand for the basic CRUD stuff gets old quickly. I’ve found some use for the hooks as well.

As a downside, it assumes that you want to have one global connection to the database, which does match super-well with e.g. how you’re supposed to use Integrant (but there’s a workaround). I haven’t used hydration much and frankly it feels a bit too magical for my taste (how does it find the right hydration functions?), even though I think that it is a very useful feature.

As the bottom-line, I use it and recommend it if you’re okay with dealing with the global state and all the import magic.


#3

@Miikka, can you hook in to the model definition to have it deal with snake to kebab case for keys both ways? I’m using honeysql and JDBC, and this adds a bunch of boilerplate.


#4

I’m not 100% sure I understand what you’re asking, but my JDBC connection is configured with the following functions (copied from metosin-common)

(require '[clojure.string :as string])

(defn entities [x]
  (string/replace x #"-" "_"))

(defn identifiers [x]
  (-> x
      (string/lower-case)
      (string/replace #"_" "-")))

(toucan.db/set-default-db-connection!
  {:jdbc-url "jdbc:postgresql://..."
   ... ;; the usual JDBC stuff
   :entities entities
   :identifiers identifiers})

(toucan.db/set-default-automatically-convert-dashes-and-underscores! true)  

With this configuration, I have snake_case column names in my database and in Clojure I refer to them with kebab-case. Works with both Toucan and HoneySQL (on which Toucan is built). If you’re using plain HoneySQL with clojure.java.jdbc, you can still use :entities and :identifiers in the DB connection map.


#5

Ah that’s what I was looking for, missed it in the toucan docs, thanks.