This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-23
Channels
- # announcements (1)
- # babashka (13)
- # cherry (12)
- # cider (6)
- # clj-kondo (3)
- # cljs-dev (28)
- # clojure (77)
- # clojure-europe (25)
- # clojure-nl (1)
- # clojure-norway (35)
- # clojure-uk (5)
- # clojurescript (31)
- # conjure (7)
- # cursive (12)
- # data-science (9)
- # datalevin (5)
- # datomic (8)
- # hyperfiddle (21)
- # jobs (7)
- # kaocha (19)
- # malli (26)
- # matrix (3)
- # releases (1)
- # shadow-cljs (42)
- # squint (95)
- # testing (2)
- # vim (14)
- # wasm (1)
Is is possible to query inside a nested map? e.g. given an entity
{:foo {:bar 123}}
do something like
(ds/q '[:find (pull ?e [*])
:where
[?e :foo ?foo]
[(= (get ?foo :bar) 123)]])
I tried this, or with [?foo :bar 123]
, but neither seem to do the trick.You can convert nested maps into structure suitable for Datalevin with libraries such as this: https://github.com/alandipert/intension
(defn paths
"Returns the set of paths into a nested map/vector."
([root]
{:pre [(or (map? root)
(vector? root))]}
(paths [] root))
([parent x]
(cond (map? x)
(mapcat (fn [[k v]] (paths (conj parent k) v)) x)
(vector? x)
(mapcat #(paths (conj parent %1) %2) (range) x)
:else [parent])))
(defn make-db
"Converts a nested structure of vectors/maps into a set of tuples suitable for
query by Datalog. Attaches the original structure to the vector returned under the :alandipert.intension/original key.
Takes an optional configuration map that can contain these options:
:paths? - false by default. Whether or not to prefix every tuple with the path.
Useful for processing structures with update-in based on query results."
[coll & [{:keys [paths?]
:or {paths? false}}]]
(with-meta
(mapv (fn [path]
(conj
(if paths? (vec (list* path path)) path)
(get-in coll path)))
(paths coll))
{::original coll}))