seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
768 stars 90 forks source link

When working with plan, it's common to want to just select some columns #139

Closed seancorfield closed 4 years ago

seancorfield commented 4 years ago

Having used plan fairly extensively in production code, I see two common patterns emerging:

Both of these are ways to avoid realizing result sets, but they require uglier code than just using execute-one! or execute!.

;; extract a column from the first row (could apply an eager function to that first row):
(reduce (fn [_ row] (reduced (col row))) nil (jdbc/plan ...))
;; extract a subset of each row:
(into [] (map #(select-keys % cols)) (jdbc/plan ...))
;; alternative (to produce vectors instead of maps):
(into [] (map (juxt cols)) (jdbc/plan ...))

Proposal:

Add next.jdbc.plan namespace, containing select-one! and select!:

(defn select-one! [connectable col sql-params opts] ...)
(defn select! [connectable cols-or-selector sql-params opts] ...)

In the latter, cols-or-selector could be a vector or a function-like thing. Passing a vector would be equivalent to passing #(select-keys % cols).