Skip to content

Commit

Permalink
Merge pull request #145 from vollcheck/auto-reload-sql-queries-on-dev
Browse files Browse the repository at this point in the history
Add a dev clause for SQL queries automatic reload
  • Loading branch information
yogthos authored Sep 3, 2024
2 parents ef4b740 + cd8e6ad commit 4c34516
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
:db.sql/query-fn
{:conn #ig/ref :db.sql/connection
:options {}
:filename "queries.sql"} <% endif %><% if migratus? %>
:filename "queries.sql"
:env #ig/ref :system/env} <% endif %><% if migratus? %>

:db.sql/migrations
{:store :database
Expand Down
27 changes: 20 additions & 7 deletions libs/kit-sql-conman/src/kit/edge/db/sql/conman.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,30 @@
[key opts old-opts old-impl]
(ig-utils/resume-handler key opts old-opts old-impl))

(defn queries-dev [load-queries]
(fn
([query params]
(conman/query (load-queries) query params))
([conn query params & opts]
(conman/query conn (load-queries) query params opts))))

(defn queries-prod [load-queries]
(let [queries (load-queries)]
(fn
([query params]
(conman/query queries query params))
([conn query params & opts]
(conman/query conn queries query params opts)))))

(defmethod ig/init-key :db.sql/query-fn
[_ {:keys [conn options filename filenames]
[_ {:keys [conn options filename filenames env]
:or {options {}}}]
(let [filenames (or filenames [filename])
queries (apply conman/bind-connection-map conn options filenames)]
load-queries #(apply conman/bind-connection-map conn options filenames)]
(with-meta
(fn
([query params]
(conman/query queries query params))
([conn query params & opts]
(apply conman/query conn queries query params opts)))
(if (= env :dev)
(queries-dev load-queries)
(queries-prod load-queries))
{:mtimes (mapv ig-utils/last-modified filenames)})))

(defmethod ig/suspend-key! :db.sql/query-fn [_ _])
Expand Down

0 comments on commit 4c34516

Please sign in to comment.