forked from calherries/graft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.cljc
42 lines (38 loc) · 1.59 KB
/
demo.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(ns demo
(:require [core :as g]))
;; Add some data to an empty db
(def db (g/transact {} [:create
#:person{:id 1
:name "Fred"
:pets [[:animal/id 3]
[:animal/id 4]]}
#:person{:id 2
:name "Rich Hickey"}
#:animal{:id 3
:name "Catso"
:vet [[:person/id 2]]}
#:animal{:id 4
:name "Doggy"
:vet [[:person/id 2]]}]))
;; Recursive query
(g/query db {[:person/id 1] [:person/name
{:person/pets [:animal/name
{:animal/vet [:person/name]}]}]})
;; => [#:person{:name "Fred",
;; :pets [#:animal{:name "Catso", :vet (#:person{:name "Rich Hickey"})}
;; #:animal{:name "Doggy", :vet (#:person{:name "Rich Hickey"})}]}]
;; Add new facts to the database
(-> db
(g/transact [:merge [:person/id 1] {:person/name "Freddy"}])
(g/query {[:person/id 1] [:person/name]}))
;; => [#:person{:name "Freddy"}]
;; Remove new facts from the database
(-> db
(g/transact [:dissoc [:person/id 1] :person/name :person/pets])
(g/query {[:person/id 1] [:person/name :person/pets]}))
;; => [#:person{:name nil, :pets nil}]
;; Delete entities
(-> db
(g/transact [:delete [:person/id 1]])
(g/query {[:person/id 1] [:person/name]}))
;; => [#:person{:name nil}]