소스 검색

enhance(dev): scripts can read or write db graphs as full path files

This allows for easier use of debugging graphs as they are exported by
users as a file. Also DRYed up duplicated helper
Gabriel Horner 6 달 전
부모
커밋
3fe790d4d7

+ 7 - 17
deps/db/script/create_graph.cljs

@@ -2,17 +2,16 @@
   "A script that creates or updates a DB graph given a sqlite.build EDN file.
   "A script that creates or updates a DB graph given a sqlite.build EDN file.
    If the given graph already exists, the EDN file updates the graph."
    If the given graph already exists, the EDN file updates the graph."
   (:require ["fs" :as fs]
   (:require ["fs" :as fs]
-            ["os" :as os]
             ["path" :as node-path]
             ["path" :as node-path]
             [babashka.cli :as cli]
             [babashka.cli :as cli]
             [clojure.edn :as edn]
             [clojure.edn :as edn]
-            [clojure.string :as string]
             [datascript.core :as d]
             [datascript.core :as d]
             [logseq.db.sqlite.export :as sqlite-export]
             [logseq.db.sqlite.export :as sqlite-export]
             [logseq.outliner.cli :as outliner-cli]
             [logseq.outliner.cli :as outliner-cli]
             [nbb.classpath :as cp]
             [nbb.classpath :as cp]
             [nbb.core :as nbb]
             [nbb.core :as nbb]
-            [validate-db]))
+            [validate-db]
+            [logseq.db.common.sqlite-cli :as sqlite-cli]))
 
 
 (defn- resolve-path
 (defn- resolve-path
   "If relative path, resolve with $ORIGINAL_PWD"
   "If relative path, resolve with $ORIGINAL_PWD"
@@ -21,17 +20,6 @@
     path
     path
     (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
     (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (def spec
 (def spec
   "Options spec"
   "Options spec"
   {:help {:alias :h
   {:help {:alias :h
@@ -48,11 +36,13 @@
             (println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
             (println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
                           (cli/format-opts {:spec spec})))
                           (cli/format-opts {:spec spec})))
             (js/process.exit 1))
             (js/process.exit 1))
-        [dir db-name] (get-dir-and-db-name graph-dir)
+        init-conn-args (conj (sqlite-cli/->open-db-args graph-dir))
         sqlite-build-edn (merge (if (:import options) {} {:auto-create-ontology? true})
         sqlite-build-edn (merge (if (:import options) {} {:auto-create-ontology? true})
                                 (-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
                                 (-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
-        graph-exists? (fs/existsSync (node-path/join dir db-name))
-        conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath) :import-type :cli/create-graph})
+        graph-exists? (fs/existsSync (apply node-path/join init-conn-args))
+        db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
+        conn (apply outliner-cli/init-conn
+                    (conj init-conn-args {:classpath (cp/get-classpath) :import-type :cli/create-graph}))
         {:keys [init-tx block-props-tx misc-tx] :as _txs}
         {:keys [init-tx block-props-tx misc-tx] :as _txs}
         (if (:import options)
         (if (:import options)
           (sqlite-export/build-import sqlite-build-edn @conn {})
           (sqlite-export/build-import sqlite-build-edn @conn {})

+ 3 - 17
deps/db/script/diff_graphs.cljs

@@ -1,27 +1,13 @@
 (ns diff-graphs
 (ns diff-graphs
   "A script that diffs two DB graphs through their sqlite.build EDN"
   "A script that diffs two DB graphs through their sqlite.build EDN"
-  (:require ["os" :as os]
-            ["path" :as node-path]
-            [babashka.cli :as cli]
+  (:require [babashka.cli :as cli]
             [clojure.data :as data]
             [clojure.data :as data]
             [clojure.pprint :as pprint]
             [clojure.pprint :as pprint]
-            [clojure.string :as string]
             [logseq.common.config :as common-config]
             [logseq.common.config :as common-config]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.db.sqlite.export :as sqlite-export]
             [logseq.db.sqlite.export :as sqlite-export]
             [nbb.core :as nbb]))
             [nbb.core :as nbb]))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (def spec
 (def spec
   "Options spec"
   "Options spec"
   {:help {:alias :h
   {:help {:alias :h
@@ -43,8 +29,8 @@
             (println (str "Usage: $0 GRAPH-NAME GRAPH-NAME2 [& ARGS] [OPTIONS]\nOptions:\n"
             (println (str "Usage: $0 GRAPH-NAME GRAPH-NAME2 [& ARGS] [OPTIONS]\nOptions:\n"
                           (cli/format-opts {:spec spec})))
                           (cli/format-opts {:spec spec})))
             (js/process.exit 1))
             (js/process.exit 1))
-        conn (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir))
-        conn2 (apply sqlite-cli/open-db! (get-dir-and-db-name graph-dir2))
+        conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir))
+        conn2 (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir2))
         export-options (select-keys options [:include-timestamps? :exclude-namespaces :exclude-built-in-pages?])
         export-options (select-keys options [:include-timestamps? :exclude-namespaces :exclude-built-in-pages?])
         export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})
         export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})
         export-map2 (sqlite-export/build-export @conn2 {:export-type :graph :graph-options export-options})
         export-map2 (sqlite-export/build-export @conn2 {:export-type :graph :graph-options export-options})

+ 3 - 10
deps/db/script/dump_datoms.cljs

@@ -3,28 +3,21 @@
 
 
      $ yarn -s nbb-logseq script/dump_datoms.cljs db-name datoms.edn"
      $ yarn -s nbb-logseq script/dump_datoms.cljs db-name datoms.edn"
     (:require ["fs" :as fs]
     (:require ["fs" :as fs]
-              ["os" :as os]
-              ["path" :as path]
+              ["path" :as node-path]
               [clojure.pprint :as pprint]
               [clojure.pprint :as pprint]
               [datascript.core :as d]
               [datascript.core :as d]
               [logseq.db.common.sqlite-cli :as sqlite-cli]
               [logseq.db.common.sqlite-cli :as sqlite-cli]
               [nbb.core :as nbb]))
               [nbb.core :as nbb]))
 
 
-(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-cli/open-db! graphs-dir graph-name)))
-
 (defn -main [args]
 (defn -main [args]
   (when (< (count args) 2)
   (when (< (count args) 2)
     (println "Usage: $0 GRAPH FILE")
     (println "Usage: $0 GRAPH FILE")
     (js/process.exit 1))
     (js/process.exit 1))
   (let [[graph-name file*] args
   (let [[graph-name file*] args
-        conn (read-graph graph-name)
+        conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-name))
         datoms (mapv #(vec %) (d/datoms @conn :eavt))
         datoms (mapv #(vec %) (d/datoms @conn :eavt))
         parent-dir (or js/process.env.ORIGINAL_PWD ".")
         parent-dir (or js/process.env.ORIGINAL_PWD ".")
-        file (path/join parent-dir file*)]
+        file (node-path/join parent-dir file*)]
     (println "Writing" (count datoms) "datoms to" file)
     (println "Writing" (count datoms) "datoms to" file)
     (fs/writeFileSync file (with-out-str (pprint/pprint datoms)))))
     (fs/writeFileSync file (with-out-str (pprint/pprint datoms)))))
 
 

+ 1 - 16
deps/db/script/export_graph.cljs

@@ -1,12 +1,9 @@
 (ns export-graph
 (ns export-graph
   "A script that exports a graph to a sqlite.build EDN file"
   "A script that exports a graph to a sqlite.build EDN file"
   (:require ["fs" :as fs]
   (:require ["fs" :as fs]
-            ["os" :as os]
             ["path" :as node-path]
             ["path" :as node-path]
             [babashka.cli :as cli]
             [babashka.cli :as cli]
-            [clojure.edn :as edn]
             [clojure.pprint :as pprint]
             [clojure.pprint :as pprint]
-            [clojure.string :as string]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.db.sqlite.export :as sqlite-export]
             [logseq.db.sqlite.export :as sqlite-export]
             [nbb.core :as nbb]))
             [nbb.core :as nbb]))
@@ -18,17 +15,6 @@
     path
     path
     (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
     (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (def spec
 (def spec
   "Options spec"
   "Options spec"
   {:help {:alias :h
   {:help {:alias :h
@@ -54,8 +40,7 @@
             (println (str "Usage: $0 GRAPH-NAME [& ARGS] [OPTIONS]\nOptions:\n"
             (println (str "Usage: $0 GRAPH-NAME [& ARGS] [OPTIONS]\nOptions:\n"
                           (cli/format-opts {:spec spec})))
                           (cli/format-opts {:spec spec})))
             (js/process.exit 1))
             (js/process.exit 1))
-        [dir db-name] (get-dir-and-db-name graph-dir)
-        conn (sqlite-cli/open-db! dir db-name)
+        conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir))
         export-options (dissoc options :file)
         export-options (dissoc options :file)
         export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})]
         export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})]
     (if (:file options)
     (if (:file options)

+ 3 - 18
deps/db/script/query.cljs

@@ -3,15 +3,12 @@
 
 
   $ yarn -s nbb-logseq script/query.cljs db-name '[:find (pull ?b [:block/name :block/title]) :where [?b :block/created-at]]'"
   $ yarn -s nbb-logseq script/query.cljs db-name '[:find (pull ?b [:block/name :block/title]) :where [?b :block/created-at]]'"
   (:require ["child_process" :as child-process]
   (:require ["child_process" :as child-process]
-            ["os" :as os]
-            ["path" :as node-path]
             [babashka.cli :as cli]
             [babashka.cli :as cli]
             [clojure.edn :as edn]
             [clojure.edn :as edn]
             [clojure.pprint :as pprint]
             [clojure.pprint :as pprint]
-            [clojure.string :as string]
             [datascript.core :as d]
             [datascript.core :as d]
-            [logseq.db.frontend.rules :as rules]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
+            [logseq.db.frontend.rules :as rules]
             [nbb.core :as nbb]))
             [nbb.core :as nbb]))
 
 
 (defn- sh
 (defn- sh
@@ -22,17 +19,6 @@
                            (clj->js (rest cmd))
                            (clj->js (rest cmd))
                            (clj->js (merge {:stdio "inherit"} opts))))
                            (clj->js (merge {:stdio "inherit"} opts))))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (def spec
 (def spec
   "Options spec"
   "Options spec"
   {:help {:alias :h
   {:help {:alias :h
@@ -48,9 +34,8 @@
             :coerce []
             :coerce []
             :desc "Lookup entities instead of query"}})
             :desc "Lookup entities instead of query"}})
 
 
-(defn query-graph [graph-dir args'' options]
-  (let [[dir db-name] (get-dir-and-db-name graph-dir)
-        conn (sqlite-cli/open-db! dir db-name)
+(defn query-graph [graph args'' options]
+  (let [conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph))
         results (if (:entity options)
         results (if (:entity options)
                   (map #(when-let [ent (d/entity @conn
                   (map #(when-let [ent (d/entity @conn
                                                  (if (string? %) (edn/read-string %) %))]
                                                  (if (string? %) (edn/read-string %) %))]

+ 4 - 17
deps/db/script/validate_db.cljs

@@ -1,11 +1,8 @@
 (ns validate-db
 (ns validate-db
   "Script that validates the datascript db of a DB graph
   "Script that validates the datascript db of a DB graph
    NOTE: This script is also used in CI to confirm our db's schema is up to date"
    NOTE: This script is also used in CI to confirm our db's schema is up to date"
-  (:require ["os" :as os]
-            ["path" :as node-path]
-            [babashka.cli :as cli]
+  (:require [babashka.cli :as cli]
             [cljs.pprint :as pprint]
             [cljs.pprint :as pprint]
-            [clojure.string :as string]
             [datascript.core :as d]
             [datascript.core :as d]
             [logseq.db.frontend.malli-schema :as db-malli-schema]
             [logseq.db.frontend.malli-schema :as db-malli-schema]
             [logseq.db.frontend.validate :as db-validate]
             [logseq.db.frontend.validate :as db-validate]
@@ -50,17 +47,6 @@
         (js/process.exit 1))
         (js/process.exit 1))
       (println "Valid!"))))
       (println "Valid!"))))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (def spec
 (def spec
   "Options spec"
   "Options spec"
   {:help {:alias :h
   {:help {:alias :h
@@ -86,8 +72,9 @@
     (validate-db* db ent-maps options)))
     (validate-db* db ent-maps options)))
 
 
 (defn- validate-graph [graph-dir options]
 (defn- validate-graph [graph-dir options]
-  (let [[dir db-name] (get-dir-and-db-name graph-dir)
-        conn (try (sqlite-cli/open-db! dir db-name)
+  (let [open-db-args (sqlite-cli/->open-db-args graph-dir)
+        db-name (if (= 1 (count open-db-args)) (first open-db-args) (second open-db-args))
+        conn (try (apply sqlite-cli/open-db! open-db-args)
                   (catch :default e
                   (catch :default e
                     (println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
                     (println "Error: For graph" (str (pr-str graph-dir) ":") (str e))
                     (js/process.exit 1)))]
                     (js/process.exit 1)))]

+ 31 - 10
deps/db/src/logseq/db/common/sqlite_cli.cljs

@@ -2,8 +2,10 @@
   "Primary ns to interact with DB files for DB and file graphs with node.js based CLIs"
   "Primary ns to interact with DB files for DB and file graphs with node.js based CLIs"
   (:require ["better-sqlite3" :as sqlite3]
   (:require ["better-sqlite3" :as sqlite3]
             ["fs" :as fs]
             ["fs" :as fs]
+            ["os" :as os]
             ["path" :as node-path]
             ["path" :as node-path]
             [cljs-bean.core :as bean]
             [cljs-bean.core :as bean]
+            [clojure.string :as string]
             ;; FIXME: datascript.core has to come before datascript.storage or else nbb fails
             ;; FIXME: datascript.core has to come before datascript.storage or else nbb fails
             [datascript.core]
             [datascript.core]
             [datascript.storage :refer [IStorage]]
             [datascript.storage :refer [IStorage]]
@@ -86,14 +88,33 @@
   "For a given database name, opens a sqlite db connection for it, creates
   "For a given database name, opens a sqlite db connection for it, creates
   needed sqlite tables if not created and returns a datascript connection that's
   needed sqlite tables if not created and returns a datascript connection that's
   connected to the sqlite db"
   connected to the sqlite db"
-  [graphs-dir db-name]
-  (let [[_db-sanitized-name db-full-path] (common-sqlite/get-db-full-path graphs-dir db-name)
-        db (new sqlite db-full-path nil)
+  ([db-full-path]
+   (open-db! nil db-full-path))
+  ([graphs-dir db-name]
+   (let [[base-name db-full-path]
+         (if (nil? graphs-dir)
+           [(node-path/basename db-name) db-name]
+           [db-name (second (common-sqlite/get-db-full-path graphs-dir db-name))])
+         db (new sqlite db-full-path nil)
         ;; For both desktop and CLI, only file graphs have db-name that indicate their db type
         ;; For both desktop and CLI, only file graphs have db-name that indicate their db type
-        schema (if (common-sqlite/local-file-based-graph? db-name)
-                 file-schema/schema
-                 db-schema/schema)]
-    (common-sqlite/create-kvs-table! db)
-    (let [storage (new-sqlite-storage db)
-          conn (common-sqlite/get-storage-conn storage schema)]
-      conn)))
+         schema (if (common-sqlite/local-file-based-graph? base-name)
+                  file-schema/schema
+                  db-schema/schema)]
+     (common-sqlite/create-kvs-table! db)
+     (let [storage (new-sqlite-storage db)
+           conn (common-sqlite/get-storage-conn storage schema)]
+       conn))))
+
+(defn ->open-db-args
+  "Creates args for open-db from a graph arg. Works for relative and absolute paths and
+   defaults to ~/logseq/graphs/ when no '/' present in name"
+  [graph-dir-or-path]
+  ;; Pass full path directly to allow for paths that don't have standard graph naming convention
+  (if (node-path/isAbsolute graph-dir-or-path)
+    [graph-dir-or-path]
+    (if (string/includes? graph-dir-or-path "/")
+      (let [resolve-path' #(if (node-path/isAbsolute %) %
+                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
+                               (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
+        ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir-or-path)))
+      [(node-path/join (os/homedir) "logseq" "graphs") graph-dir-or-path])))

+ 8 - 19
deps/graph-parser/script/db_import.cljs

@@ -4,9 +4,7 @@
    the import process"
    the import process"
   (:require ["fs" :as fs]
   (:require ["fs" :as fs]
             ["fs/promises" :as fsp]
             ["fs/promises" :as fsp]
-            ["os" :as os]
             ["path" :as node-path]
             ["path" :as node-path]
-            #_:clj-kondo/ignore
             [babashka.cli :as cli]
             [babashka.cli :as cli]
             [cljs.pprint :as pprint]
             [cljs.pprint :as pprint]
             [clojure.set :as set]
             [clojure.set :as set]
@@ -18,7 +16,8 @@
             [logseq.outliner.pipeline :as outliner-pipeline]
             [logseq.outliner.pipeline :as outliner-pipeline]
             [nbb.classpath :as cp]
             [nbb.classpath :as cp]
             [nbb.core :as nbb]
             [nbb.core :as nbb]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            [logseq.db.common.sqlite-cli :as sqlite-cli]))
 
 
 (def tx-queue (atom cljs.core/PersistentQueue.EMPTY))
 (def tx-queue (atom cljs.core/PersistentQueue.EMPTY))
 (def original-transact! d/transact!)
 (def original-transact! d/transact!)
@@ -125,17 +124,6 @@
       (p/let [_ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
       (p/let [_ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
         {:import-state (:import-state doc-options)}))))
         {:import-state (:import-state doc-options)}))))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (def spec
 (def spec
   "Options spec"
   "Options spec"
   {:help {:alias :h
   {:help {:alias :h
@@ -171,9 +159,11 @@
             (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
             (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
                           (cli/format-opts {:spec spec})))
                           (cli/format-opts {:spec spec})))
             (js/process.exit 1))
             (js/process.exit 1))
-        [dir db-name] (get-dir-and-db-name db-graph-dir)
+        init-conn-args (sqlite-cli/->open-db-args db-graph-dir)
+        db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
+        db-dir (if (= 1 (count init-conn-args)) (node-path/dirname (first init-conn-args)) (second init-conn-args))
         file-graph' (resolve-path file-graph)
         file-graph' (resolve-path file-graph)
-        conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath)})
+        conn (apply outliner-cli/init-conn (conj init-conn-args {:classpath (cp/get-classpath)}))
         directory? (.isDirectory (fs/statSync file-graph'))
         directory? (.isDirectory (fs/statSync file-graph'))
         user-options (cond-> (merge {:all-tags false} (dissoc options :verbose :files :help :continue))
         user-options (cond-> (merge {:all-tags false} (dissoc options :verbose :files :help :continue))
                        ;; coerce option collection into strings
                        ;; coerce option collection into strings
@@ -182,12 +172,11 @@
                        true
                        true
                        (set/rename-keys {:all-tags :convert-all-tags? :remove-inline-tags :remove-inline-tags?}))
                        (set/rename-keys {:all-tags :convert-all-tags? :remove-inline-tags :remove-inline-tags?}))
         _ (when (:verbose options) (prn :options user-options))
         _ (when (:verbose options) (prn :options user-options))
-        options' (merge {:user-options user-options
-                         :graph-name db-name}
+        options' (merge {:user-options user-options}
                         (select-keys options [:files :verbose :continue :debug]))]
                         (select-keys options [:files :verbose :continue :debug]))]
     (p/let [{:keys [import-state]}
     (p/let [{:keys [import-state]}
             (if directory?
             (if directory?
-              (import-file-graph-to-db file-graph' (node-path/join dir db-name) conn options')
+              (import-file-graph-to-db file-graph' db-dir conn options')
               (import-files-to-db file-graph' conn options'))]
               (import-files-to-db file-graph' conn options'))]
 
 
       (when-let [ignored-props (seq @(:ignored-properties import-state))]
       (when-let [ignored-props (seq @(:ignored-properties import-state))]

+ 4 - 17
deps/outliner/script/transact.cljs

@@ -1,34 +1,21 @@
 (ns transact
 (ns transact
   "This script generically runs transactions against the queried blocks"
   "This script generically runs transactions against the queried blocks"
-  (:require ["os" :as os]
-            ["path" :as node-path]
-            [clojure.edn :as edn]
-            [clojure.string :as string]
+  (:require [clojure.edn :as edn]
             [datascript.core :as d]
             [datascript.core :as d]
             [logseq.db.frontend.rules :as rules]
             [logseq.db.frontend.rules :as rules]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.db.common.sqlite-cli :as sqlite-cli]
             [logseq.outliner.db-pipeline :as db-pipeline]
             [logseq.outliner.db-pipeline :as db-pipeline]
             [nbb.core :as nbb]))
             [nbb.core :as nbb]))
 
 
-(defn- get-dir-and-db-name
-  "Gets dir and db name for use with open-db! Works for relative and absolute paths and
-   defaults to ~/logseq/graphs/ when no '/' present in name"
-  [graph-dir]
-  (if (string/includes? graph-dir "/")
-    (let [resolve-path' #(if (node-path/isAbsolute %) %
-                             ;; $ORIGINAL_PWD used by bb tasks to correct current dir
-                             (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
-      ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
-    [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
-
 (defn -main [args]
 (defn -main [args]
   (when (< (count args) 3)
   (when (< (count args) 3)
     (println "Usage: $0 GRAPH-DIR QUERY TRANSACT-FN")
     (println "Usage: $0 GRAPH-DIR QUERY TRANSACT-FN")
     (js/process.exit 1))
     (js/process.exit 1))
   (let [[graph-dir query* transact-fn*] args
   (let [[graph-dir query* transact-fn*] args
         dry-run? (contains? (set args) "-n")
         dry-run? (contains? (set args) "-n")
-        [dir db-name] (get-dir-and-db-name graph-dir)
-        conn (sqlite-cli/open-db! dir db-name)
+        open-db-args (sqlite-cli/->open-db-args graph-dir)
+        db-name (if (= 1 (count open-db-args)) (first open-db-args) (second open-db-args))
+        conn (apply sqlite-cli/open-db! open-db-args)
         ;; find blocks to update
         ;; find blocks to update
         query (into (edn/read-string query*) [:in '$ '%]) ;; assumes no :in are in queries
         query (into (edn/read-string query*) [:in '$ '%]) ;; assumes no :in are in queries
         transact-fn (edn/read-string transact-fn*)
         transact-fn (edn/read-string transact-fn*)

+ 9 - 4
deps/outliner/src/logseq/outliner/cli.cljs

@@ -46,10 +46,15 @@
    * :additional-config - Additional config map to merge into repo config.edn
    * :additional-config - Additional config map to merge into repo config.edn
    * :classpath - A java classpath string i.e. paths delimited by ':'. Used to find default config.edn
    * :classpath - A java classpath string i.e. paths delimited by ':'. Used to find default config.edn
      that comes with Logseq"
      that comes with Logseq"
-  [dir db-name & [opts]]
-  (fs/mkdirSync (node-path/join dir db-name) #js {:recursive true})
-  ;; Same order as frontend.db.conn/start!
-  (let [conn (sqlite-cli/open-db! dir db-name)]
+  [& args*]
+  (let [[args opts] (if (map? (last args*))
+                      [(butlast args*) (last args*)]
+                      [args* {}])
+        ;; Only mkdir when a dir and db-name are passed
+        _ (when (= 2 (count args))
+            (fs/mkdirSync (apply node-path/join args) #js {:recursive true}))
+        ;; Same order as frontend.db.conn/start!
+        conn (apply sqlite-cli/open-db! args)]
     (db-pipeline/add-listener conn)
     (db-pipeline/add-listener conn)
     (setup-init-data conn opts)
     (setup-init-data conn opts)
     conn))
     conn))

+ 10 - 10
scripts/src/logseq/tasks/db_graph/create_graph_with_properties.cljs

@@ -4,7 +4,6 @@
    NOTE: This script is also used in CI to confirm graph creation works"
    NOTE: This script is also used in CI to confirm graph creation works"
   (:require ["fs" :as fs]
   (:require ["fs" :as fs]
             ["fs-extra$default" :as fse]
             ["fs-extra$default" :as fse]
-            ["os" :as os]
             ["path" :as node-path]
             ["path" :as node-path]
             [babashka.cli :as cli]
             [babashka.cli :as cli]
             [cljs.pprint :as pprint]
             [cljs.pprint :as pprint]
@@ -18,7 +17,8 @@
             [logseq.db.frontend.property.type :as db-property-type]
             [logseq.db.frontend.property.type :as db-property-type]
             [logseq.outliner.cli :as outliner-cli]
             [logseq.outliner.cli :as outliner-cli]
             [nbb.classpath :as cp]
             [nbb.classpath :as cp]
-            [nbb.core :as nbb]))
+            [nbb.core :as nbb]
+            [logseq.db.common.sqlite-cli :as sqlite-cli]))
 
 
 (defn- date-journal-title [date]
 (defn- date-journal-title [date]
   (date-time-util/int->journal-title (date-time-util/date->int date) "MMM do, yyyy"))
   (date-time-util/int->journal-title (date-time-util/date->int date) "MMM do, yyyy"))
@@ -210,14 +210,15 @@
             (println (str "Usage: $0 GRAPH-NAME [OPTIONS]\nOptions:\n"
             (println (str "Usage: $0 GRAPH-NAME [OPTIONS]\nOptions:\n"
                           (cli/format-opts {:spec spec})))
                           (cli/format-opts {:spec spec})))
             (js/process.exit 1))
             (js/process.exit 1))
-        [dir db-name] (if (string/includes? graph-dir "/")
-                        ((juxt node-path/dirname node-path/basename) graph-dir)
-                        [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
-        db-path (node-path/join dir db-name "db.sqlite")
-        _ (when (fs/existsSync db-path)
+        init-conn-args (sqlite-cli/->open-db-args graph-dir)
+        db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
+        db-path (apply node-path/join init-conn-args)
+        ;; Only remove the directory if the directory is being overwritten
+        _ (when (and (= 2 (count init-conn-args)) (fs/existsSync db-path))
             (fse/removeSync db-path))
             (fse/removeSync db-path))
-        conn (outliner-cli/init-conn dir db-name {:additional-config (:config options)
-                                                  :classpath (cp/get-classpath)})
+        conn (apply outliner-cli/init-conn
+                    (conj init-conn-args {:additional-config (:config options)
+                                          :classpath (cp/get-classpath)}))
         init-data (create-init-data)
         init-data (create-init-data)
         _ (when (:file options) (fs/writeFileSync (:file options) (with-out-str (pprint/pprint init-data))))
         _ (when (:file options) (fs/writeFileSync (:file options) (with-out-str (pprint/pprint init-data))))
         {:keys [init-tx block-props-tx]} (outliner-cli/build-blocks-tx init-data)
         {:keys [init-tx block-props-tx]} (outliner-cli/build-blocks-tx init-data)
@@ -226,7 +227,6 @@
     (when (seq conflicting-names)
     (when (seq conflicting-names)
       (println "Error: Following names conflict -" (string/join "," conflicting-names))
       (println "Error: Following names conflict -" (string/join "," conflicting-names))
       (js/process.exit 1))
       (js/process.exit 1))
-    (println "DB dir: " (node-path/join dir db-name))
     (println "Generating" (count (filter :block/name init-tx)) "pages and"
     (println "Generating" (count (filter :block/name init-tx)) "pages and"
              (count (filter :block/title init-tx)) "blocks ...")
              (count (filter :block/title init-tx)) "blocks ...")
     (d/transact! conn init-tx)
     (d/transact! conn init-tx)

+ 7 - 8
scripts/src/logseq/tasks/db_graph/create_graph_with_schema_org.cljs

@@ -11,8 +11,6 @@
        type logseq doesnt' support yet
        type logseq doesnt' support yet
      * schema.org assumes no cardinality. For now, only :node properties are given a :cardinality :many"
      * schema.org assumes no cardinality. For now, only :node properties are given a :cardinality :many"
   (:require ["fs" :as fs]
   (:require ["fs" :as fs]
-            ["os" :as os]
-            ["path" :as node-path]
             [babashka.cli :as cli]
             [babashka.cli :as cli]
             [clojure.edn :as edn]
             [clojure.edn :as edn]
             [clojure.set :as set]
             [clojure.set :as set]
@@ -23,7 +21,8 @@
             [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.property :as db-property]
             [logseq.outliner.cli :as outliner-cli]
             [logseq.outliner.cli :as outliner-cli]
             [nbb.classpath :as cp]
             [nbb.classpath :as cp]
-            [nbb.core :as nbb]))
+            [nbb.core :as nbb]
+            [logseq.db.common.sqlite-cli :as sqlite-cli]))
 
 
 (defn- get-comment-string
 (defn- get-comment-string
   [rdfs-comment renamed-pages]
   [rdfs-comment renamed-pages]
@@ -406,11 +405,11 @@
             (println (str "Usage: $0 GRAPH-NAME [OPTIONS]\nOptions:\n"
             (println (str "Usage: $0 GRAPH-NAME [OPTIONS]\nOptions:\n"
                           (cli/format-opts {:spec spec})))
                           (cli/format-opts {:spec spec})))
             (js/process.exit 1))
             (js/process.exit 1))
-        [dir db-name] (if (string/includes? graph-dir "/")
-                        ((juxt node-path/dirname node-path/basename) graph-dir)
-                        [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
-        conn (outliner-cli/init-conn dir db-name {:additional-config (:config options)
-                                                  :classpath (cp/get-classpath)})
+        init-conn-args (sqlite-cli/->open-db-args graph-dir)
+        db-name (if (= 1 (count init-conn-args)) (first init-conn-args) (second init-conn-args))
+        conn (apply outliner-cli/init-conn
+                    (conj init-conn-args {:additional-config (:config options)
+                                          :classpath (cp/get-classpath)}))
         init-data (create-init-data (d/q '[:find [?name ...] :where [?b :block/name ?name]] @conn)
         init-data (create-init-data (d/q '[:find [?name ...] :where [?b :block/name ?name]] @conn)
                                     options)
                                     options)
         {:keys [init-tx block-props-tx]} (outliner-cli/build-blocks-tx init-data)]
         {:keys [init-tx block-props-tx]} (outliner-cli/build-blocks-tx init-data)]