Browse Source

chore: mv logseq.graph-parser.util.db to db dep

This ns is more appropriate in db and was only in graph-parser because
the date-time-util lib wasn't accessible to db until #10933 landed.
graph-parser dep is specific to file graphs and as much as possible
anything unrelated to this should not be in this dep
Gabriel Horner 1 year ago
parent
commit
26bc6f0d1d

+ 2 - 1
.clj-kondo/config.edn

@@ -135,6 +135,8 @@
              logseq.common.util.page-ref page-ref
              logseq.common.util.block-ref block-ref
              logseq.db ldb
+             logseq.db.frontend.content db-content
+             logseq.db.frontend.inputs db-inputs
              logseq.db.frontend.property db-property
              logseq.db.frontend.property.type db-property-type
              logseq.db.frontend.property.util db-property-util
@@ -147,7 +149,6 @@
              logseq.graph-parser.block gp-block
              logseq.graph-parser.mldoc gp-mldoc
              logseq.graph-parser.property gp-property
-             logseq.graph-parser.util.db db-util
              logseq.outliner.core outliner-core
              logseq.outliner.op outliner-op
              logseq.outliner.pipeline outliner-pipeline

+ 2 - 0
deps/db/.carve/ignore

@@ -8,3 +8,5 @@ logseq.db.frontend.rules/extract-rules
 logseq.db.frontend.property.type/type-or-closed-value?
 ;; Internal API
 logseq.db.frontend.rules/rules
+;; API
+logseq.db.frontend.inputs/resolve-input

+ 11 - 14
deps/graph-parser/src/logseq/graph_parser/util/db.cljs → deps/db/src/logseq/db/frontend/inputs.cljs

@@ -1,6 +1,5 @@
-(ns logseq.graph-parser.util.db
-  "Db util fns that are useful for the frontend and nbb-logseq. This may be used
-  by the graph-parser soon but if not, it should be in its own library"
+(ns logseq.db.frontend.inputs
+  "Handles :inputs in queries"
   (:require [cljs-time.core :as t]
             [logseq.common.util.date-time :as date-time-util]
             [logseq.common.util :as common-util]
@@ -17,9 +16,7 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
   ([date hours mins secs millisecs]
    (.setHours (js/Date. date) hours mins secs millisecs)))
 
-(def date->int date-time-util/date->int)
-
-(defn old->new-relative-date-format [input]
+(defn- old->new-relative-date-format [input]
   (let [count (re-find #"^\d+" (name input))
         plus-minus (if (re-find #"after" (name input)) "+" "-")
         ms? (string/ends-with? (name input) "-ms")]
@@ -33,11 +30,11 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
   (old->new-relative-date-format "1d-after-ms")
   (old->new-relative-date-format "1w-after-ms"))
 
-(defn get-relative-date [input]
+(defn- get-relative-date [input]
   (case (or (namespace input) "today")
     "today" (t/today)))
 
-(defn get-offset-date [relative-date direction amount unit]
+(defn- get-offset-date [relative-date direction amount unit]
   (let [offset-fn (case direction "+" t/plus "-" t/minus)
         offset-amount (parse-long amount)
         offset-unit-fn (case unit
@@ -47,7 +44,7 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
                          "y" t/years)]
     (offset-fn (offset-fn relative-date (offset-unit-fn offset-amount)))))
 
-(defn get-ts-units
+(defn- get-ts-units
   "There are currently several time suffixes being used in inputs:
   - ms: milliseconds, will return a time relative to the direction the date is being adjusted
   - start: will return the time at the start of the day [00:00:00.000]
@@ -69,7 +66,7 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
        (min 59  (parse-long (str s1 s2)))
        (min 999 (parse-long (str ms1 ms2 ms3)))])))
 
-(defn keyword-input-dispatch [input]
+(defn- keyword-input-dispatch [input]
   (cond
     (#{:current-page :query-page :current-block :parent-block :today :yesterday :tomorrow :right-now-ms} input) input
 
@@ -101,13 +98,13 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
     (:db/id (:block/parent (d/entity db [:block/uuid current-block-uuid])))))
 
 (defmethod resolve-keyword-input :today [_ _ _]
-  (date->int (t/today)))
+  (date-time-util/date->int (t/today)))
 
 (defmethod resolve-keyword-input :yesterday [_ _ _]
-  (date->int (t/minus (t/today) (t/days 1))))
+  (date-time-util/date->int (t/minus (t/today) (t/days 1))))
 
 (defmethod resolve-keyword-input :tomorrow [_ _ _]
-  (date->int (t/plus (t/today) (t/days 1))))
+  (date-time-util/date->int (t/plus (t/today) (t/days 1))))
 
 (defmethod resolve-keyword-input :right-now-ms [_ _ _]
   (common-util/time-ms))
@@ -125,7 +122,7 @@ it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00."
   (let [relative-to (get-relative-date input)
         [_ offset-direction offset offset-unit] (re-find #"^([+-])(\d+)([dwmy])$" (name input))
         offset-date (get-offset-date relative-to offset-direction offset offset-unit)]
-    (date->int offset-date)))
+    (date-time-util/date->int offset-date)))
 
 ;; relative-date-time returns an epoch int
 (defmethod resolve-keyword-input :relative-date-time [_ input _]

+ 0 - 2
deps/graph-parser/.carve/ignore

@@ -7,8 +7,6 @@ logseq.graph-parser.mldoc/link?
 ;; API
 logseq.graph-parser/get-blocks-to-delete
 ;; API
-logseq.graph-parser.util.db/resolve-input
-;; API
 logseq.graph-parser.text/get-file-basename
 ;; API
 logseq.graph-parser.mldoc/mldoc-link?

+ 3 - 3
src/main/frontend/db/model.cljs

@@ -15,8 +15,8 @@
             [logseq.db.frontend.rules :as rules]
             [logseq.db.frontend.content :as db-content]
             [logseq.graph-parser.text :as text]
-            [logseq.graph-parser.util.db :as db-util]
             [logseq.common.util :as common-util]
+            [logseq.common.util.date-time :as date-time-util]
             [frontend.config :as config]
             [logseq.db :as ldb]))
 
@@ -601,7 +601,7 @@ independent of format as format specific heading characters are stripped"
 
 (defn get-journals-length
   []
-  (let [today (db-util/date->int (js/Date.))]
+  (let [today (date-time-util/date->int (js/Date.))]
     (d/q '[:find (count ?page) .
            :in $ ?today
            :where
@@ -618,7 +618,7 @@ independent of format as format specific heading characters are stripped"
    (when (conn/get-db repo-url)
      (let [date (js/Date.)
            _ (.setDate date (- (.getDate date) (dec n)))
-           today (db-util/date->int (js/Date.))]
+           today (date-time-util/date->int (js/Date.))]
        (->>
         (react/q repo-url [:frontend.worker.react/journals] {:use-cache? false}
                  '[:find [(pull ?page [*]) ...]

+ 5 - 5
src/main/frontend/db/query_dsl.cljs

@@ -12,10 +12,10 @@
             [frontend.db.utils :as db-utils]
             [frontend.db.conn :as conn]
             [datascript.core :as d]
-            [logseq.graph-parser.util.db :as db-util]
             [logseq.db.frontend.rules :as rules]
             [frontend.template :as template]
             [logseq.graph-parser.text :as text]
+            [logseq.common.util.date-time :as date-time-util]
             [logseq.common.util.page-ref :as page-ref]
             [logseq.common.util :as common-util]
             [frontend.util.text :as text-util]
@@ -57,13 +57,13 @@
   (let [input (string/lower-case (name input))]
     (cond
       (= "today" input)
-      (db-util/date->int (t/today))
+      (date-time-util/date->int (t/today))
 
       (= "yesterday" input)
-      (db-util/date->int (t/yesterday))
+      (date-time-util/date->int (t/yesterday))
 
       (= "tomorrow" input)
-      (db-util/date->int (t/plus (t/today) (t/days 1)))
+      (date-time-util/date->int (t/plus (t/today) (t/days 1)))
 
       (page-ref/page-ref? input)
       (let [input (-> (page-ref/get-page-name input)
@@ -80,7 +80,7 @@
                  "m" t/months
                  "w" t/weeks
                  t/days)]
-        (db-util/date->int (t/plus (t/today) (tf duration)))))))
+        (date-time-util/date->int (t/plus (t/today) (tf duration)))))))
 
 (defn- ->timestamp [input]
   (let [input (string/lower-case (name input))]

+ 9 - 9
src/main/frontend/db/query_react.cljs

@@ -10,24 +10,24 @@
             [frontend.debug :as debug]
             [frontend.extensions.sci :as sci]
             [frontend.state :as state]
-            [logseq.graph-parser.util.db :as db-util]
+            [logseq.db.frontend.inputs :as db-inputs]
             [logseq.common.util.page-ref :as page-ref]
             [frontend.util :as util]
             [frontend.date :as date]
             [lambdaisland.glogi :as log]))
 
 (defn resolve-input
-  "Wrapper around db-util/resolve-input which provides editor-specific state"
+  "Wrapper around db-inputs/resolve-input which provides editor-specific state"
   ([db input]
    (resolve-input db input {}))
   ([db input opts]
-   (db-util/resolve-input db
-                          input
-                          (merge {:current-page-fn (fn []
-                                                     (or (state/get-current-page)
-                                                         (:page (state/get-default-home))
-                                                         (date/today)))}
-                                 opts))))
+   (db-inputs/resolve-input db
+                            input
+                            (merge {:current-page-fn (fn []
+                                                       (or (state/get-current-page)
+                                                           (:page (state/get-default-home))
+                                                           (date/today)))}
+                                   opts))))
 
 (defn custom-query-result-transform
   [query-result remove-blocks q]

+ 12 - 11
src/test/frontend/db/query_react_test.cljs

@@ -4,7 +4,7 @@
             [clojure.string :as string]
             [frontend.state :as state]
             [frontend.date :as date]
-            [logseq.graph-parser.util.db :as db-util]
+            [logseq.db.frontend.inputs :as db-inputs]
             [frontend.test.helper :as test-helper :refer [load-test-files]]
             [frontend.db.query-custom :as query-custom]
             [frontend.db.utils :as db-utils]
@@ -64,6 +64,7 @@ adds rules that users often use"
                                                      [?t :block/name ?tag-name]]}
                                     {:current-page-fn (constantly current-page)})))
 
+;; TODO: Move most resolve-input tests to deps/db when a load-test-files helper is available for deps
 (deftest resolve-input-for-page-and-block-inputs
   (load-test-files [{:file/path "pages/page1.md"
                      :file/content
@@ -183,16 +184,16 @@ created-at:: %s
 created-at:: %s
 - +1y
 created-at:: %s"
-                                                   (db-util/date-at-local-ms (t/minus (t/today) (t/years 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/minus (t/today) (t/months 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/minus (t/today) (t/weeks 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/minus (t/today) (t/days 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/today) 12 0 0 0)
-                                                   (db-util/date-at-local-ms (t/today) 18 0 0 0)
-                                                   (db-util/date-at-local-ms (t/plus (t/today) (t/days 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/plus (t/today) (t/weeks 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/plus (t/today) (t/months 1)) 0 0 0 0)
-                                                   (db-util/date-at-local-ms (t/plus (t/today) (t/years 1)) 0 0 0 0))}])
+                                                   (db-inputs/date-at-local-ms (t/minus (t/today) (t/years 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/minus (t/today) (t/months 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/minus (t/today) (t/weeks 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/minus (t/today) (t/days 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/today) 12 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/today) 18 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/plus (t/today) (t/days 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/plus (t/today) (t/weeks 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/plus (t/today) (t/months 1)) 0 0 0 0)
+                                                   (db-inputs/date-at-local-ms (t/plus (t/today) (t/years 1)) 0 0 0 0))}])
 
   (is (= ["today" "tonight"] (blocks-created-between-inputs :-0d-ms :+0d-ms))
       ":+0d-ms and :-0d-ms resolve to correct datetime range")