Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
Created November 27, 2017 16:26
Show Gist options
  • Save rsslldnphy/05dcf2905a2e226efe2089effcd2c753 to your computer and use it in GitHub Desktop.
Save rsslldnphy/05dcf2905a2e226efe2089effcd2c753 to your computer and use it in GitHub Desktop.
HugSQL namespace generator macro
(ns my-app.sql
(:require [hugsql.core :as hugsql]))
(defmacro defsql
[& args]
(let [files (vec (drop-last 2 args))
alias (last args)]
`(let [current-namespace# *ns*
query-namespace# (symbol (str (ns-name *ns*) ".sql"))]
(create-ns query-namespace#)
(in-ns query-namespace#)
(require '[hugsql.core :as hugsql])
(doseq [file# ~files]
(hugsql/def-db-fns file# {:command-options [{:identifiers identity}]}))
(in-ns (ns-name current-namespace#))
(alias '~alias query-namespace#))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment