Skip to content

Instantly share code, notes, and snippets.

@yayitswei
Last active December 23, 2015 12:29
Show Gist options
  • Save yayitswei/6635158 to your computer and use it in GitHub Desktop.
Save yayitswei/6635158 to your computer and use it in GitHub Desktop.
learning how to use the history db
(defn balance-history [id]
(d/q '[:find ?tx ?tx-time ?v
:in $ ?e ?a
:where [?e ?a ?v ?tx _]
[?tx :db/txInstant ?tx-time]]
(d/history (db/db)) id :account/balance))
@(d/transact conn [[:credit 17592186145525 100]])
(balance-history 17592186145525)
; => #{[13194139534419 #inst "2013-09-20T09:04:56.730-00:00" 0]
; [13194139534419 #inst "2013-09-20T09:04:56.730-00:00" 100]
; [13194139534415 #inst "2013-09-20T09:00:49.186-00:00" 0]}
; why does this return three results? I want only two: the first 0 and 100.
[
{:db/id #db/id[:db.part/db]
:db/ident :account/balance
:db/cardinality :db.cardinality/one
:db/valueType :db.type/bigint
:db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/user]
:db/ident :credit
:db/fn #db/fn {:lang "clojure"
:params [db id amount]
:code [[:db/add id :account/balance (biginteger balance)]]}}
]
@yayitswei
Copy link
Author

the answer was to restrict ?added (the fifth field in the query pattern) to true: :where [?e ?a ?v ?tx true]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment