Browse Source

Provide sqlite.cli for reusable sqlite fns

Already had to do read-graph 3 times and will soon need to it outside
logseq so worth putting this behind an api ns
Gabriel Horner 2 years ago
parent
commit
172ee0ae24

+ 1 - 0
deps/db/.carve/config.edn

@@ -3,6 +3,7 @@
                   logseq.db.sqlite.restore
                   logseq.db.sqlite.rtc
                   logseq.db.sqlite.util
+                  logseq.db.sqlite.cli
                   ;; Some fns are used by frontend but not worth moving over yet
                   logseq.db.schema]
  :report {:format :ignore}}

+ 4 - 8
deps/db/script/query.cljs

@@ -4,9 +4,8 @@
      $ yarn -s nbb-logseq script/query.cljs db-name '[:find (pull ?b [:block/name :block/content]) :where [?b :block/created-at]]'"
     (:require [datascript.core :as d]
               [clojure.edn :as edn]
-              [cljs-bean.core :as bean]
               [logseq.db.sqlite.db :as sqlite-db]
-              [logseq.db.sqlite.restore :as sqlite-restore]
+              [logseq.db.sqlite.cli :as sqlite-cli]
               [logseq.db.rules :as rules]
               [nbb.core :as nbb]
               ["path" :as path]
@@ -15,12 +14,9 @@
 (defn read-graph
   "The db graph bare version of gp-cli/parse-graph"
   [graph-name]
-  (let [graphs-dir (path/join (os/homedir) "logseq/graphs")
-        _ (sqlite-db/open-db! graphs-dir graph-name)
-        {:keys [uuid->db-id-map conn]}
-        (sqlite-restore/restore-initial-data (bean/->js (sqlite-db/get-initial-data graph-name)))
-        db (sqlite-restore/restore-other-data conn (sqlite-db/get-other-data graph-name []) uuid->db-id-map)]
-    (d/conn-from-db db)))
+  (let [graphs-dir (path/join (os/homedir) "logseq/graphs")]
+    (sqlite-db/open-db! graphs-dir graph-name)
+    (sqlite-cli/read-graph graph-name)))
 
 (defn -main [args]
   (when (not= 2 (count args))

+ 25 - 0
deps/db/src/logseq/db/sqlite/cli.cljs

@@ -0,0 +1,25 @@
+(ns ^:node-only logseq.db.sqlite.cli
+  "Primary ns to interact with DB graphs with node.js based CLIs"
+  (:require [logseq.db.sqlite.db :as sqlite-db]
+            [logseq.db.sqlite.restore :as sqlite-restore]
+            [cljs-bean.core :as bean]
+            [datascript.core :as d]
+            ["fs" :as fs]
+            ["path" :as node-path]))
+
+(defn db-graph-directory?
+  "Returns boolean indicating if the given directory is a DB graph"
+  [graph-dir]
+  (fs/existsSync (node-path/join graph-dir "db.sqlite")))
+
+(defn read-graph
+  "Reads a given sqlite db and returns a datascript connection of its contents.
+   The sqlite db is assumed to have already been opened by sqlite-db/open-db!"
+  [db-name]
+  (let [{:keys [uuid->db-id-map conn]}
+        (sqlite-restore/restore-initial-data (bean/->js (sqlite-db/get-initial-data db-name)))
+        db (sqlite-restore/restore-other-data
+                conn
+                (sqlite-db/get-other-data db-name [])
+                uuid->db-id-map)]
+    (d/conn-from-db db)))

+ 2 - 8
deps/graph-parser/test/logseq/graph_parser/cli_test.cljs

@@ -5,7 +5,7 @@
             [clojure.string :as string]
             [datascript.core :as d]
             [logseq.db.sqlite.db :as sqlite-db]
-            [logseq.db.sqlite.restore :as sqlite-restore]
+            [logseq.db.sqlite.cli :as sqlite-cli]
             [logseq.db.sqlite.util :as sqlite-util]
             [cljs-bean.core :as bean]
             [logseq.db :as ldb]
@@ -136,13 +136,7 @@
                        (assoc % :datoms (sqlite-util/block-map->datoms-str frontend-blocks %)))
                      frontend-blocks)
         _ (sqlite-db/upsert-blocks! db-name (bean/->js blocks))
-        {:keys [uuid->db-id-map conn]}
-        (sqlite-restore/restore-initial-data (bean/->js (sqlite-db/get-initial-data db-name)))
-        new-db (sqlite-restore/restore-other-data
-                conn
-                (sqlite-db/get-other-data db-name [])
-                uuid->db-id-map)
-        conn (d/conn-from-db new-db)]
+        conn (sqlite-cli/read-graph db-name)]
     (ldb/create-default-pages! conn)
     @conn))
 

+ 3 - 12
scripts/src/logseq/tasks/dev/publishing.cljs

@@ -4,6 +4,7 @@
             [logseq.publishing :as publishing]
             [logseq.db.sqlite.db :as sqlite-db]
             [logseq.db.sqlite.restore :as sqlite-restore]
+            [logseq.db.sqlite.cli :as sqlite-cli]
             ["fs" :as fs]
             ["path" :as node-path]
             [cljs-bean.core :as bean]
@@ -22,20 +23,10 @@
                        output-path
                        {:repo-config repo-config})))
 
-(defn- read-db-graph [db-name]
-  (let [{:keys [uuid->db-id-map conn]}
-        (sqlite-restore/restore-initial-data (bean/->js (sqlite-db/get-initial-data db-name)))
-        new-db (sqlite-restore/restore-other-data
-                conn
-                (sqlite-db/get-other-data db-name [])
-                uuid->db-id-map)]
-
-    (d/conn-from-db new-db)))
-
 (defn- publish-db-graph [static-dir graph-dir output-path]
   (let [db-name (node-path/basename graph-dir)
         _ (sqlite-db/open-db! (node-path/dirname graph-dir) db-name)
-        conn (read-db-graph db-name)
+        conn (sqlite-cli/read-graph db-name)
         repo-config (-> (d/q '[:find ?content
                                :where [?b :file/path "logseq/config.edn"] [?b :file/content ?content]]
                              @conn)
@@ -55,6 +46,6 @@
   (let [[static-dir graph-dir output-path]
         ;; Offset relative paths since they are run in a different directory than user is in
         (map #(if (node-path/isAbsolute %) % (node-path/resolve ".." %)) args)]
-    (if (fs/existsSync (node-path/join graph-dir "db.sqlite"))
+    (if (sqlite-cli/db-graph-directory? graph-dir)
       (publish-db-graph static-dir graph-dir output-path)
       (publish-file-graph static-dir graph-dir output-path))))