Skip to content

Instantly share code, notes, and snippets.

@prepor
Created February 17, 2022 11:41
Show Gist options
  • Save prepor/534e89413cf428dbdb881d1cd4b185fe to your computer and use it in GitHub Desktop.
Save prepor/534e89413cf428dbdb881d1cd4b185fe to your computer and use it in GitHub Desktop.
(def schema {:kb/parent {:db/type :db.type/ref}
:kb/children {:db/cardinality :db.cardinality/one}})
(def conn
(let [c (d/create-conn schema)
{:keys [tempids]}
(d/transact! c
[{:db/id -1
:title "Intermediate"
:tags #{:level}}
{:db/id -2
:title "Business"
:tags #{:theme}}
{:db/id -3
:tags #{:learn}}
{:db/id -4
:tags #{:practice}}])]
(d/transact! c
[{:kb/children [(tempids -1)]}
{:kb/parent (tempids -1)
:kb/children [(tempids -2)]}
{:kb/parent (tempids -2)
:kb/children [(tempids -3) (tempids -3)]}])
c))
(defn render-branch [e]
(let [rel-node (first (:kb/_parent e))
children (map (partial d/entity @conn) (:kb/children rel-node))]
[:li
[:pre (pr-str (dissoc (d/pull @conn '[*] (:db/id e))
:db/id))]
[:ul
(for [child children]
^{:key child} [render-branch child])]]))
(defn root []
(let [root-nodes (mapcat first (d/q '[:find ?children
:where [?e :kb/children ?children]
(not [?e :kb/parent])]
@conn))]
[:div.content
[:ul
(for [e root-nodes]
^{:key e} [render-branch (d/entity @conn e)])]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment