1
0

utils.cljs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. (ns frontend.db.utils
  2. "Some utils are required by other namespace in frontend.db package."
  3. (:require [datascript.core :as d]
  4. [frontend.state :as state]
  5. [datascript.transit :as dt]
  6. [frontend.db.conn :as conn]
  7. [frontend.config :as config]
  8. [logseq.graph-parser.util :as gp-util]
  9. [clojure.string :as string]
  10. [logseq.graph-parser.util.page-ref :as page-ref]))
  11. ;; transit serialization
  12. (defn db->string [db]
  13. (dt/write-transit-str db))
  14. (defn db->json [db]
  15. (js/JSON.stringify
  16. (into-array
  17. (for [d (d/datoms db :eavt)]
  18. #js [(:e d) (name (:a d)) (:v d)]))))
  19. (defn db->edn-str [db]
  20. (pr-str db))
  21. (defn string->db [s]
  22. (dt/read-transit-str s))
  23. (defn seq-flatten [col]
  24. (flatten (seq col)))
  25. (defn group-by-page
  26. [blocks]
  27. (if (:block/page (first blocks))
  28. (some->> blocks
  29. (group-by :block/page))
  30. blocks))
  31. (defn get-tx-id [tx-report]
  32. (get-in tx-report [:tempids :db/current-tx]))
  33. (defn get-max-tx-id
  34. [db]
  35. (:max-tx db))
  36. (defn entity
  37. "This function will return nil if passed `id-or-lookup-ref` is an integer and
  38. the entity doesn't exist in db.
  39. `repo-or-db`: a repo string or a db,
  40. `id-or-lookup-ref`: same as d/entity."
  41. ([id-or-lookup-ref]
  42. (entity (state/get-current-repo) id-or-lookup-ref))
  43. ([repo-or-db id-or-lookup-ref]
  44. (when-let [db (if (string? repo-or-db)
  45. ;; repo
  46. (let [repo (or repo-or-db (state/get-current-repo))]
  47. (conn/get-db repo))
  48. ;; db
  49. repo-or-db)]
  50. (d/entity db id-or-lookup-ref))))
  51. (defn special-id->page
  52. "Convert special id backs to page name."
  53. [content refs]
  54. (reduce
  55. (fn [content ref]
  56. (if (:block/name ref)
  57. (string/replace content (str config/page-ref-special-chars (:block/uuid ref)) (:block/original-name ref))
  58. content))
  59. content
  60. refs))
  61. (defn special-id-ref->page
  62. "Convert special id ref backs to page name."
  63. [content refs]
  64. (reduce
  65. (fn [content ref]
  66. (if (:block/name ref)
  67. (string/replace content
  68. (str page-ref/left-brackets
  69. config/page-ref-special-chars
  70. (:block/uuid ref)
  71. page-ref/right-brackets)
  72. (:block/original-name ref))
  73. content))
  74. content
  75. refs))
  76. (defn update-block-content
  77. "Replace `[[internal-id]]` with `[[page name]]`"
  78. [item eid]
  79. (if (config/db-based-graph? (state/get-current-repo))
  80. (if-let [content (:block/content item)]
  81. (let [refs (:block/refs (entity eid))]
  82. (assoc item :block/content (special-id->page content refs)))
  83. item)
  84. item))
  85. (defn pull
  86. ([eid]
  87. (pull (state/get-current-repo) '[*] eid))
  88. ([selector eid]
  89. (pull (state/get-current-repo) selector eid))
  90. ([repo selector eid]
  91. (when-let [db (conn/get-db repo)]
  92. (let [result (d/pull db selector eid)]
  93. (update-block-content result eid)))))
  94. (defn pull-many
  95. ([eids]
  96. (pull-many '[*] eids))
  97. ([selector eids]
  98. (pull-many (state/get-current-repo) selector eids))
  99. ([repo selector eids]
  100. (when-let [db (conn/get-db repo)]
  101. (let [selector (if (some #{:db/id} selector) selector (conj selector :db/id))]
  102. (->> (d/pull-many db selector eids)
  103. (map #(update-block-content % (:db/id %))))))))
  104. (defn- actual-transact!
  105. [repo-url tx-data tx-meta]
  106. (let [tx-data (gp-util/fast-remove-nils tx-data)]
  107. (when (seq tx-data)
  108. ;; (prn :debug "DB transact:")
  109. ;; (frontend.util/pprint {:tx-data tx-data
  110. ;; :tx-meta tx-meta})
  111. (when-let [conn (conn/get-db repo-url false)]
  112. (if tx-meta
  113. (d/transact! conn (vec tx-data) tx-meta)
  114. (d/transact! conn (vec tx-data)))))))
  115. (if config/publishing?
  116. (defn- transact!*
  117. [repo-url tx-data tx-meta]
  118. ;; :save-block is for query-table actions like sorting and choosing columns
  119. (when (#{:collapse-expand-blocks :save-block} (:outliner-op tx-meta))
  120. (actual-transact! repo-url tx-data tx-meta)))
  121. (def transact!* actual-transact!))
  122. (defn transact!
  123. ([tx-data]
  124. (transact! (state/get-current-repo) tx-data))
  125. ([repo-url tx-data]
  126. (transact! repo-url tx-data nil))
  127. ([repo-url tx-data tx-meta]
  128. (transact!* repo-url tx-data tx-meta)))
  129. (defn get-key-value
  130. ([key]
  131. (get-key-value (state/get-current-repo) key))
  132. ([repo-url key]
  133. (when-let [db (conn/get-db repo-url)]
  134. (some-> (d/entity db key)
  135. key))))
  136. (defn q
  137. [query & inputs]
  138. (when-let [repo (state/get-current-repo)]
  139. (apply d/q query (conn/get-db repo) inputs)))