1

I want to transact a deeply nested tree structure into Datomic. An example data structure:

{:tree/id (d/tempid :db.part/user),
 :tree/nodes [
   {:node/name "Node1",
    :node/parent "root-node-ref",
    :node/tasks {"task-entities-map"}},
   {:node/name "Node2",
    :node/parent "node1-ref",
    :node/tasks {"task-entities-map"}}]}

Here Node2 is a child of Node1, and Node1 is a child of some root node.

Datomic docs at http://blog.datomic.com/2013/06/component-entities.html indicate that it's not necessary to specify temp ids for nested maps, as they will be created automatically (given that :db/isComponent is set to true for :tree/nodes, :node/tasks etc.).

The question is: how do i specify the parent-child relations here, as given by :node/parent attributes? I want to avoid having to specify node children e.g. by :node/children attribute. And will Datomic automatically specify temp ids for the entities in :node/tasks lists?

Thanks in advance.

1 Answer 1

3
{:db/id (d/tempid :db.part/user),
 :tree/nodes 
 [{:db/id (d/tempid :db.part/user -1),
   :db/name "Node1",
   :node/parent (:db/id root-node)} ;; assuming you have queried root-node
  {:node/name "Node2",
   :node/parent (d/tempid :db.part/user -1)}]

Docstring of d/tempid:

Within the scope of a single transaction, tempids map consistently to permanent ids. Values of n from -1 to -1000000, inclusive, are reserved for user-created tempids.

Find children of Node1 like this

 (d/q '[:find ?children
        :where [?node-1 :node/name "Node1"]
               [?children :node/parent ?node-1]] 
      (d/db conn))

Or, assuming that you have queried node1, find its children via

 (:node/_parent node1)
3
  • I am sorry, i've used the wrong name for the variable (sub-nodes) that must have confused you. Will change sub-nodes to say tasks and update the original question. 'tasks' are a list of entities that are not included in the original hierarchy. So that there are only :node/parents specified, but not :node/sub-nodes.
    – vitvly
    Sep 18, 2014 at 8:27
  • I have modified the answer accordingly. Sep 18, 2014 at 9:41
  • Thanks! Had to correct tempid generation though, so that negative values are different for nodes and tasks.
    – vitvly
    Sep 19, 2014 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.