Skip to content

Instantly share code, notes, and snippets.

@Jaretbinford
Created November 30, 2017 16:01
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 Jaretbinford/3920c36e332d728f73f0256b6e2f5101 to your computer and use it in GitHub Desktop.
Save Jaretbinford/3920c36e332d728f73f0256b6e2f5101 to your computer and use it in GitHub Desktop.
;;Example using an in-mem db
;;Transacts jaret and his 1000 + dogs
(require '[datomic.api :as d])
(def uri "datomic:mem://test")
(d/create-database uri)
(def conn (d/connect uri))
(def schema
[{:db/id #db/id[:db.part/db]
:db/ident :dog/owner
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/doc "A dogs owner id"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :dog/ownername
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "A dogs owner name"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :dog/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "A dogs name"
:db.install/_attribute :db.part/db}])
(d/transact conn schema)
(def dogdata [{:dog/owner 100
:dog/ownername "jaret"
:dog/name "Tig" }])
(d/transact conn dogdata)
(let [uri "datomic:mem://test"
conn (d/connect uri)]
(time (dotimes [_ 1000]
(let [more-dogs {:db/id (d/tempid :db.part/user)
:dog/name (str (java.util.UUID/randomUUID))
:dog/owner 100}]
@(d/transact conn [more-dogs])))))
(def db (d/db conn))
(def all-dog-names '[:find ?dog-name ?owner
:where [_ :dog/name ?dog-name]
[_ :dog/owner ?owner]])
(d/q all-dog-names db)
(def all-dog-names-by-owner '[:find ?e ?name
:where [?e :dog/name ?name]
[?e :dog/owner 100]])
(def results
(d/q all-dog-names-by-owner db))
(count results)
;;1001
;;Pull without limit
(count (:dog/_owner (d/pull db '["*" {:dog/_owner [:dog/name]}] 100)))
;1000
;;Pull with limit nil
(def resultpull
(d/pull db '[{(limit :dog/_owner nil) [:dog/name]}]
100))
resultpull
(count (:dog/_owner resultpull))
;1001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment