我可以将一个实体及其父组件一直拉到组件树上吗?
Can I pull an entity and it's parent components all the way up the tree of components?
是否可以将一个实体和所有父组件一直拉到树上?
一个反向递归拉。
如果没有,我该怎么做?
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")))])
使用查询以递归方式拉取组件父项(注意“_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 "L1"}]}]}
是否可以将一个实体和所有父组件一直拉到树上?
一个反向递归拉。
如果没有,我该怎么做?
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")))])
使用查询以递归方式拉取组件父项(注意“_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 "L1"}]}]}