Search code examples
clojuredatomic

how does the tempid number relate to the final :tempid hashmap?


If I want to stick a record into datomic:

i.e.

{:db/id #db/id[:db.part/user -1226692165], :address/country "Australia"}

I get a map back:

{:db-before #db{1000 #inst "2014-10-28T05:39:40.413-00:00"},
 :db-after #db{1001 #inst "2014-10-28T05:39:53.626-00:00"},
 :tx-data [#datom[13194139534313 50 #inst "2014-10-28T05:39:53.626-00:00" 13194139534313 true]
           #datom[17592186045418 67 "Australia" 13194139534313 true]],
 :tempids {-9223350047848912453 17592186045418}}

with -9223350047848912453 being the key for the tempid

now.... how do these map to each other?

 #db/id[:db.part/user -1226692165] <=> -9223350047848912453

Solution

  • You can map the temporary ids to entity ids using the resolve-tempid function.

    Something along these lines:

    (defn transact!
      [conn tx]
      (let [{eids :tempids} @(d/transact conn tx)]
            (map #(d/resolve-tempid (d/db conn)
                                    eids
                                    (:db/id %)) tx)))
    

    The relationship otherwise is an ordered one (first with first, second with second, etc.)