是否可以将实体和所有组件父项拉到树上?
反向递归拉。
如果没有,我该怎么做?
答案 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"}]}]}