我正在学习http://www.datomic.com/company/resources/tutorial教程,但我想我缺少关于如何访问Datomic时间模型的难题。

如果我执行一系列添加和撤消

;; DO a series of transactions
;; (transact conn [:db/add entity-id attribute value0])

(use 'datomic.api)
(dir datomic.api)
(def conn (connect "datomic:dev://localhost:4334/demo"))

(transact conn '[:db/add 2000 :db/doc "Hello There"])
(q '[:find ?e ?n :where [?e :db/doc ?n] [(= 2000 ?e)]] (db conn))
; => <HashSet [[2000 "Hello There"]]>

(transact conn '[:db/add 2000 :db/doc "Hello There 1"])
(q '[:find ?e ?n :where [?e :db/doc ?n] [(= 2000 ?e)]] (db conn))
; => <HashSet [[2000 "Hello There 1"]]>

(transact conn '[:db/add 2000 :db/doc "Hello There 2"])
(q '[:find ?e ?n :where [?e :db/doc ?n] [(= 2000 ?e)]] (db conn))
; => <HashSet [[2000 "Hello There 2"]]>

(transact conn '[:db/add 2000 :db/doc "Hello There 3"])
(q '[:find ?e ?n :where [?e :db/doc ?n] [(= 2000 ?e)]] (db conn))
; => <HashSet [[2000 "Hello There 3"]]>

如何获得(实体2000属性:db/doc)上的值的一系列更改?

我想要某种形式的东西
[ [Transaction Number, Time, Value] .... [Transaction Number, Time, Value]]

例如:
[ [T1, "2012-March-16-9:30:12", "Hello There"]
  ....
  ....
  ....
  [T27, "2012-June-14-9:54:38", "Hello There 3"] ]

并不是那么困难,但是有很多我不熟悉的:db内部参数。

最佳答案

看看(history db)函数in the reference.



使用history,您可以执行以下操作来获取所需的数据:

datomic-test.core> (q '[:find ?tx ?tx-time ?v
                        :in $ ?e ?a
                        :where [?e ?a ?v ?tx _]
                               [?tx :db/txInstant ?tx-time]]
                      (d/history (db conn))
                      2000
                      :db/doc)
#<HashSet [[13194139534315 #inst "2012-09-09T00:45:49.086-00:00" "Hello There"] [...]]>

还要查看(tempid :db.part/user)以获得ID,而不是使用像2000这样的硬编码数字。

10-07 19:27
查看更多