Skip to content

Instantly share code, notes, and snippets.

@verma
Last active September 4, 2018 19:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verma/1be6a0ddba850eb0da437968cd5994aa to your computer and use it in GitHub Desktop.
Save verma/1be6a0ddba850eb0da437968cd5994aa to your computer and use it in GitHub Desktop.
(ns dt.core
(:require [datascript.core :as d]))
;; schema so nice
(def schema {:car/maker {:db/type :db.type/ref}
:car/colors {:db/cardinality :db.cardinality/many}})
;; create the connection
(def conn (d/create-conn schema))
;; insert a maker
(d/transact! conn [{:maker/name "Honda"
:maker/country "Japan"}])
;; insert a maker a car along with it, referring back to
(d/transact! conn [{:db/id -1
:maker/name "BMW"
:maker/country "Germany"}
{:car/maker -1
:car/name "i525"
:car/colors ["red" "green" "blue"]}])
;; query a single the name of the cars we know for maker BMW.
(d/q '[:find ?name
:where
[?e :maker/name "BMW"]
[?c :car/maker ?e]
[?c :car/name ?name]]
@conn)
;; Another way of doing this
(let [car-entity (ffirst
(d/q '[:find ?c
:where
[?e :maker/name "BMW"]
[?c :car/maker ?e]]
@conn))]
(:car/name (d/entity @conn car-entity)))
@vojtatranta
Copy link

Wuuuf, could you please please add an example in JS too? Thanks!

@jtlocsei
Copy link

jtlocsei commented Sep 4, 2018

Thank you for writing the Datascript 101 tutorial and giving newcomers like me a non-daunting way to get into it. Much appreciated!

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