Skip to content

Instantly share code, notes, and snippets.

@robert-stuttaford
Created May 26, 2015 19:12
Show Gist options
  • Select an option

  • Save robert-stuttaford/077198b368006acf0454 to your computer and use it in GitHub Desktop.

Select an option

Save robert-stuttaford/077198b368006acf0454 to your computer and use it in GitHub Desktop.
Testing behaviour of Datomic's :db/isComponent
(ns scratch
(:require [datomic.api :as d]))
(def u "datomic:mem://component-test")
(d/delete-database u)
(d/create-database u)
(def c (d/connect u))
(def s [{:db/id #db/id[:db.part/db]
:db/ident :some/component
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/isComponent true
:db.install/_attribute :db.part/db}])
@(d/transact c s)
(def es (let [sub #db/id[:db.part/user]]
[{:db/id #db/id[:db.part/user] :db/doc "Owner One" :some/component sub}
{:db/id #db/id[:db.part/user] :db/doc "Owner Two" :some/component sub}
{:db/id sub :db/doc "The Sub"}]))
@(d/transact c es)
(def db (d/db c))
(defn find-by-doc [db doc]
(some->> (d/q '[:find ?e . :in $ ?doc :where [?e :db/doc ?doc]] db doc)
(d/entity db)))
(def before (map d/touch
(remove nil?
[(find-by-doc db "Owner One")
(find-by-doc db "Owner Two")
(find-by-doc db "The Sub")])))
(def r [[:db.fn/retractEntity (:db/id (find-by-doc db "Owner One"))]])
@(d/transact c r)
(def db2 (d/db c))
(def after (map d/touch
(remove nil?
[(find-by-doc db2 "Owner One")
(find-by-doc db2 "Owner Two")
(find-by-doc db2 "The Sub")])))
[before after]
;; [({:db/doc "Owner One",
;; :some/component {:db/doc "The Sub", :db/id 17592186045420},
;; :db/id 17592186045418}
;; {:db/doc "Owner Two",
;; :some/component {:db/doc "The Sub", :db/id 17592186045420},
;; :db/id 17592186045419}
;; {:db/doc "The Sub", :db/id 17592186045420})
;; ({:db/doc "Owner Two", :db/id 17592186045419})]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment