db.cljs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. (ns frontend.db
  2. "Main entry ns for db related fns"
  3. (:require [frontend.config :as config]
  4. [frontend.db.conn :as conn]
  5. [frontend.db.model]
  6. [frontend.db.utils]
  7. [frontend.modules.outliner.op :as outliner-op]
  8. [frontend.modules.outliner.ui :as ui-outliner-tx]
  9. [frontend.namespaces :refer [import-vars]]
  10. [frontend.state :as state]
  11. [logseq.db :as ldb]
  12. [logseq.outliner.op]))
  13. (import-vars
  14. [frontend.db.conn
  15. ;; TODO: remove later
  16. get-repo-path
  17. get-repo-name
  18. get-short-repo-name
  19. get-db
  20. remove-conn!]
  21. [frontend.db.utils
  22. entity pull pull-many]
  23. [frontend.db.model
  24. delete-files get-block-and-children get-block-by-uuid get-block-children sort-by-order
  25. get-block-parent get-block-parents parents-collapsed?
  26. get-block-immediate-children get-block-page
  27. get-file file-exists? get-files-full
  28. get-latest-journals get-page get-case-page get-page-alias-names
  29. get-page-blocks-no-cache get-page-format
  30. journal-page? page? sub-block
  31. page-empty? page-exists? get-alias-source-page
  32. has-children? whiteboard-page?])
  33. (defn start-db-conn!
  34. ([repo]
  35. (start-db-conn! repo {}))
  36. ([repo option]
  37. (conn/start! repo option)))
  38. (def new-block-id ldb/new-block-id)
  39. (defn transact!
  40. ([tx-data]
  41. (transact! (state/get-current-repo) tx-data nil))
  42. ([repo tx-data]
  43. (transact! repo tx-data nil))
  44. ([repo tx-data tx-meta]
  45. (if config/publishing?
  46. ;; :save-block is for query-table actions like sorting and choosing columns
  47. (when (or (#{:collapse-expand-blocks :save-block} (:outliner-op tx-meta))
  48. (:init-db? tx-meta))
  49. (conn/transact! repo tx-data tx-meta))
  50. (ui-outliner-tx/transact! tx-meta
  51. (outliner-op/transact! tx-data tx-meta)))))
  52. (defn set-file-last-modified-at!
  53. "Refresh file timestamps to DB"
  54. [repo path last-modified-at]
  55. (when (and repo (not (config/db-based-graph? repo)) path last-modified-at)
  56. (transact! repo
  57. [{:file/path path
  58. :file/last-modified-at last-modified-at}] {})))
  59. (defn set-file-content!
  60. ([repo path content]
  61. (set-file-content! repo path content {}))
  62. ([repo path content opts]
  63. (when (and repo path)
  64. (let [tx-data {:file/path path
  65. :file/content content}]
  66. (transact! repo [tx-data] opts)))))