Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of how to do query.sql code reload #72

claudiu-apetrei opened this issue Aug 10, 2017 · 4 comments


None yet
4 participants
Copy link

commented Aug 10, 2017


Hugsql seems amazing.
Pretty new to clojure, and just can't seem to find any resource out there on how to reload the query.sql.

I am using component for my project and have core reload with '' with set-refresh-dirs.

Guessing that the workflow can't be restart repl.
Would be really helpful if you could share a example of a reload flow Thank you :)


This comment has been minimized.

Copy link

commented Aug 14, 2017

Here's the solution I came up with. It's pretty dirty, but seems to work ok. I'd love to know of a better way though.

  (:require [ :as hugsql]
            [com.stuartsierra.component :as component]
            [clojure.set :as set]))

(def this-ns *ns*)

(def queries-file "queries/app.sql")

;; DB fns need to be defined twice, once here to ensure they exist when refreshing
;; the namespace.
(hugsql/def-db-fns queries-file)

;; And once here as a component, so that changes to the file will still be picked up.
(defrecord PostgresQueries [resource-path]
  (start [this]
    (hugsql/def-db-fns resource-path {:ns this-ns})
  (stop [this]

(defn new-queries []
  (map->PostgresQueries {:resource-path queries-file}))

This comment has been minimized.

Copy link

commented Oct 4, 2017

I got around this by using map-of-db-fns within the component, passing in my db connection with use-component, and appending a function which takes a keyword and (maybe) options, and calls the function using the db spec:

(defrecord Queries [resource-path hikari-cp]
  (start [this]
    (let [map-of-fns (hugsql/map-of-db-fns queries-file)]
      (assoc this
             :fns map-of-fns
             (fn query-fn
                (query-fn kw {}))
               ([kw opts]
                ((get-in map-of-fns [kw :fn])
                 (:db-spec hikari-cp)
  (stop [this]
    (-> this
        (dissoc :query-fn)
        (dissoc :fns))))

This comment has been minimized.

Copy link

commented Feb 16, 2018

I submitted a patch to tools.namespace which would allow reloads to work. In your namespace, you'd be able to track external files as part of your namespace changing.

(ns my-ns
  { #{"queries/app.sql"})

(def queries-file "queries/app.sql")
(hugsql/def-db-fns queries-file)

It has not been looked at yet though 🤞


This comment has been minimized.

Copy link

commented Jul 27, 2018

Hey folks I have a fork with TNS-48 applied.

There's some instructions in the README (it should work as a drop-in replacement for existing tools.namespace 😁)


I'm looking for feedback to see if this is something valuable to y'all 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.