2014-09-05 100 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" } ] ])))