transact.cljs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (ns transact
  2. "This script generically runs transactions against the queried blocks"
  3. (:require ["os" :as os]
  4. ["path" :as node-path]
  5. [clojure.edn :as edn]
  6. [clojure.string :as string]
  7. [datascript.core :as d]
  8. [logseq.db.frontend.rules :as rules]
  9. [logseq.db.sqlite.cli :as sqlite-cli]
  10. [logseq.outliner.db-pipeline :as db-pipeline]
  11. [nbb.core :as nbb]))
  12. (defn- get-dir-and-db-name
  13. "Gets dir and db name for use with open-db! Works for relative and absolute paths and
  14. defaults to ~/logseq/graphs/ when no '/' present in name"
  15. [graph-dir]
  16. (if (string/includes? graph-dir "/")
  17. (let [resolve-path' #(if (node-path/isAbsolute %) %
  18. ;; $ORIGINAL_PWD used by bb tasks to correct current dir
  19. (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
  20. ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
  21. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
  22. (defn -main [args]
  23. (when (< (count args) 3)
  24. (println "Usage: $0 GRAPH-DIR QUERY TRANSACT-FN")
  25. (js/process.exit 1))
  26. (let [[graph-dir query* transact-fn*] args
  27. dry-run? (contains? (set args) "-n")
  28. [dir db-name] (get-dir-and-db-name graph-dir)
  29. conn (sqlite-cli/open-db! dir db-name)
  30. ;; find blocks to update
  31. query (into (edn/read-string query*) [:in '$ '%]) ;; assumes no :in are in queries
  32. transact-fn (edn/read-string transact-fn*)
  33. blocks-to-update (mapv first (d/q query @conn (rules/extract-rules rules/db-query-dsl-rules)))
  34. ;; TODO: Use sci eval when it's available in nbb-logseq
  35. update-tx (mapv (fn [id] (eval (list transact-fn id)))
  36. blocks-to-update)]
  37. (if dry-run?
  38. (do (println "Would update" (count blocks-to-update) "blocks with the following tx:")
  39. (prn update-tx)
  40. (println "With the following blocks updated:")
  41. (prn (map #(select-keys (d/entity @conn %) [:block/name :block/title]) blocks-to-update)))
  42. (do
  43. (db-pipeline/add-listener conn)
  44. (d/transact! conn update-tx)
  45. (println "Updated" (count update-tx) "block(s) for graph" (str db-name "!"))))))
  46. (when (= nbb/*file* (nbb/invoked-file))
  47. (-main *command-line-args*))