如何在datomic中更新/覆盖带基数的ref属性?

fra*_*ure 6 clojure datomic

假设我有一个包含属性的模式:x/value,其中:x/value是一个组件,是一个ref,并且基数很多.架构还有一个x的id :x/id.

现在让我们说我说我做了以下事情:

(d/transact conn [{:x/id "1234" :x/value [{:text "test"}]}])
Run Code Online (Sandbox Code Playgroud)

然后我想更新值,这意味着我想要替换:x/value,所以最后我有一个像这样的实体:

{:db/id <some eid>
 :x/id "1234"
 :x/value [{:text "replacement"}]}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

到目前为止,我尝试过以下方法:

(d/transact conn [{:x/id "1234" :x/value [{:text "replacement"}]}])
Run Code Online (Sandbox Code Playgroud)

但这只是添加了一个新的参考,所以我有一个实体看起来像:

{:db/id <some eid>
 :x/id "1234"
 :x/value [{:text "test"} {:text "replacement"}]}
Run Code Online (Sandbox Code Playgroud)

我认为,实现我想要的方法的一种方法是通过实体id手动收回两个:text属性,然后为x实体执行新的添加事务.

但我想知道是否有更好的方法来做到这一点.有任何想法吗?

Ala*_*son 5

您需要收回旧值,然后使用新值更新它:

[:db/retract  entity-id attribute old-value]
[:db/add      entity-id attribute new-value]
Run Code Online (Sandbox Code Playgroud)

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

您可以在Tupelo Datomic的James Bond示例中看到更多详细信息。这是创建属性的方式:

  (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
Run Code Online (Sandbox Code Playgroud)

假设詹姆斯把刀扔向小人。我们需要将其从数据库中删除。

(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 } ))
Run Code Online (Sandbox Code Playgroud)

詹姆斯击败No博士后,我们需要从数据库中删除他(及其所有物品)。

; 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's 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"] } )))
Run Code Online (Sandbox Code Playgroud)

更新:本机Datomic解决方案

使用原生的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"}
Run Code Online (Sandbox Code Playgroud)

更新#2:

旁注:我注意到您的示例显示了:arb/value [{:db/id 17592186045435, :content/text "tester"}],这是一个长度为1的映射列表。这与我的示例不同::weapon / type只是一组N个项目。此输出差异是因为您正在使用pullDatomic 的API。但是,这不会影响您的原始问题。

众所周知,这些年来詹姆斯有很多邦德女郎。这是一个如何添加一些蜂蜜并将它们降级的例子:

(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"] ))
    )
Run Code Online (Sandbox Code Playgroud)

请注意,由于该属性具有,因此您只能使用LookupReflike 。如果不是,则必须使用EID将其与父实体分离。[:person/name "Honey Rider"]:person/name:db.unique/value:content/text:db.unique/value