Skip to content

Instantly share code, notes, and snippets.

@kordano
Created October 4, 2021 16:22
Show Gist options
  • Save kordano/96ec82b4dc8ced0b1526e2784d6ff9fc to your computer and use it in GitHub Desktop.
Save kordano/96ec82b4dc8ced0b1526e2784d6ff9fc to your computer and use it in GitHub Desktop.
datahike-upsert-history
(require '[datahike.api :as d])
(def schema [{:db/ident :name
:db/cardinality :db.cardinality/one
:db/index true
:db/unique :db.unique/identity
:db/valueType :db.type/string}
{:db/ident :parents
:db/cardinality :db.cardinality/many
:db/valueType :db.type/ref}
{:db/ident :age
:db/cardinality :db.cardinality/one
:db/valueType :db.type/long}])
(def cfg {:store {:backend :mem
:id "sandbox"}
:name "sandbox"
:keep-history? true
:schema-flexibility :write
:attribute-refs? true})
(d/delete-database cfg)
(d/create-database cfg)
(def conn (d/connect cfg))
(d/transact conn {:tx-data schema})
(d/transact conn {:tx-data [{:name "Alice"
:age 25}]})
(d/datoms @conn :eavt [:name "Alice"] :age)
(d/datoms (d/history @conn) :eavt [:name "Alice"] :age)
(d/transact conn {:tx-data [{:db/id [:name "Alice"]
:age 25}]})
(d/datoms @conn :eavt [:name "Alice"] :age)
(d/datoms (d/history @conn) :eavt [:name "Alice"] :age)
(d/transact conn {:tx-data [{:db/id [:name "Alice"]
:age 25}]})
(d/datoms @conn :eavt [:name "Alice"] :age)
(d/datoms (d/history @conn) :eavt [:name "Alice"] :age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment