Browse Source

fix: query function for db graphs

Fixes logseq/db-test#203. Also removed unused helpers
Gabriel Horner 8 months ago
parent
commit
4a0225b7ee

+ 0 - 26
deps/db/src/logseq/db/frontend/property.cljs

@@ -683,22 +683,6 @@
   (or (:block/title ent)
       (:logseq.property/value ent)))
 
-(defn- ref->property-value-content
-  "Given a ref from a pulled query e.g. `{:db/id X}`, gets a readable name for
-  the property value of a ref type property"
-  [db ref]
-  (some->> (:db/id ref)
-           (d/entity db)
-           property-value-content))
-
-(defn ref->property-value-contents
-  "Given a ref or refs from a pulled query e.g. `{:db/id X}`, gets a readable
-  name for the property values of a ref type property"
-  [db ref]
-  (if (or (set? ref) (sequential? ref))
-    (set (map #(ref->property-value-content db %) ref))
-    (ref->property-value-content db ref)))
-
 (defn get-closed-value-entity-by-name
   "Given a property, finds one of its closed values by name or nil if none
   found. Works for all closed value types"
@@ -734,16 +718,6 @@
   [property]
   (= (:db/cardinality property) :db.cardinality/many))
 
-(defn properties-by-name
-  "Given a block from a query result, returns a map of its properties indexed by
-  property names"
-  [db block]
-  (->> (properties block)
-       (map (fn [[k v]]
-              [(:block/title (d/entity db k))
-               (ref->property-value-contents db v)]))
-       (into {})))
-
 (defn public-built-in-property?
   "Indicates whether built-in property can be seen and edited by users"
   [entity]

+ 2 - 3
src/main/frontend/commands.cljs

@@ -426,8 +426,7 @@
        ["Advanced Query" (advanced-query-steps) "Create an advanced query block" :icon/query]
        (when-not db?
          ["Zotero" (zotero-steps) "Import Zotero journal article" :icon/circle-letter-z])
-       (when-not db?
-         ["Query function" [[:editor/input "{{function }}" {:backward-pos 2}]] "Create a query function" :icon/queryCode])
+       ["Query function" [[:editor/input "{{function }}" {:backward-pos 2}]] "Create a query function" :icon/queryCode]
        ["Calculator"
         (calc-steps)
         "Insert a calculator" :icon/calculator]
@@ -474,7 +473,7 @@
         commands)
 
 ;; Allow user to modify or extend, should specify how to extend.
-
+      
       (state/get-commands)
       (when-let [plugin-commands (seq (some->> (state/get-plugins-slash-commands)
                                                (mapv #(vec (concat % [nil :icon/puzzle])))))]

+ 35 - 21
src/main/frontend/components/block/macros.cljs

@@ -3,14 +3,30 @@
   (:require [clojure.walk :as walk]
             [frontend.extensions.sci :as sci]
             [frontend.handler.common :as common-handler]
-            [frontend.handler.db-based.property.util :as db-pu]
             [goog.string :as gstring]
             [goog.string.format]
             [frontend.state :as state]
-            [frontend.config :as config]))
+            [frontend.config :as config]
+            [datascript.core :as d]
+            [logseq.db.frontend.property :as db-property]
+            [frontend.db.conn :as db-conn]))
+
+(defn- properties-by-name
+  "Given a block from a query result, returns a map of its properties indexed by
+  property names"
+  [db block]
+  (->> (db-property/properties block)
+       (map (fn [[k v]]
+              [(:block/title (d/entity db k))
+               ;; For now just support cardinality :one
+               (when-not (set? v)
+                 (some->> (:db/id v)
+                          (d/entity db)
+                          db-property/property-value-content))]))
+       (into {})))
 
 (defn- normalize-query-function
-  [ast repo result]
+  [ast* repo result]
   (let [ast (walk/prewalk
              (fn [f]
                (if (and (list? f)
@@ -25,26 +41,21 @@
                     (first f)
                     (list 'map (second f) 'result)))
                  f))
-             ast)]
+             ast*)
+        db-based-graph? (config/db-based-graph? repo)
+        ;; These keyword aliases should be the same as those used in the query-table for sorting
+        special-file-graph-keywords
+        {:block :block/title
+         :page :block/name
+         :created-at :block/created-at
+         :updated-at :block/updated-at}]
     (walk/postwalk
      (fn [f]
        (cond
          (keyword? f)
-         ;; These keyword aliases should be the same as those used in the query-table for sorting
-         (case f
-           :block
-           :block/title
-
-           :page
-           :block/name
-
-           :created-at
-           :block/created-at
-
-           :updated-at
-           :block/updated-at
-
-           (let [prop-key (if (config/db-based-graph? repo) (name f) f)
+         (if-let [kw (and (not db-based-graph?) (get special-file-graph-keywords f))]
+           kw
+           (let [prop-key (if db-based-graph? (name f) f)
                  vals (map #(get-in % [:block/properties prop-key]) result)
                  int? (some integer? vals)]
              `(~'fn [~'b]
@@ -65,9 +76,12 @@
                        (mapcat val query-result*)
                        query-result*)
         repo (state/get-current-repo)
+        db (db-conn/get-db repo)
         query-result' (if (config/db-based-graph? repo)
-                       (map #(assoc % :block/properties (db-pu/properties-by-name repo %)) query-result)
-                       query-result)
+                        (->> query-result
+                             (map #(d/entity db (:db/id %)))
+                             (map #(hash-map :block/properties (properties-by-name db %))))
+                        query-result)
         fn-string (-> (gstring/format "(fn [result] %s)" (first arguments))
                       (common-handler/safe-read-string "failed to parse function")
                       (normalize-query-function repo query-result')

+ 1 - 7
src/main/frontend/handler/db_based/property/util.cljs

@@ -12,16 +12,10 @@
     (db-property/property-value-content e)
     e))
 
-(defn properties-by-name
-  "Given a block from a query result, returns a map of its properties indexed by property names"
-  [repo block]
-  (let [db (conn/get-db repo)]
-    (db-property/properties-by-name db block)))
-
 (defn readable-properties
   "Given a DB graph's properties, returns a readable properties map with keys as
   property names and property values dereferenced where possible. Has some
-  overlap with db-property/properties-by-name"
+  overlap with block-macros/properties-by-name"
   ([properties] (readable-properties properties {:original-key? true}))
   ([properties {:keys [original-key? key-fn]
                 :or {key-fn identity}}]