Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created July 17, 2012 21:24
Show Gist options
  • Save noahlz/3132207 to your computer and use it in GitHub Desktop.
Save noahlz/3132207 to your computer and use it in GitHub Desktop.
First steps with Datomic
;; adopted from http://www.datomic.com/company/resources/getting-started
;; Clojure 1.4.0
;; user=>
(use '[datomic.api :only [q db] :as d])
;;=> nil
;; user=>
(doc d/q)
;; -------------------------
;; datomic.api/q
;; ([query & inputs])
;; Executes a query against inputs.
;;
;; Inputs are data sources e.g. a database value retrieved from
;; Connection.db, a list of lists, and/or rules. If only one data
;; source is provided, no :in section is required, else the :in
;; section describes the inputs.
;;
;; query can be a map, list, or string:
;;
;; The query map form is {:find vars :in sources :where clauses}
;; where vars, sources and clauses are lists.
;;
;; The query list form is [:find ?var1 ?var2 ...
;; :in $src1 $src2 ...
;; :where clause1 clause2 ...]
;; The query list form is converted into the map form internally.
;;
;; The query string form is a string which, when read, results
;; in a query list form or query map form.
;;
;; Query parse results are cached.
;;
;; Returns a collection of result tuples, as list, with the
;; first item in each list corresponding to ?var1, etc.
;;=> nil
;; user=>
(def uri "datomic:mem://seattle")
;;=> #'user/uri
;; user=>
(d/create-database uri))
;;=> true
;; user=>
(def conn (d/connect uri))
;;=> #'user/conn
;; user=>
conn
;;=> #<LocalConnection datomic.peer.LocalConnection@79a86488>
;; user=>
(d/transact conn [["db/add" (d/tempid "db.part/user") "db/doc" "hello world"]])
;;=> #<common$completed_future$reify__217@1cac622a: {:t 1000}>
;; user=>
(def results (d/q '[:find ?entity :where [?entity :db/doc "hello world"]] (db conn)))
;;=> #'user/results
;; user=>
results
;;=> #<HashSet [[17592186045417]]>
;; user=>
(for [x results] (d/entity (db conn) (first x)))
;;=> ({:db/id 17592186045417})
;; user=>
(for [x results] (:db/doc (d/entity (db conn) (first x))))
;;=> ("hello world")
;; user=>
(defn run-query [query conn]
(let [results (d/q query (d/db conn))
(println "Found" (count results) "results")
(doseq [x results] (println x))))
;;=> #'user/run-query
;; user=>
(def data (read-string (slurp "samples/seattle/seattle-schema.dtm")))
;;=> #'user/data
;; user=>
@(d/transact conn data)
;; #<common$completed_future$reify__217@22e1469c: {:t 1000}>
;;=> {:t 1000}
;; user=>
(def data (read-string (slurp "samples/seattle/seattle-data0.dtm")))
;;=> #'user/data
;; user=>
@(d/transact conn data)
;;=> {:t 1021}
;; user=>
(run-query '[:find ?c :where [?c :community/name]] conn)
;;=> Found 150 results
;;=> [17592186045520]
;;=> [17592186045518]
;;=> [17592186045516]
;;=> ...
;;=> [17592186045566]
;;=> nil
@noahlz
Copy link
Author

noahlz commented Jul 17, 2012

"db/add" could be :db/add

@zachthompson
Copy link

Extra ) line 44
missing ] line 77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment