فهرست منبع

fix: listen to db changes after initial data transacted

Tienson Qin 1 سال پیش
والد
کامیت
16689299c2

+ 13 - 8
src/main/frontend/db_worker.cljs

@@ -31,7 +31,8 @@
             [shadow.cljs.modern :refer [defclass]]
             [logseq.common.util :as common-util]
             [frontend.worker.db.fix :as db-fix]
-            [logseq.db.frontend.order :as db-order]))
+            [logseq.db.frontend.order :as db-order]
+            [logseq.db.sqlite.create-graph :as sqlite-create-graph]))
 
 (defonce *sqlite worker-state/*sqlite)
 (defonce *sqlite-conns worker-state/*sqlite-conns)
@@ -161,7 +162,7 @@
       [db search-db])))
 
 (defn- create-or-open-db!
-  [repo]
+  [repo {:keys [config]}]
   (when-not (worker-state/get-sqlite-conn repo)
     (p/let [[db search-db] (get-db-and-search-db repo)
             storage (new-sqlite-storage repo {})]
@@ -173,6 +174,9 @@
       (let [schema (sqlite-util/get-schema repo)
             conn (sqlite-common-db/get-storage-conn storage schema)]
         (swap! *datascript-conns assoc repo conn)
+        (when config
+          (let [initial-data (sqlite-create-graph/build-db-initial-data config)]
+            (d/transact! conn initial-data {:initial-db? true})))
         (p/let [_ (op-mem-layer/<init-load-from-indexeddb2! repo)]
           (db-listener/listen-db-changes! repo conn))))))
 
@@ -282,12 +286,13 @@
      (bean/->js dbs)))
 
   (createOrOpenDB
-   [_this repo & {:keys [close-other-db?]
-                  :or {close-other-db? true}}]
-   (p/do!
-    (when close-other-db?
-      (close-other-dbs! repo))
-    (create-or-open-db! repo)))
+   [_this repo opts-str]
+   (let [{:keys [close-other-db? config]
+          :or {close-other-db? true}} (ldb/read-transit-str opts-str)]
+     (p/do!
+      (when close-other-db?
+        (close-other-dbs! repo))
+      (create-or-open-db! repo {:config config}))))
 
   (getMaxTx
    [_this repo]

+ 1 - 1
src/main/frontend/handler/file_based/nfs.cljs

@@ -128,7 +128,7 @@
                                [:notification/show
                                 {:content (str "This graph already exists in \"" (:root exists-graph) "\"")
                                  :status :warning}])
-                              (p/do! (persist-db/<new repo)
+                              (p/do! (persist-db/<new repo {})
                                      (repo-handler/start-repo-db-if-not-exists! repo)
                                      (when (config/global-config-enabled?)
                                        (global-config-handler/restore-global-config!))

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

@@ -22,7 +22,6 @@
             [frontend.persist-db :as persist-db]
             [promesa.core :as p]
             [frontend.db.persist :as db-persist]
-            [logseq.db.sqlite.create-graph :as sqlite-create-graph]
             [electron.ipc :as ipc]
             [cljs-bean.core :as bean]
             [frontend.mobile.util :as mobile-util]
@@ -189,13 +188,11 @@
 
 (defn- create-db [full-graph-name {:keys [file-graph-import?]}]
   (->
-   (p/let [_ (persist-db/<new full-graph-name)
+   (p/let [config (migrate-db-config config/config-default-content)
+           _ (persist-db/<new full-graph-name {:config config})
            _ (start-repo-db-if-not-exists! full-graph-name)
            _ (state/add-repo! {:url full-graph-name})
            _ (when-not file-graph-import? (route-handler/redirect-to-home!))
-           initial-data (sqlite-create-graph/build-db-initial-data
-                         (migrate-db-config 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
            _ (state/pub-event! [:init/commands])

+ 2 - 2
src/main/frontend/persist_db.cljs

@@ -36,9 +36,9 @@
 
 ;; FIXME: limit repo name's length and sanity
 ;; @shuyu Do we still need this?
-(defn <new [repo]
+(defn <new [repo opts]
   {:pre [(<= (count repo) 128)]}
-  (p/let [_ (protocol/<new (get-impl) repo)]
+  (p/let [_ (protocol/<new (get-impl) repo opts)]
     (<export-db repo {})))
 
 (defn export-current-graph!

+ 3 - 3
src/main/frontend/persist_db/browser.cljs

@@ -138,9 +138,9 @@
 
 (defrecord InBrowser []
   protocol/PersistentDB
-  (<new [_this repo]
+  (<new [_this repo opts]
     (when-let [^js sqlite @*worker]
-      (.createOrOpenDB sqlite repo)))
+      (.createOrOpenDB sqlite repo (ldb/write-transit-str opts))))
 
   (<list-db [_this]
     (when-let [^js sqlite @*worker]
@@ -163,7 +163,7 @@
                   disk-db-data (when-not db-exists? (ipc/ipc :db-get repo))
                   _ (when disk-db-data
                       (.importDb sqlite repo disk-db-data))
-                  _ (.createOrOpenDB sqlite repo)]
+                  _ (.createOrOpenDB sqlite repo (ldb/write-transit-str {}))]
             (.getInitialData sqlite repo))
           (p/catch sqlite-error-handler))))
 

+ 1 - 1
src/main/frontend/persist_db/protocol.cljs

@@ -3,7 +3,7 @@
 
 (defprotocol PersistentDB
   (<list-db [this] "List all databases")
-  (<new [this repo] "Create or open a graph")
+  (<new [this repo opts] "Create or open a graph")
   (<unsafe-delete [this repo] "Delete graph and its vfs")
   (<release-access-handles [this repo] "Release access file handles")
   (<fetch-initial-data [this repo opts] "Fetch Initial data")

+ 1 - 1
src/main/frontend/worker/rtc/full_upload_download_graph.cljs

@@ -248,7 +248,7 @@
       (m/?
        (c.m/await-promise
         (p/do!
-         (.createOrOpenDB worker-obj repo {:close-other-db? false})
+         (.createOrOpenDB worker-obj repo (ldb/write-transit-str {:close-other-db? false}))
          (.exportDB worker-obj repo)
          (.transact worker-obj repo init-tx-data {:rtc-download-graph? true
                                                   :gen-undo-ops? false