create_graph.cljs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. (ns create-graph
  2. "An example script that creates a DB graph given a sqlite.build EDN file"
  3. (:require [logseq.outliner.db-pipeline :as db-pipeline]
  4. [clojure.string :as string]
  5. [clojure.edn :as edn]
  6. [datascript.core :as d]
  7. ["path" :as node-path]
  8. ["os" :as os]
  9. ["fs" :as fs]
  10. [nbb.classpath :as cp]
  11. [nbb.core :as nbb]))
  12. (defn- resolve-path
  13. "If relative path, resolve with $ORIGINAL_PWD"
  14. [path]
  15. (if (node-path/isAbsolute path)
  16. path
  17. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  18. (defn -main [args]
  19. (when (not= 2 (count args))
  20. (println "Usage: $0 GRAPH-DIR EDN-PATH")
  21. (js/process.exit 1))
  22. (let [[graph-dir edn-path] args
  23. [dir db-name] (if (string/includes? graph-dir "/")
  24. ((juxt node-path/dirname node-path/basename) graph-dir)
  25. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
  26. sqlite-build-edn (-> (resolve-path edn-path) fs/readFileSync str edn/read-string)
  27. conn (db-pipeline/init-conn dir db-name {:classpath (cp/get-classpath)})
  28. {:keys [init-tx block-props-tx]} (db-pipeline/build-blocks-tx sqlite-build-edn)]
  29. (println "Generating" (count (filter :block/name init-tx)) "pages and"
  30. (count (filter :block/content init-tx)) "blocks ...")
  31. (d/transact! conn init-tx)
  32. (d/transact! conn block-props-tx)
  33. (println "Created graph" (str db-name "!"))))
  34. (when (= nbb/*file* (:file (meta #'-main)))
  35. (-main *command-line-args*))