Last active
September 4, 2018 19:50
-
-
Save verma/1be6a0ddba850eb0da437968cd5994aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
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
Wuuuf, could you please please add an example in JS too? Thanks!