Getting the id of an inserted entity in datomic?

前端 未结 4 1865
臣服心动
臣服心动 2021-02-19 20:43

After I run a transaction in datomic to insert a value, how I can use the return value of the transaction to get the ids of any entities that were created?

Here is a sam

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 20:55

    Use d/resolve-tempid. If you were to transact a single entity, looking at :tx-data would work but if your transaction contained more than one entity, then you wouldn't know the order in which they appear in :tx-data.

    What you should do is give temporary ids to your entities (before transacting them) using either (d/tempid) or its literal representation #db/id[:db.part/user _negativeId_] and then use d/resolve-tempid to go from your temporary id to the real id given by the database. The code would look something like:

    (d/resolve-tempid (d/db conn) (:tempids tx) (d/tempid :db.part/user _negativeId_))
    

    For a full code sample, see this gist.

提交回复
热议问题