Skip to content

Instantly share code, notes, and snippets.

@matthavener
Created August 2, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthavener/4e61cf3db97fde90cde56af0d556ba6b to your computer and use it in GitHub Desktop.
Save matthavener/4e61cf3db97fde90cde56af0d556ba6b to your computer and use it in GitHub Desktop.
datomic isComponent
(do
(d/delete-database "datomic:mem://test-is-component")
(d/create-database "datomic:mem://test-is-component")
(let [conn (d/connect "datomic:mem://test-is-component")
db (-> (d/transact conn [{:db/ident :dev/ref,
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/isComponent true}])
deref
:db-after)
{:keys [db-after tempids]} (d/with db [{:db/id "parent1"
:db/doc "parent1"
:dev/ref "child"}
{:db/id "parent2"
:db/doc "parent2"}
{:db/id "child"
:db/doc "child"}
])
db-a db-after
; move child to parent2?
db-b (:db-after (d/with db-a [[:db/add
(get tempids "parent2")
:dev/ref
(get tempids "child")]]))]
; child's parent is still parent1
(assert (= (:dev/_ref (d/entity db-b (get tempids "child")))
(d/entity db-b (get tempids "parent1"))))
; parent1 and parent2 both "own" child
(assert (= (:dev/ref (d/entity db-b (get tempids "parent1")))
(:dev/ref (d/entity db-b (get tempids "parent2")))))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment