After a bit more reading, I've figured out that the :in
keyword is the key to all of this. Examples are given in the 'Advanced Queries' section of the Tutorial - http://docs.datomic.com/tutorial.html.
This is the equivalent query listing all attributes in the :account
namespace
(d/q '[:find ?e ?ident ?ns :in $ ?ns :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns)]]
(d/db *conn*)
"account")
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>
This is the equivalent in a function
(defn get-ns-attrs [_ns db]
(d/q '[:find ?e ?ident :in $ ?ns :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns) ]] db (str _ns)))
(get-ns-attrs :account (d/db *conn*))
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>
If you require more modularity, the function can be further broken down using %
to pass in a set of rules:
(def rule-nsAttrs
'[[nsAttrs ?e ?ident ?ns]
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns)]])
(defn get-ns-attrs [_ns db]
(d/q '[:find ?e ?ident :in $ % ?ns :where
(nsAttrs ?e ?ident ?ns)]
(d/db *conn*)
[rule-nsAttrs]
(str _ns)))
(get-ns-attrs :account (d/db *conn*))
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>