This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-10
Channels
- # announcements (28)
- # architecture (1)
- # babashka (4)
- # beginners (55)
- # biff (4)
- # calva (11)
- # clerk (1)
- # clj-http (2)
- # clj-kondo (1)
- # clojure (46)
- # clojure-austin (1)
- # clojure-europe (32)
- # clojure-nl (1)
- # clojure-norway (17)
- # clojure-uk (4)
- # clojurescript (5)
- # cursive (6)
- # datomic (2)
- # emacs (2)
- # events (1)
- # fulcro (33)
- # hoplon (4)
- # humbleui (30)
- # hyperfiddle (50)
- # jackdaw (2)
- # jobs (13)
- # joyride (8)
- # lsp (2)
- # malli (2)
- # off-topic (6)
- # re-frame (4)
- # remote-jobs (1)
- # rum (10)
- # shadow-cljs (10)
- # xtdb (7)
Hi there, I am trying to build a :where
clause from a map of keys...
Is there a way to do that? I am playing with :in
and its bindings but I cannot really understand if I have to use :in
for this
Yes, you can build it programmatically. I have a program where I built my queries automatically. This is code that illustrates it:
(-> acc
(update :in intov ['lower 'upper])
(update :where intov ['[car :car/miles miles]
'[(< miles upper)]
'[(> miles lower)]])
(update :params intov [lower upper])))
Where acc is a map that I build in reduce over parameters. The trick is to put '
before your vectors where you don't need to change them.this
'{:find [*] :where [a :b/id c]}
Equals to
{:find '[*] :where '[a :b/id c]}
And then you can use regular clojure functions to construct second map.I think I have managed, these is what I eventually drafted
(defn compute-criteria-in
[searchable-keys search-criteria]
(let [bindings (->>
searchable-keys
(into []
(comp (map (fn [search-key]
;; NOTE: we need find cause `nil`s are valid values and `get` would discard them
(when (find search-criteria search-key)
(symbol (str "?" (name search-key))))))
(filter some?))))]
;; DONT: we want an empty vector not [[]] if no bindings were computed
(cond-> []
(seq bindings) (conj bindings))))
It should work. Also, you don't really need to have ?
before symbol in the xtdb query. The question mark is a convention, so it's easier to read the query with eyes. If you autogenerate query, you can skip that. Just to make your code a tiny bit easier.
yep sounds good thanks!
it's a bit different from this I guess https://docs.xtdb.com/language-reference/1.23.3/datalog-queries/#_relation_binding Cause I have only one map of key-values