Last active
June 14, 2021 06:49
-
-
Save kordano/1f6637b57e0a6de854235241c8d5abe1 to your computer and use it in GitHub Desktop.
Retract add Datahike example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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