Browse Source

Merge branch 'feat/db' into enhance/plugin-web

charlie 10 tháng trước cách đây
mục cha
commit
aceaf0c879

+ 1 - 14
src/main/frontend/components/property.cljs

@@ -236,20 +236,7 @@
 
             ;; using class as property
             (and property (ldb/class? property))
-            (let [schema (assoc (:block/schema property)
-                                :type :node)]
-              (p/do!
-               (db/transact! (state/get-current-repo)
-                             [{:db/id (:db/id property)
-                               :db/ident (:db/ident property)
-                               :db/cardinality :db.cardinality/one
-                               :db/valueType :db.type/ref
-                               :db/index true
-                               :block/tags :logseq.class/Property
-                               :block/schema schema
-                               :property/schema.classes (:db/id property)}]
-                             {:outliner-op :save-block})
-               (reset! *show-new-property-config? false)))
+            (pv/<set-class-as-property! (state/get-current-repo) property)
 
             (or (not= :default type)
                 (and (= :default type) (seq (:property/closed-values property))))

+ 18 - 1
src/main/frontend/components/property/value.cljs

@@ -158,6 +158,20 @@
                                  (remove ldb/property?))]
     (or (seq selected-blocks) [block])))
 
+(defn <set-class-as-property!
+  [repo property]
+  (db/transact! repo
+                [{:db/id (:db/id property)
+                  :db/ident (:db/ident property)
+                  :db/cardinality :db.cardinality/one
+                  :db/valueType :db.type/ref
+                  :db/index true
+                  :block/tags :logseq.class/Property
+                  :block/schema (assoc (:block/schema property)
+                                       :type :node)
+                  :property/schema.classes (:db/id property)}]
+                {:outliner-op :save-block}))
+
 (defn <add-property!
   "If a class and in a class schema context, add the property to its schema.
   Otherwise, add a block's property and its value"
@@ -173,7 +187,10 @@
      (assert (qualified-keyword? property-id) "property to add must be a keyword")
      (p/do!
       (if (and class? class-schema?)
-        (db-property-handler/class-add-property! (:db/id block) property-id)
+        (p/do!
+         (when (ldb/class? property)
+           (<set-class-as-property! repo property))
+         (db-property-handler/class-add-property! (:db/id block) property-id))
         (let [block-ids (map :block/uuid blocks)]
           (if (and (db-property-type/all-ref-property-types (get-in property [:block/schema :type]))
                    (string? property-value))

+ 13 - 9
src/main/frontend/handler/common/developer.cljs

@@ -1,17 +1,17 @@
 (ns frontend.handler.common.developer
   "Common fns for developer related functionality"
-  (:require [frontend.db :as db]
-            [cljs.pprint :as pprint]
-            [frontend.state :as state]
+  (:require [cljs.pprint :as pprint]
+            [datascript.impl.entity :as de]
+            [frontend.config :as config]
+            [frontend.db :as db]
+            [frontend.format.mldoc :as mldoc]
             [frontend.handler.notification :as notification]
+            [frontend.persist-db :as persist-db]
+            [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.util.page :as page-util]
-            [frontend.format.mldoc :as mldoc]
-            [frontend.config :as config]
-            [frontend.persist-db :as persist-db]
-            [promesa.core :as p]
-            [datascript.impl.entity :as de]
-            [logseq.db.frontend.property :as db-property]))
+            [logseq.db.frontend.property :as db-property]
+            [promesa.core :as p]))
 
 ;; Fns used between menus and commands
 (defn show-entity-data
@@ -88,6 +88,10 @@
         (show-content-ast (get-in page-data [:block/file :file/content]) (:block/format page-data))
         (notification/show! "No page found" :warning)))))
 
+(defn ^:export validate-db []
+  (when-let [^Object worker @state/*db-worker]
+    (.validate-db worker (state/get-current-repo))))
+
 (defn import-chosen-graph
   [repo]
   (p/let [_ (persist-db/<unsafe-delete repo)]

+ 6 - 2
src/main/frontend/handler/worker.cljs

@@ -4,8 +4,9 @@
             [frontend.handler.file :as file-handler]
             [frontend.handler.notification :as notification]
             [frontend.state :as state]
-            [promesa.core :as p]
-            [logseq.db :as ldb]))
+            [lambdaisland.glogi :as log]
+            [logseq.db :as ldb]
+            [promesa.core :as p]))
 
 (defmulti handle identity)
 
@@ -25,6 +26,9 @@
 (defmethod handle :notification [_ _worker data]
   (apply notification/show! data))
 
+(defmethod handle :log [_ _worker [name level data]]
+  (log/log name level data))
+
 (defmethod handle :add-repo [_ _worker data]
   (state/add-repo! {:url (:repo data)})
   (state/pub-event! [:graph/switch (:repo data) {:rtc-download? true}]))

+ 22 - 16
src/main/frontend/modules/shortcut/config.cljs

@@ -1,34 +1,34 @@
 (ns frontend.modules.shortcut.config
-  (:require [clojure.string :as str]
+  (:require [clojure.data :as data]
+            [clojure.string :as str]
+            [electron.ipc :as ipc]
+            [frontend.commands :as commands]
             [frontend.components.commit :as commit]
-            [frontend.extensions.srs.handler :as srs]
+            [frontend.config :as config]
+            [frontend.dicts :as dicts]
             [frontend.extensions.pdf.utils :as pdf-utils]
+            [frontend.extensions.srs.handler :as srs]
             [frontend.handler.config :as config-handler]
             [frontend.handler.editor :as editor-handler]
-            [frontend.handler.paste :as paste-handler]
+            [frontend.handler.export :as export-handler]
             [frontend.handler.history :as history]
+            [frontend.handler.journal :as journal-handler]
+            [frontend.handler.jump :as jump-handler]
             [frontend.handler.notification :as notification]
             [frontend.handler.page :as page-handler]
+            [frontend.handler.paste :as paste-handler]
+            [frontend.handler.plugin :as plugin-handler]
+            [frontend.handler.plugin-config :as plugin-config-handler]
             [frontend.handler.route :as route-handler]
-            [frontend.handler.journal :as journal-handler]
             [frontend.handler.search :as search-handler]
             [frontend.handler.ui :as ui-handler]
-            [frontend.handler.plugin :as plugin-handler]
-            [frontend.handler.export :as export-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
-            [frontend.handler.plugin-config :as plugin-config-handler]
             [frontend.handler.window :as window-handler]
-            [frontend.handler.jump :as jump-handler]
-            [frontend.dicts :as dicts]
             [frontend.modules.shortcut.before :as m]
             [frontend.state :as state]
             [frontend.util :refer [mac?] :as util]
-            [frontend.commands :as commands]
-            [frontend.config :as config]
-            [electron.ipc :as ipc]
-            [promesa.core :as p]
-            [clojure.data :as data]
-            [medley.core :as medley]))
+            [medley.core :as medley]
+            [promesa.core :as p]))
 
 (defn- search
   [mode]
@@ -603,7 +603,11 @@
 
    :dev/show-page-ast {:binding []
                        :inactive (not (state/developer-mode?))
-                       :fn :frontend.handler.common.developer/show-page-ast}})
+                       :fn :frontend.handler.common.developer/show-page-ast}
+
+   :dev/validate-db   {:binding []
+                       :inactive (not (state/developer-mode?))
+                       :fn :frontend.handler.common.developer/validate-db}})
 
 (let [keyboard-commands
       {::commands (set (keys all-built-in-keyboard-shortcuts))
@@ -809,6 +813,7 @@
           :dev/show-page-data
           :dev/show-page-ast
           :dev/replace-graph-with-db-file
+          :dev/validate-db
           :ui/customize-appearance])
         (with-meta {:before m/enable-when-not-editing-mode!}))
 
@@ -996,6 +1001,7 @@
      :dev/show-page-data
      :dev/show-page-ast
      :dev/replace-graph-with-db-file
+     :dev/validate-db
      :ui/clear-all-notifications]
 
     :shortcut.category/plugins

+ 33 - 0
src/main/frontend/worker/db/validate.cljs

@@ -0,0 +1,33 @@
+(ns frontend.worker.db.validate
+  "Validate db"
+  (:require [cljs.pprint :as pprint]
+            [frontend.worker.util :as worker-util]
+            [logseq.db :as ldb]
+            [logseq.db.frontend.validate :as db-validate]))
+
+(defn- counts-from-entities
+  [entities]
+  {:entities (count entities)
+   :pages (count (filter :block/name entities))
+   :blocks (count (filter :block/title entities))
+   :classes (count (filter ldb/class? entities))
+   :objects (count (filter #(seq (:block/tags %)) entities))
+   :properties (count (filter ldb/property? entities))
+   :property-values (count (mapcat :block/properties entities))})
+
+(defn validate-db
+  [db]
+  (let [{:keys [errors datom-count entities]} (db-validate/validate-db! db)]
+    (if errors
+      (do
+        (worker-util/post-message :log [:db-invalid :error
+                                        {:msg (str "Validation detected " (count errors) " invalid block(s):")
+                                         :counts (assoc (counts-from-entities entities) :datoms datom-count)}])
+        (pprint/pprint errors)
+        (worker-util/post-message :notification
+                                  [(str "Validation detected " (count errors) " invalid block(s). These blocks may be buggy when you interact with them. See the javascript console for more.")
+                                   :warning false]))
+
+      (worker-util/post-message :notification
+                                [(str "Your graph is valid! " (assoc (counts-from-entities entities) :datoms datom-count))
+                                 :success false]))))

+ 6 - 0
src/main/frontend/worker/db_worker.cljs

@@ -13,6 +13,7 @@
             [frontend.worker.db-listener :as db-listener]
             [frontend.worker.db-metadata :as worker-db-metadata]
             [frontend.worker.db.migrate :as db-migrate]
+            [frontend.worker.db.validate :as worker-db-validate]
             [frontend.worker.device :as worker-device]
             [frontend.worker.export :as worker-export]
             [frontend.worker.file :as file]
@@ -908,6 +909,11 @@
    (undo-redo/record-editor-info! repo (ldb/read-transit-str editor-info-str))
    nil)
 
+  (validate-db
+   [_this repo]
+   (when-let [conn (worker-state/get-datascript-conn repo)]
+     (worker-db-validate/validate-db @conn)))
+
   (dangerousRemoveAllDbs
    [this repo]
    (p/let [dbs (.listDB this)]

+ 1 - 0
src/resources/dicts/en.edn

@@ -796,4 +796,5 @@
   :dev/show-page-data "(Dev) Show page data"
   :dev/show-page-ast "(Dev) Show page AST"
   :dev/replace-graph-with-db-file "(Dev) Replace graph with its db.sqlite file"
+  :dev/validate-db "(Dev) Validate current graph"
   :window/close "Close window"}}