How to update / overwrite ref attribute with lots of data in datomic?

Suppose I have a schema that includes an attribute :x/valuewhere :x/valueis a component, is ref and has the power of many. The schema also has id for x :x/id.

Now let's say I say that I am doing the following:

(d/transact conn [{:x/id "1234" :x/value [{:text "test"}]}])

Then later I want to update the value, which means I want to replace :x/value, so in the end I have an object like:

{:db/id <some eid>
 :x/id "1234"
 :x/value [{:text "replacement"}]}

How should I do it?

So far I have tried the following:

(d/transact conn [{:x/id "1234" :x/value [{:text "replacement"}]}])

But it just added a new ref, so I got an object similar to:

{:db/id <some eid>
 :x/id "1234"
 :x/value [{:text "test"} {:text "replacement"}]}

, , , : , x.

, . ?

+4
1

, :

[:db/retract  entity-id attribute old-value]
[:db/add      entity-id attribute new-value]

. http://docs.datomic.com/transactions.html

Tupelo Datomic. :

  (td/transact *conn* ;   required              required              zero-or-more
                      ;  <attr name>         <attr value type>       <optional specs ...>
    (td/new-attribute   :person/name         :db.type/string         :db.unique/value)      ; each name      is unique
    (td/new-attribute   :person/secret-id    :db.type/long           :db.unique/value)      ; each secret-id is unique
    (td/new-attribute   :weapon/type         :db.type/ref            :db.cardinality/many)  ; one may have many weapons
    (td/new-attribute   :location            :db.type/string)     ; all default values
    (td/new-attribute   :favorite-weapon     :db.type/keyword ))  ; all default values

, . .

(td/transact *conn* 
  (td/retract-value james-eid :weapon/type :weapon/knife))
(is (= (td/entity-map (live-db) james-eid)  ; lookup by EID 
       {:person/name "James Bond" :location "London" :weapon/type #{:weapon/wit :weapon/gun} :person/secret-id 7 } ))

, ( , ) .

; We see that Dr No is in the DB...
(let [tuple-set   (td/find  :let    [$ (live-db)]
                            :find   [?name ?loc] ; <- shape of output tuples
                            :where  {:person/name ?name :location ?loc} ) ]
  (is (= tuple-set #{ ["James Bond"     "London"]
                      ["M"              "London"]
                      ["Dr No"          "Caribbean"]
                      ["Honey Rider"    "Caribbean"] } )))
; we do the retraction...
(td/transact *conn*
  (td/retract-entity [:person/name "Dr No"] ))
; ...and now he gone!
(let [tuple-set   (td/find  :let    [$ (live-db)]
                            :find   [?name ?loc]
                            :where  {:person/name ?name :location ?loc} ) ]
  (is (= tuple-set #{ ["James Bond"     "London"]
                      ["M"              "London"]
                      ["Honey Rider"    "Caribbean"] } )))

:

native datomic , , Tupelo:

; Dr No is no match for James. He gives up trying to use guile...
; Remove it using native Datomic.
(spy :before (td/entity-map (live-db) [:person/name "Dr No"]))
(d/transact *conn*
            [[:db/retract [:person/name "Dr No"] :weapon/type :weapon/guile]])
(is (= (spy :after (td/entity-map (live-db) [:person/name "Dr No"])) ; LookupRef
       {:person/name "Dr No"
        :location "Caribbean"
        :weapon/type #{:weapon/knife :weapon/gun}}))

:before => {:person/name "Dr No",
            :weapon/type #{:weapon/guile :weapon/knife :weapon/gun},
            :location "Caribbean"}
:after  => {:person/name "Dr No",
            :weapon/type #{:weapon/knife :weapon/gun},
            :location "Caribbean"}

# 2:

: , :arb/value [{:db/id 17592186045435, :content/text "tester"}], 1. , : weapon/type - N . , API pull Datomic. .

, -. , , :

(defn get-bond-girl-names []
    (let [result-pull (d/pull (live-db) [:bond-girl] [:person/name "James Bond"])
          bond-girl-names (forv [girl-entity (grab :bond-girl result-pull) ]
                               (grab :person/name (td/entity-map (live-db) (grab :db/id girl-entity))))
          ]
      bond-girl-names))

  (td/transact *conn*
    (td/new-attribute :bond-girl :db.type/ref :db.cardinality/many))  ; there are many Bond girls

  (let [tx-result          @(td/transact *conn*
                              (td/new-entity {:person/name "Sylvia Trench"})
                              (td/new-entity {:person/name "Tatiana Romanova"})
                              (td/new-entity {:person/name "Pussy Galore"})
                              (td/new-entity {:person/name "Bibi Dahl"})
                              (td/new-entity {:person/name "Octopussy"})
                              (td/new-entity {:person/name "Paris Carver"})
                              (td/new-entity {:person/name "Christmas Jones"}))
        tx-datoms          (td/tx-datoms (live-db) tx-result)
        girl-datoms        (vec (remove #(= :db/txInstant (grab :a %)) tx-datoms))
        girl-eids          (mapv :e girl-datoms)
        txr-2              (td/transact *conn*
                             (td/update [:person/name "James Bond"] ; update using a LookupRef
                               {:bond-girl girl-eids})
                             (td/update [:person/name "James Bond"] ; don't forget to add Honey Rider!
                               {:bond-girl #{[:person/name "Honey Rider"]}}))

  ]
    (is (= (get-bond-girl-names)
          ["Sylvia Trench" "Tatiana Romanova" "Pussy Galore" "Bibi Dahl"
           "Octopussy" "Paris Carver" "Christmas Jones" "Honey Rider"]))
    ; Suppose Bibi Dahl is just not refined enough for James. Give her a demotion.
    (td/transact *conn*
      (td/retract-value [:person/name "James Bond"] :bond-girl [:person/name "Bibi Dahl"]))

    (newline)
    (is (= (get-bond-girl-names)  ; Note that Bibi Dahl is no longer listed
          ["Sylvia Trench" "Tatiana Romanova" "Pussy Galore"
           "Octopussy" "Paris Carver" "Christmas Jones" "Honey Rider"] ))
    )

, LookupRef [:person/name "Honey Rider"], :person/name :db.unique/value. :content/text :db.unique/value, EID, .

+4

Source: https://habr.com/ru/post/1672241/


All Articles