Skip to content

Instantly share code, notes, and snippets.

@kordano
Last active June 14, 2021 06:49
Show Gist options
  • Select an option

  • Save kordano/1f6637b57e0a6de854235241c8d5abe1 to your computer and use it in GitHub Desktop.

Select an option

Save kordano/1f6637b57e0a6de854235241c8d5abe1 to your computer and use it in GitHub Desktop.
Retract add Datahike example
(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 :sibling
: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"}
:keep-history? true
:schema-flexibility :write
:initial-tx schema})
(d/delete-database cfg)
(d/create-database cfg)
(def conn (d/connect cfg))
(d/transact conn [{:name "Alice"
:age 25}
{:name "Bob"
:age 35}
{:name "Charlie"
:age 45
:sibling [[:name "Alice"] [:name "Bob"]]}])
(d/q '[:find ?e ?n ?sn
:where
[?e :name ?n]
[?e :sibling ?s]
[?s :name ?sn]]
@conn)
(d/transact conn {:tx-data [[:db/retract [:name "Charlie"] :sibling]
[:db/add [:name "Charlie"] :sibling [:name "Alice"]]]})
(d/q '[:find ?e ?n ?sn
:where
[?e :name ?n]
[?e :sibling ?s]
[?s :name ?sn]]
@conn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment