Skip to content

Instantly share code, notes, and snippets.

@alndvz
Created November 2, 2021 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alndvz/89931bb8f16867fd5f2e28d36ff85be9 to your computer and use it in GitHub Desktop.
Save alndvz/89931bb8f16867fd5f2e28d36ff85be9 to your computer and use it in GitHub Desktop.
XTDB History Example
(ns vid9.core
(:require [xtdb.api :as xt]))
(def node (xt/start-node {:xtdb.jdbc/connection-pool {:dialect {:xtdb/module 'xtdb.jdbc.mysql/->dialect}
:db-spec {:host "localhost"
:dbname "xtdbdb"
:user "root"
:password "password"}}
:xtdb/tx-log {:xtdb/module 'xtdb.jdbc/->tx-log
:connection-pool :xtdb.jdbc/connection-pool}
:xtdb/document-store {:xtdb/module 'xtdb.jdbc/->document-store
:connection-pool :xtdb.jdbc/connection-pool}}))
(def products-1 [{:xt/id :towel1
:prod/name "Purple Towel"
:prod/quantity 10
:prod/date-updated #inst "2021-11-02T18:00:00.000-00:00"
:prod/updated-by "Allan"
:prod/price 200}
{:xt/id :facecloth1
:prod/name "Blue Facecloth"
:prod/quantity 5
:prod/date-updated #inst "2021-11-02T18:00:00.000-00:00"
:prod/updated-by "Allan"
:prod/price 50}])
(def products-2 [{:xt/id :towel1
:prod/name "Purple Towel"
:prod/quantity 7
:prod/date-updated #inst "2021-11-10T18:00:00.000-00:00"
:prod/updated-by "Allan"
:prod/price 200}
{:xt/id :facecloth1
:prod/name "Blue Facecloth"
:prod/quantity 1
:prod/date-updated #inst "2021-11-10T18:00:00.000-00:00"
:prod/updated-by "Allan"
:prod/price 50}])
(def products-3 [{:xt/id :towel1
:prod/name "Purple Towel"
:prod/quantity 17
:prod/date-updated #inst "2021-11-12T18:00:00.000-00:00"
:prod/updated-by "Dan"
:prod/price 210}
{:xt/id :facecloth1
:prod/name "Blue Facecloth"
:prod/quantity 6
:prod/date-updated #inst "2021-11-12T18:00:00.000-00:00"
:prod/updated-by "Allan"
:prod/price 55}])
(def products-4 [{:xt/id :towel1
:prod/name "Purple Towel"
:prod/quantity 20
:prod/date-updated #inst "2021-11-13T18:00:00.000-00:00"
:prod/updated-by "Dan"
:prod/price 210}
{:xt/id :facecloth1
:prod/name "Blue Facecloth"
:prod/quantity 10
:prod/date-updated #inst "2021-11-13T18:00:00.000-00:00"
:prod/updated-by "Dan"
:prod/price 55}])
(defn prepare-docs [docs]
(mapv (fn [doc]
[::xt/put doc #inst "2021-01-04T18:00:00.000-00:00"]) docs))
(comment
(xt/submit-tx node (prepare-docs products-4))
nil)
(def history (xt/entity-history (xt/db node) :towel1 :desc {:with-docs? true}))
history
(->> (map ::xt/doc history)
(filter #(= (get % :prod/updated-by) "Dan")))
(xt/q (xt/db node {::xt/valid-time #inst "2021-01-03T18:00:00.000-00:00"})
'{:find [(sum ?total)]
:where [[?e :xt/id]
[?e :prod/price ?price]
[?e :prod/quantity ?quantity]
[(* ?price ?quantity) ?total]]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment