Skip to content

Instantly share code, notes, and snippets.

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 KnowledgeGarden/780b2d485a2211ccf6e4c643bc187ac8 to your computer and use it in GitHub Desktop.
Save KnowledgeGarden/780b2d485a2211ccf6e4c643bc187ac8 to your computer and use it in GitHub Desktop.
datascript test
(ns datascript-test.core
(:require [datascript.core :as d])
)
;; working from https://github.com/kristianmandrup/datascript-tutorial/blob/master/datascript_getting_started.md
;;; Create a DataScript "connection" (an atom with the current DB value)
(def conn (d/create-conn))
;; Define datoms to transact
(def datoms [{:db/id -1 :name "Bob" :age 30}
{:db/id -2 :name "Sally" :age 15}])
;;; Query to find names for entities (people) whose age is less than 18
(def q-young '[:find ?n
:in $ ?min-age
:where
[?e :name ?n]
[?e :age ?a]
[(< ?a ?min-age)]])
;; main entry point
(defn foo
"I don't do a whole lot."
[]
(println "conn" conn)
;;; Add the datoms via transaction
(d/transact! conn datoms)
(println "conn2" conn)
;; execute query: q-young, passing 18 as min-age
;; q wants a query, db, and - in this case - a parameter
(d/q q-young @conn 18)
(println) ;; gap before showing output
(d/q '[:find ?attr :where [?attr :name]] @conn)
(d/q '[:find ?e ?attr ?value :where [?e ?attr ?value]] @conn)
;; todo - this is not showing any output
(d/pull @conn '[:db/id, :name] 536870913)
;; does not respond
)
;; conn #object[clojure.lang.Atom 0x83e635f {:status :ready,
;; :val #datascript/DB {:schema nil, :datoms []}}]
;; conn2 #object[clojure.lang.Atom 0x83e635f {:status :ready,
;; :val #datascript/DB {:schema nil,
;; :datoms [[1 :age 30 536870913] [1 :name Bob 536870913]
;; [2 :age 15 536870913] [2 :name Sally 536870913]]}}]
;; for now, query is not returning anything
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment