Skip to content

Instantly share code, notes, and snippets.

@Akeboshiwind
Created February 19, 2024 11:01
Show Gist options
  • Save Akeboshiwind/87ed84c715576ec50227ca260072b302 to your computer and use it in GitHub Desktop.
Save Akeboshiwind/87ed84c715576ec50227ca260072b302 to your computer and use it in GitHub Desktop.
xtdb-v1 entity-history
; Add some sensor readings for sensor :a
(xt/submit-tx xtdb-node
[; Day 1: You get a reading of 1
[::xt/put
{:xt/id :a :value 1}
#inst "2024-01-01"]
; Day 2: You get a reading of 2
[::xt/put
{:xt/id :a :value 2}
#inst "2024-01-02"]
; Day 3: You get a reading of 2 again
[::xt/put
{:xt/id :a :value 2}
#inst "2024-01-03"]
; Day 4: You get a reading of 3
[::xt/put
{:xt/id :a :value 3}
#inst "2024-01-04"]])
; Get the current value of :a
(xt/q (xt/db xtdb-node)
'{:find [(pull doc [*])]
:where [[doc :xt/id :a]]})
; => #{[{:xt/id :a, :value 3}]}
; How has :a changed over "valid time"?
(let [sensor :a]
(->> (xt/entity-history (xt/db xtdb-node)
sensor
:asc
{:with-docs? true})
(mapv ::xt/doc)))
; => [{:xt/id :a, :value 1}
; {:xt/id :a, :value 2}
; {:xt/id :a, :value 2}
; {:xt/id :a, :value 3}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment