datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
lilactown 2020-03-21T17:34:14.000500Z

related to my previous question, now that I’ve transacted this deeply nested data, I need to pull it all out

lilactown 2020-03-21T17:34:55.001300Z

I need a query that will return an arbitrarily (but finite, no cycles) deep tree of comments

lilactown 2020-03-21T17:35:24.001900Z

I think pull might be what I want, but I’m struggling to find resources on how to write this query

oconn 2020-03-21T19:10:17.003800Z

As long as the comments are set up as refs you can query and shape the output using pull

(as-> '[:find (pull ?branch [*
                             {::branches/event-automation-records
                              [*
                               {::event-automation-records/event
                                [::events/uuid
                                 ::events/name]}]}])
        :in $ ?organization-uuid
        :where [?branch ::branches/organization ?organization]
               [?organization ::organizations/uuid ?organization-uuid]] $
  (db/q $ org-uuid)
  ;; ...
  )

oconn 2020-03-21T19:22:02.010800Z

Reading your previous comment, I’m not sure how datascript stored the value of comments (and I could be missing something), but it may have just taken that vec of comments and set it as the value of :comments (since datascript can store any type for transacted values) https://github.com/tonsky/datascript#differences-from-datomic - In that case I'm not sure you would be able to leverage any of the query rule system for that subcollection. @lilactown

lilactown 2020-03-21T21:15:31.011300Z

yeah I’m not sure either tbh. will check

lilactown 2020-03-21T21:17:07.012600Z

if they did get transacted as entities, will your pull query get all nested comments as well? e.g.

{:id 1
 :comments [{:id 2
             :comments [,,,]}
            {:id 3
             :comments [,,,]}]}
?

oconn 2020-03-21T21:38:06.014Z

I believe you would have to do something like this (in datomic) https://docs.datomic.com/on-prem/pull.html#org32b7414 by tagging the entity as a component :db/isComponent true but I'm not sure if you can do this with datascript.

oconn 2020-03-21T21:39:03.014700Z

Maybe so - https://github.com/tonsky/datascript/issues/3

lilactown 2020-03-21T23:12:37.015100Z

@oconn :db/isComponent true is exactly what I wanted!! thank you!