我可以将一个实体及其父组件从组件树中拉出来吗?

时间:2016-12-01 11:05:15

标签: clojure datomic datalog

是否可以将实体和所有组件父项拉到树上?

反向递归拉。

如果没有,我该怎么做?

1 个答案:

答案 0 :(得分:2)

Datomic pull表达式支持反向属性导航。假设您的架构是这样的:

(d/transact conn [{:db/id                 (d/tempid :db.part/db)
                   :db/ident              :node/children
                   :db/valueType          :db.type/ref
                   :db/cardinality        :db.cardinality/many
                   :db.install/_attribute :db.part/db}])

创建树:

(defn node [name & children]
  (cond-> {:db/id  (d/tempid :db.part/user)
           :db/doc name}
    children (assoc :node/children children)))

(d/transact conn [(node "L1"
                        (node "L1.1"
                              (node "L1.1.1")
                              (node "L1.1.2"))
                        (node "L1.2"
                              (node "L1.2.1")))])

使用query以递归方式提取组件父项(请注意" _children"):

(d/q '[:find (pull ?e [:db/doc {:node/_children ...}]) .
       :where [?e :db/doc "L1.2.1"]]
     (d/db conn)) 

=> {:db / doc" L1.2.1",:node / _children [{:db / doc" L1.2",:node / _children [{:db / doc&# 34; LI"}]}]}