2014-09-05 98 views
0

I touchentity並獲得許多實體ID。我需要所有的屬性值而不是id,同時保持嵌套結構。如何獲取所有ref屬性值?

(d/touch (d/entity (get-db) (ffirst (find-all-families)))) 
=> {:family/parent #{{:db/id 17592186045423} 
        {:db/id 17592186045424} 
        {:db/id 17592186045426} 
        {:db/id 17592186045427}}, 
    :family/child #{{:db/id 17592186045420} 
        {:db/id 17592186045421}}, 
    :family/address {:db/id 17592186045428}, 
    :family/email "[email protected]", 
    :db/id 17592186045429} 

想過用類似簡單的觸摸所有的實體ID,但好像複雜性向上蔓延,如果我想所有的人:

(map d/touch (:family/parent (d/touch (d/entity (get-db) (ffirst (find-all-families)))))) 

不知道什麼是慣用的方法是:找到一種方法,通過查詢或通過clojure進行更多操作。

回答

0

在Datomic中這樣做的慣用方法是在模式中聲明組件。 touch將觸及該實體的所有屬性,包括遞歸的任何組件

0

您可能希望使用the Datomic Pull API來實現此目的。它可以遞歸地返回用戶指定爲「組件」的所有子實體的attr/value對。舉例:

(def dark-side-of-the-moon [:release/gid #uuid "24824319-9bb8-3d1e-a2c5-b8b864dafd1b"]) 

    (d/pull db [:release/media] dark-side-of-the-moon) 

    ; result 
    {:release/media 
    [{:db/id 17592186121277, 
    :medium/format {:db/id 17592186045741}, 
    :medium/position 1, 
    :medium/trackCount 10, 
    :medium/tracks 
    [{:db/id 17592186121278, 
     :track/duration 68346, 
     :track/name "Speak to Me", 
     :track/position 1, 
     :track/artists [{:db/id 17592186046909}]} 
     {:db/id 17592186121279, 
     :track/duration 168720, 
     :track/name "Breathe", 
     :track/position 2, 
     :track/artists [{:db/id 17592186046909}]} 
     {:db/id 17592186121280, 
     :track/duration 230600, 
     :track/name "On the Run", 
     :track/position 3, 
     :track/artists [{:db/id 17592186046909}]} 
     ...]}]} 

您也可以使用the Tupelo Datomic Pull API,我認爲這更好。舉個例子:

; If you wish to retain duplicate results on output, you must use td/query-pull and the Datomic 
    ; Pull API to return a list of results (instead of a set). 
    (let [result-pull  (td/query-pull :let [$ (live-db)]     ; $ is the implicit db name 
             :find [ (pull ?eid [:location]) ] ; output :location for each ?eid found 
             :where [ [?eid :location] ])  ; find any ?eid with a :location attr 
     result-sort  (sort-by #(-> % first :location) result-pull) 
    ] 
    (is (s/validate [ts/TupleMap] result-pull)) ; a list of tuples of maps 
    (is (= result-sort [ [ {:location "Caribbean"} ] 
          [ {:location "London" } ] 
          [ {:location "London" } ] ])))