Create a gist now

Instantly share code, notes, and snippets.

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
[?e :maker/name "BMW"]
[?c :car/maker ?e]
[?c :car/name ?name]]
;; Another way of doing this
(let [car-entity (ffirst
(d/q '[:find ?c
[?e :maker/name "BMW"]
[?c :car/maker ?e]]
(:car/name (d/entity @conn car-entity)))

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

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