فهرست منبع

enhance: add basic script to test db graph import

also tweaked query script to work with db graphs by relative path
Gabriel Horner 1 سال پیش
والد
کامیت
a61ee21e25
4فایلهای تغییر یافته به همراه94 افزوده شده و 19 حذف شده
  1. 9 1
      bb.edn
  2. 22 17
      deps/db/script/query.cljs
  3. 63 0
      deps/graph-parser/script/db_import.cljs
  4. 0 1
      deps/graph-parser/src/logseq/graph_parser/exporter.cljs

+ 9 - 1
bb.edn

@@ -68,12 +68,20 @@
 
   dev:db-query
   {:doc "Query a DB graph's datascript db"
-   :task (apply shell {:dir "deps/db"} "yarn -s nbb-logseq script/query.cljs" *command-line-args*)}
+   :requires ([babashka.fs :as fs])
+   :task (apply shell {:dir "deps/db" :extra-env {"ORIGINAL_PWD" (fs/cwd)}}
+                "yarn -s nbb-logseq script/query.cljs" *command-line-args*)}
 
   dev:db-transact
   {:doc "Transact against a DB graph's datascript db"
    :task (apply shell {:dir "deps/outliner"} "yarn -s nbb-logseq script/transact.cljs" *command-line-args*)}
 
+  dev:db-import
+  {:doc "Import a file graph to db graph"
+   :requires ([babashka.fs :as fs])
+   :task (apply shell {:dir "deps/graph-parser" :extra-env {"ORIGINAL_PWD" (fs/cwd)}}
+                "yarn -s nbb-logseq -cp src:../outliner/src:../../scripts/src script/db_import.cljs" *command-line-args*)}
+
   dev:db-datoms
   {:doc "Write db's datoms to a file"
    :requires ([babashka.fs :as fs])

+ 22 - 17
deps/db/script/query.cljs

@@ -1,27 +1,32 @@
-  (ns query
-    "An example script that queries any db graph from the commandline e.g.
+(ns query
+  "An example script that queries any db graph from the commandline e.g.
 
-     $ 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]
-              [logseq.db.sqlite.db :as sqlite-db]
-              [logseq.db.frontend.rules :as rules]
-              [nbb.core :as nbb]
-              ["path" :as path]
-              ["os" :as os]))
+  $ 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]
+            [logseq.db.sqlite.db :as sqlite-db]
+            [logseq.db.frontend.rules :as rules]
+            [nbb.core :as nbb]
+            [clojure.string :as string]
+            ["path" :as node-path]
+            ["os" :as os]))
 
-(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)))
+(defn- get-dir-and-db-name
+  "Gets dir and db name for use with open-db!"
+  [graph-dir]
+  (if (string/includes? graph-dir "/")
+    (let [graph-dir'
+          (node-path/join (or js/process.env.ORIGINAL_PWD ".") graph-dir)]
+      ((juxt node-path/dirname node-path/basename) graph-dir'))
+    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
 
 (defn -main [args]
   (when (< (count args) 2)
     (println "Usage: $0 GRAPH QUERY")
     (js/process.exit 1))
-  (let [[graph-name query*] args
-        conn (read-graph graph-name)
+  (let [[graph-dir query*] args
+        [dir db-name] (get-dir-and-db-name graph-dir)
+        conn (sqlite-db/open-db! dir db-name)
         query (into (edn/read-string query*) [:in '$ '%]) ;; assumes no :in are in queries
         results (mapv first (d/q query @conn (rules/extract-rules rules/db-query-dsl-rules)))]
     (when ((set args) "-v") (println "DB contains" (count (d/datoms @conn :eavt)) "datoms"))

+ 63 - 0
deps/graph-parser/script/db_import.cljs

@@ -0,0 +1,63 @@
+(ns db-import
+  "Imports given file(s) to a db graph. This script is primarly for
+   developing the import feature and for engineers who want to customize
+   the import process"
+  (:require [clojure.string :as string]
+            [datascript.core :as d]
+            ["path" :as node-path]
+            ["os" :as os]
+            ["fs" :as fs]
+            [nbb.core :as nbb]
+            [babashka.cli :as cli]
+            [logseq.common.config :as common-config]
+            [logseq.graph-parser.exporter :as gp-exporter]
+            [logseq.tasks.db-graph.create-graph :as create-graph]))
+
+(defn- setup-import-options
+  [db config user-options]
+  {:extract-options {:date-formatter (common-config/get-date-formatter config)
+                     :user-config config
+                     :filename-format (or (:file/name-format config) :legacy)}
+   :user-options user-options
+   :page-tags-uuid (:block/uuid (d/entity db [:block/name "pagetags"]))
+   :import-state (gp-exporter/new-import-state)
+   :macros (:macros config)})
+
+(defn- import-file-graph-to-db [file-graph conn db-name]
+  ;; TODO: Read in repo config
+  (let [import-options (setup-import-options @conn
+                                             {:file/name-format :triple-lowbar}
+                                             {:graph-name db-name})
+        ;; TODO: Read files dir and port more from import
+        file file-graph
+        m {:file/path file
+           :file/content (str (fs/readFileSync file))}]
+    (gp-exporter/add-file-to-db-graph conn (:file/path m) (:file/content m) import-options)))
+
+(def spec
+  "Options spec"
+  {:help {:alias :h
+          :desc "Print help"}
+   :verbose {:alias :v
+             :desc "Verbose mode"}})
+
+(defn -main [args]
+  (let [[file-graph db-graph-dir] args
+        options (cli/parse-opts args {:spec spec})
+        _ (when (or (< (count args) 2) (:help options))
+            (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
+                          (cli/format-opts {:spec spec})))
+            (js/process.exit 1))
+        [dir db-name] (if (string/includes? db-graph-dir "/")
+                        (let [graph-dir'
+                              (node-path/join (or js/process.env.ORIGINAL_PWD ".") db-graph-dir)]
+                          ((juxt node-path/dirname node-path/basename) graph-dir'))
+                        [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
+        file-graph' (node-path/join (or js/process.env.ORIGINAL_PWD ".") file-graph)
+        conn (create-graph/init-conn dir db-name)]
+    (import-file-graph-to-db file-graph' conn db-name)
+    (when (:verbose options) (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
+    (println "Created graph" (str db-name "!"))))
+
+(when (= nbb/*file* (:file (meta #'-main)))
+  (-main *command-line-args*))

+ 0 - 1
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -247,7 +247,6 @@
                    (into {}))
               _ (when (seq property-changes) (prn :PROP-CHANGES property-changes))
               options' (assoc options :property-changes property-changes)]
-          (prn :classes-from-properties classes-from-properties (:block/name block))
           (cond-> (assoc-in block [:block/properties]
                             (update-properties properties' db page-names-to-uuids
                                                (select-keys block [:block/properties-text-values :block/name :block/content])