vitvly
vitvly

Reputation: 3638

Datomic component ids

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.

Upvotes: 1

Views: 309

Answers (1)

Leon Grapenthin
Leon Grapenthin

Reputation: 9276

{: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)

Upvotes: 3

Related Questions