Skip to content

Instantly share code, notes, and snippets.

@kordano
Last active November 18, 2020 09:52
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 kordano/8ef2486d83c9a2f10542151e290ec360 to your computer and use it in GitHub Desktop.
Save kordano/8ef2486d83c9a2f10542151e290ec360 to your computer and use it in GitHub Desktop.
Example code for 'load-entities'
(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))
(def entities
[[1000 :db/txInstant #inst "2020-11-18T09:30:20.275-00:00" 1000 true] ;; tx meta data
[100 :name "Alice" 1000 true] ;; normal entity
[100 :age 25 1000 true]
[1001 :db/txInstant #inst "2020-11-18T09:35:20.122-00:00" 1001 true]
[101 :name "Bob" 1001 true]
[101 :age 35 1001 true]
[1002 :db/txInstant #inst "2020-11-18T09:35:25.459-00:00" 1002 true]
[100 :age 25 1002 false] ;; entity updates in history
[100 :age 30 1002 true]
[1003 :db/txInstant #inst "2020-11-18T09:36:11.765-00:00" 1003 true]
[102 :name "Charlie" 1003 true]
[102 :age 45 1003 true]
[102 :sibling 100 1003 true] ;; references
[102 :sibling 101 1003 true] ;; many cardinality
])
@(d/load-entities conn entities)
(d/q '[:find (pull ?e [* {:sibling [:name]}] )
:where
[?e :name _]] @conn) ;; check data with new IDs
(d/q '[:find ?n ?a ?d ?added
:where
[?e :name ?n]
[?e :age ?a ?t ?added]
[?t :db/txInstant ?d]]
(d/history @conn)) ;; check changes in history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment