fix: get children classes' objects too for a parent class

refactor/add-page-type
Tienson Qin 2024-07-05 22:07:39 +08:00
parent f8894663cc
commit dc456c0c72
8 changed files with 39 additions and 48 deletions

View File

@ -374,7 +374,6 @@
[db property-id] [db property-id]
(:class/_schema.properties (d/entity db property-id))) (:class/_schema.properties (d/entity db property-id)))
(defn get-alias-source-page (defn get-alias-source-page
"return the source page (page-name) of an alias" "return the source page (page-name) of an alias"
[db alias-id] [db alias-id]
@ -540,3 +539,16 @@
(->> (d/datoms db :avet :block/type "property") (->> (d/datoms db :avet :block/type "property")
(map (fn [d] (map (fn [d]
(d/entity db (:e d)))))) (d/entity db (:e d))))))
(defn get-class-parents
[class]
(let [*classes (atom #{})]
(when-let [parent (:class/parent class)]
(loop [current-parent parent]
(when (and
current-parent
(contains? (:block/type parent) "class")
(not (contains? @*classes (:db/id parent))))
(swap! *classes conj (:db/id current-parent))
(recur (:class/parent current-parent)))))
@*classes))

View File

@ -336,18 +336,8 @@
(defn ^:api get-class-parents (defn ^:api get-class-parents
[tags] [tags]
(let [tags' (filter (fn [tag] (contains? (:block/type tag) "class")) tags) (let [tags' (filter (fn [tag] (contains? (:block/type tag) "class")) tags)]
*classes (atom #{})] (set (mapcat ldb/get-class-parents tags'))))
(doseq [tag tags']
(when-let [parent (:class/parent tag)]
(loop [current-parent parent]
(when (and
current-parent
(contains? (:block/type parent) "class")
(not (contains? @*classes (:db/id parent))))
(swap! *classes conj (:db/id current-parent))
(recur (:class/parent current-parent))))))
@*classes))
(defn ^:api get-class-properties (defn ^:api get-class-properties
[class] [class]

View File

@ -14,7 +14,7 @@
(defn- get-all-objects (defn- get-all-objects
[class] [class]
(->> (:block/_tags class) (->> (db-model/get-class-objects (state/get-current-repo) (:db/id class))
(map (fn [row] (assoc row :id (:db/id row)))))) (map (fn [row] (assoc row :id (:db/id row))))))
(defn- add-new-object! (defn- add-new-object!
@ -24,7 +24,7 @@
:properties {:block/tags (:db/id class)} :properties {:block/tags (:db/id class)}
:edit-block? false}) :edit-block? false})
set-data! (get-in table [:data-fns :set-data!]) set-data! (get-in table [:data-fns :set-data!])
_ (set-data! (get-all-objects (db/entity (:db/id class))))] _ (set-data! (get-all-objects class))]
(editor-handler/edit-block! (db/entity [:block/uuid (:block/uuid block)]) 0 :unknown-container))) (editor-handler/edit-block! (db/entity [:block/uuid (:block/uuid block)]) 0 :unknown-container)))
(rum/defc objects-inner < rum/static (rum/defc objects-inner < rum/static
@ -34,8 +34,8 @@
columns (views/build-columns config properties)] columns (views/build-columns config properties)]
(rum/use-effect! (rum/use-effect!
(fn [] (fn []
(p/let [_result (db-async/<get-tag-objects (state/get-current-repo) (:db/id view-entity))] (p/let [_result (db-async/<get-tag-objects (state/get-current-repo) (:db/id class))]
(set-data! (get-all-objects (db/entity (:db/id view-entity)))))) (set-data! (get-all-objects class))))
[object-ids]) [object-ids])
(views/view view-entity {:data data (views/view view-entity {:data data
:set-data! set-data! :set-data! set-data!
@ -46,7 +46,7 @@
[state class] [state class]
[:div.ml-2 [:div.ml-2
(let [config {:container-id (:container-id state)} (let [config {:container-id (:container-id state)}
object-ids (db-model/get-class-objects (state/get-current-repo) (:db/id class)) object-ids (map :db/id (db-model/get-class-objects (state/get-current-repo) (:db/id class)))
;; TODO: create the default table view for this class ;; TODO: create the default table view for this class
view-entity class] view-entity class]
(objects-inner config class view-entity object-ids))]) (objects-inner config class view-entity object-ids))])

View File

@ -338,8 +338,7 @@
(let [e (db/entity [:block/uuid id])] (let [e (db/entity [:block/uuid id])]
(when-not (= :logseq.class/Root (:db/ident e)) (when-not (= :logseq.class/Root (:db/ident e))
e))))) e)))))
(->> (model/get-class-objects repo (:db/id class)) (model/get-class-objects repo (:db/id class))))
(keep db/entity))))
classes) classes)
:else :else

View File

@ -244,12 +244,14 @@
(defn <get-tag-objects (defn <get-tag-objects
[graph tag-id] [graph tag-id]
(<q graph {:transact-db? true} (let [class-children (db-model/get-class-children graph tag-id)
'[:find [(pull ?b [*]) ...] class-ids (distinct (conj class-children tag-id))]
:in $ ?tag-id (<q graph {:transact-db? true}
:where '[:find [(pull ?b [*]) ...]
[?b :block/tags ?tag-id]] :in $ [?tag-id ...]
tag-id)) :where
[?b :block/tags ?tag-id]]
class-ids)))
(defn <get-tags (defn <get-tags
[graph] [graph]

View File

@ -781,28 +781,16 @@ independent of format as format specific heading characters are stripped"
(:class-parent rules/rules)) (:class-parent rules/rules))
distinct)) distinct))
;; FIXME: async query && use d/datoms instead of datalog
(defn get-class-objects (defn get-class-objects
[repo class-id] [repo class-id]
(when-let [class (db-utils/entity repo class-id)] (when-let [class (db-utils/entity repo class-id)]
(-> (react/q repo [:frontend.worker.react/objects (:db/id class)] (if (first (:class/_parent class)) ; has children classes
{:query-fn (fn [_] (let [all-classes (conj (->> (get-class-children repo class-id)
(when-let [class (db-utils/entity repo class-id)] (map #(db-utils/entity repo %)))
(if (first (:class/_parent class)) ; has children classes class)]
(d/q (->> (mapcat :block/_tags all-classes)
'[:find [?object ...] distinct))
:in $ % ?parent (:block/_tags class))))
:where
(class-parent ?parent ?c)
(or-join [?object ?c]
[?object :block/tags ?parent]
[?object :block/tags ?c])]
(conn/get-db repo)
(:class-parent rules/rules)
class-id)
(map :db/id (:block/_tags class)))))}
nil)
react)))
(defn get-all-namespace-relation (defn get-all-namespace-relation

View File

@ -42,7 +42,7 @@
_ (test-helper/create-page! "class1" opts) _ (test-helper/create-page! "class1" opts)
class (db/get-case-page "class1") class (db/get-case-page "class1")
_ (test-helper/save-block! repo fbid "Block 1" {:tags ["class1"]})] _ (test-helper/save-block! repo fbid "Block 1" {:tags ["class1"]})]
(is (= (model/get-class-objects repo (:db/id class)) (is (= (map :db/id (model/get-class-objects repo (:db/id class)))
[(:db/id (db/entity [:block/uuid fbid]))])) [(:db/id (db/entity [:block/uuid fbid]))]))
(testing "classes parent" (testing "classes parent"
@ -52,7 +52,7 @@
(db/transact! [{:db/id (:db/id class2) (db/transact! [{:db/id (:db/id class2)
:class/parent (:db/id class)}])) :class/parent (:db/id class)}]))
(test-helper/save-block! repo sbid "Block 2" {:tags ["class2"]}) (test-helper/save-block! repo sbid "Block 2" {:tags ["class2"]})
(is (= (model/get-class-objects repo (:db/id class)) (is (= (map :db/id (model/get-class-objects repo (:db/id class)))
[(:db/id (db/entity [:block/uuid fbid])) [(:db/id (db/entity [:block/uuid fbid]))
(:db/id (db/entity [:block/uuid sbid]))]))))) (:db/id (db/entity [:block/uuid sbid]))])))))

View File

@ -25,7 +25,7 @@
(docs-graph-helper/docs-graph-assertions db graph-dir (map :file/path files)) (docs-graph-helper/docs-graph-assertions db graph-dir (map :file/path files))
(testing "Additional Counts" (testing "Additional Counts"
(is (= 63706 (count (d/datoms db :eavt))) "Correct datoms count") (is (= 64476 (count (d/datoms db :eavt))) "Correct datoms count")
(is (= 5946 (is (= 5946
(ffirst (ffirst