Skip to content

Instantly share code, notes, and snippets.

@jballanc
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jballanc/e31f1a7f9d813550fd91 to your computer and use it in GitHub Desktop.
Save jballanc/e31f1a7f9d813550fd91 to your computer and use it in GitHub Desktop.
Nested datomic components
(use '[datomic.api :only [q db] :as d])
(use 'clojure.pprint)
(def uri "datomic:mem://seattle")
(d/create-database uri)
(def conn (d/connect uri))
(def schema-tx
[{:db/id #db/id[:db.part/db]
:db/ident :author/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :author/post
:db/valueType :db.type/ref
:db/isComponent true
:db/cardinality :db.cardinality/many
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :post/title
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :post/comment
:db/valueType :db.type/ref
:db/isComponent true
:db/cardinality :db.cardinality/many
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :comment/text
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}])
@(d/transact conn schema-tx)
@(d/transact conn [{:db/id #db/id [:db.part/user -1]
:author/name "Joe"
:author/post [{:db/id #db/id [:db.part/user -2]
:post/title "First"
:post/comment [{:db/id #db/id [:db.part/user -3]
:comment/text "1st"}
{:db/id #db/id [:db.part/user -4]
:comment/text "2nd"}]}
{:db/id #db/id [:db.part/user -5]
:post/title "2nd"
:post/comment [{:db/id #db/id [:db.part/user -6]
:comment/text "first"}
{:db/id #db/id [:db.part/user -7]
:comment/text "second"}]}]}])
(def author
(d/entity
(db conn)
(ffirst
(d/q '[:find ?a
:where
[?a :author/name "Joe"]]
(db conn)))))
(d/touch author)
;; => {:author/name "Joe",
;; :author/post #{{:post/title "2nd",
;; :post/comment #{{:comment/text "first",
;; :db/id 17592186045423}
;; {:comment/text "second",
;; :db/id 17592186045424}},
;; :db/id 17592186045422}
;; {:post/title "First",
;; :post/comment #{{:comment/text "2nd",
;; :db/id 17592186045421}
;; {:comment/text "1st",
;; :db/id 17592186045420}},
;; :db/id 17592186045419}},
;; :db/id 17592186045418}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment