Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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

This comment has been minimized.

Show comment
Hide comment
@vojtatranta

vojtatranta Jul 2, 2016

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

vojtatranta commented Jul 2, 2016

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

@jtlocsei

This comment has been minimized.

Show comment
Hide comment
@jtlocsei

jtlocsei 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!

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