瀏覽代碼

Move handler.common.repo fn into sqlite-util

This makes create-graph* namespaces independent of frontend
as they should be to be used externally
Gabriel Horner 2 年之前
父節點
當前提交
c07da6cc80

+ 35 - 2
deps/db/src/logseq/db/sqlite/util.cljs

@@ -2,8 +2,11 @@
   "Utils fns for backend sqlite db"
   (:require [cljs-time.coerce :as tc]
             [cljs-time.core :as t]
+            [clojure.string :as string]
             [cognitect.transit :as transit]
-            [logseq.db.schema :as db-schema]))
+            [datascript.core :as d]
+            [logseq.db.schema :as db-schema]
+            [logseq.db.property :as db-property]))
 
 (defn- type-of-block
   "
@@ -66,4 +69,34 @@
                (assoc block :block/updated-at updated-at)
                 (nil? (:block/created-at block))
                 (assoc :block/created-at updated-at))]
-    block))
+    block))
+
+(defn sanitize-page-name
+  "Prepares a string for insertion to :block/name. Not using
+  gp-util/page-name-sanity-lc yet because it's unclear if db graphs have all the
+  same naming constraints"
+  [s]
+  (string/lower-case s))
+
+(defn build-db-initial-data
+  [config-content]
+  (let [initial-files [{:block/uuid (d/squuid)
+                        :file/path (str "logseq/" "config.edn")
+                        :file/content config-content}
+                       {:block/uuid (d/squuid)
+                        :file/path (str "logseq/" "custom.css")
+                        :file/content ""}
+                       {:block/uuid (d/squuid)
+                        :file/path (str "logseq/" "custom.js")
+                        :file/content ""}]
+        default-properties (map
+                            (fn [[k-keyword {:keys [schema original-name]}]]
+                              (let [k-name (name k-keyword)]
+                                (block-with-timestamps
+                                 {:block/schema schema
+                                  :block/original-name (or original-name k-name)
+                                  :block/name (sanitize-page-name k-name)
+                                  :block/uuid (d/squuid)
+                                  :block/type "property"})))
+                            db-property/built-in-properties)]
+    (concat initial-files default-properties)))

+ 1 - 4
scripts/nbb.edn

@@ -5,7 +5,4 @@
   logseq/outliner
   {:local/root "../deps/outliner"}
   logseq/publishing
-  {:local/root "../deps/publishing"}
-  logseq/frontend
-  {:local/root ".."
-   :deps/root "src/main"}}}
+  {:local/root "../deps/publishing"}}}

+ 5 - 7
scripts/src/logseq/tasks/db_graph/create_graph.cljs

@@ -11,9 +11,7 @@
             [datascript.core :as d]
             ["fs" :as fs]
             ["path" :as node-path]
-            [nbb.classpath :as cp]
-            ;; TODO: Move this namespace to more stable deps/ namespaces
-            [frontend.handler.common.repo :as repo-common-handler]))
+            [nbb.classpath :as cp]))
 
 (defn- find-on-classpath [rel-path]
   (some (fn [dir]
@@ -29,7 +27,7 @@
   (let [config-content (or (some-> (find-on-classpath "templates/config.edn") fs/readFileSync str)
                            (do (println "Setting graph's config to empty since no templates/config.edn was found.")
                                "{}"))]
-    (d/transact! conn (repo-common-handler/build-db-initial-data config-content))))
+    (d/transact! conn (sqlite-util/build-db-initial-data config-content))))
 
 (defn init-conn
   "Create sqlite DB, initialize datascript connection and sync listener and then
@@ -86,7 +84,7 @@
                             (into {}))
         page-uuids (->> pages-and-blocks
                         (map :page)
-                        (map (juxt #(or (:block/name %) (string/lower-case (:block/original-name %)))
+                        (map (juxt #(or (:block/name %) (sqlite-util/sanitize-page-name (:block/original-name %)))
                                    :block/uuid))
                         (into {}))
         block-uuids (->> pages-and-blocks
@@ -173,7 +171,7 @@
                                           :block/schema (merge {:type :default}
                                                                (get-in properties [prop-name :block/schema]))
                                           :block/original-name (name prop-name)
-                                          :block/name (string/lower-case (name prop-name))
+                                          :block/name (sqlite-util/sanitize-page-name (name prop-name))
                                           :block/type "property"
                                           :block/created-at created-at
                                           :block/updated-at created-at}
@@ -191,7 +189,7 @@
                [(merge (dissoc page :properties)
                        {:db/id page-id
                         :block/original-name (or (:block/original-name page) (string/capitalize (:block/name page)))
-                        :block/name (or (:block/name page) (string/lower-case (:block/original-name page)))
+                        :block/name (or (:block/name page) (sqlite-util/sanitize-page-name (:block/original-name page)))
                         :block/created-at created-at
                         :block/updated-at created-at}
                        (when (seq (:properties page))

+ 3 - 2
scripts/src/logseq/tasks/db_graph/create_graph_with_properties.cljs

@@ -2,6 +2,7 @@
   "Script that generates all the permutations of property types and cardinality.
    Also creates a page of queries that exercises most properties"
   (:require [logseq.tasks.db-graph.create-graph :as create-graph]
+            [logseq.db.sqlite.util :as sqlite-util]
             [clojure.string :as string]
             [datascript.core :as d]
             ["path" :as node-path]
@@ -11,7 +12,7 @@
 (defn- date-journal-title [date]
   (let [title (.toLocaleString date "en-US" #js {:month "short" :day "numeric" :year "numeric"})
         suffixes {1 "st" 21 "st" 31 "st" 2 "nd" 22 "nd" 3 "rd" 23 "rd" 33 "rd"}]
-    (string/lower-case
+    (sqlite-util/sanitize-page-name
      (string/replace-first title #"(\d+)" (str "$1" (suffixes (.getDate date) "th"))))))
 
 (defn- date-journal-day [date]
@@ -78,7 +79,7 @@
     (println "Generating" (count (filter :block/name blocks-tx)) "pages and"
              (count (filter :block/content blocks-tx)) "blocks ...")
     (d/transact! conn blocks-tx)
-    (println "Created graph" (str db-name "!"))))
+    (println "Created graph" (str db-name " with " (count (d/datoms @conn :eavt)) " datoms!"))))
 
 (when (= nbb/*file* (:file (meta #'-main)))
   (-main *command-line-args*))

+ 2 - 1
scripts/src/logseq/tasks/db_graph/create_graph_with_schema_org.cljs

@@ -9,6 +9,7 @@
        type logseq doesnt' support yet
      * schema.org assumes no cardinality. For now, only :object properties are given a :cardinality :many"
   (:require [logseq.tasks.db-graph.create-graph :as create-graph]
+            [logseq.db.sqlite.util :as sqlite-util]
             [clojure.string :as string]
             [datascript.core :as d]
             ["path" :as node-path]
@@ -133,7 +134,7 @@
   [property-ids class-ids {:keys [verbose]}]
   (let [conflicts
         (->> (concat property-ids class-ids)
-             (group-by (comp string/lower-case first))
+             (group-by (comp sqlite-util/sanitize-page-name first))
              (filter #(> (count (val %)) 1))
              vals)
         ;; If this assertion fails then renamed-classes approach to resolving

+ 0 - 28
src/main/frontend/handler/common/repo.cljs

@@ -1,28 +0,0 @@
-(ns ^:nbb-compatible frontend.handler.common.repo
-  (:require [datascript.core :as d]
-            [logseq.db.property :as db-property]
-            [logseq.graph-parser.util :as gp-util]
-            [logseq.db.sqlite.util :as sqlite-util]))
-
-(defn build-db-initial-data
-  [config-content]
-  (let [initial-files [{:block/uuid (d/squuid)
-                        :file/path (str "logseq/" "config.edn")
-                        :file/content config-content}
-                       {:block/uuid (d/squuid)
-                        :file/path (str "logseq/" "custom.css")
-                        :file/content ""}
-                       {:block/uuid (d/squuid)
-                        :file/path (str "logseq/" "custom.js")
-                        :file/content ""}]
-        default-properties (map
-                            (fn [[k-keyword {:keys [schema original-name]}]]
-                              (let [k-name (name k-keyword)]
-                                (sqlite-util/block-with-timestamps
-                                 {:block/schema schema
-                                  :block/original-name (or original-name k-name)
-                                  :block/name (gp-util/page-name-sanity-lc k-name)
-                                  :block/uuid (d/squuid)
-                                  :block/type "property"})))
-                            db-property/built-in-properties)]
-    (concat initial-files default-properties)))

+ 2 - 2
src/main/frontend/handler/repo.cljs

@@ -12,7 +12,6 @@
             [frontend.handler.file :as file-handler]
             [frontend.handler.repo-config :as repo-config-handler]
             [frontend.handler.common.file :as file-common-handler]
-            [frontend.handler.common.repo :as repo-common-handler]
             [frontend.handler.route :as route-handler]
             [frontend.handler.ui :as ui-handler]
             [frontend.handler.global-config :as global-config-handler]
@@ -28,6 +27,7 @@
             [frontend.db.persist :as db-persist]
             [logseq.graph-parser :as graph-parser]
             [logseq.graph-parser.config :as gp-config]
+            [logseq.db.sqlite.util :as sqlite-util]
             [electron.ipc :as ipc]
             [cljs-bean.core :as bean]
             [clojure.core.async :as async]
@@ -547,7 +547,7 @@
           _ (route-handler/redirect-to-home!)
           _ (db/transact! full-graph-name [(react/kv :db/type "db")]
                           {:skip-persist? true})
-          initial-data (repo-common-handler/build-db-initial-data config/config-default-content)
+          initial-data (sqlite-util/build-db-initial-data config/config-default-content)
           _ (db/transact! full-graph-name initial-data)
           _ (repo-config-handler/set-repo-config-state! full-graph-name config/config-default-content)
           ;; TODO: handle global graph