【问题标题】:How to get all ref attribute values?如何获取所有 ref 属性值?
【发布时间】:2014-09-05 00:24:16
【问题描述】:

touchentity 并获得许多实体 ID。在保持嵌套结构的同时,我想要所有属性值而不是 id。

(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 "someemail@gmail.com",
    :db/id 17592186045429}

考虑过使用类似简单地触摸所有实体 ID 的方法,但如果我想要所有实体 ID,似乎复杂性会上升:

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

不确定惯用的方法是什么:通过查询端或通过 clojure 找到一种方法来做更多的事情。

【问题讨论】:

    标签: clojure datomic


    【解决方案1】:

    在 Datomic 中执行此操作的惯用方式是在架构中声明组件。 touch 将触及实体的所有属性,包括递归的任何组件

    【讨论】:

      【解决方案2】:

      您可能希望为此使用the Datomic Pull API。它可以递归地返回用户指定为“组件”的所有子实体的属性/值对。一个例子:

        (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"   } ] ] )))
      

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-15
        • 2023-04-01
        • 2020-02-16
        相关资源
        最近更新 更多