Shout less in database wrapper

This commit is contained in:
arne 2025-11-21 13:15:00 +01:00
commit 54067c2103
2 changed files with 42 additions and 23 deletions

View file

@ -160,7 +160,7 @@
(declare fetch-posts!)
(defn- fetch-application-settings []
(->> (db/open-cursor! ::db/application ::db/all)
(->> (db/open-cursor ::db/application ::db/all)
(db/first-result (map #(js->clj % :keywordize-keys true)))))
(defn setup-application!
@ -198,7 +198,7 @@
(.then (fn [application]
(when application
(swap! state assoc :section :posts)
(promise-all [application (db/count! ::db/posts)]))))
(promise-all [application (db/count ::db/posts)]))))
(.then (fn [[application post-count]]
(when post-count
(if (zero? post-count)
@ -295,10 +295,10 @@
(map #(js->clj % :keywordize-keys true)))
refresh-id (now)]
(swap! state update-in [:section/posts :loading] conj refresh-id)
(. (promise-all [(db/count! ::db/posts)
(->> (db/open-cursor! ::db/posts ::db/all
{:index ::db/post-created-at
:direction :desc})
(. (promise-all [(db/count ::db/posts)
(->> (db/open-cursor ::db/posts ::db/all
{:index ::db/post-created-at
:direction :desc})
(db/transduce-cursor xform))])
(then (fn [[total displayed-posts]]
(swap! state update :section/posts #(-> (assoc % :total total)
@ -370,6 +370,24 @@
[:div [:strong "Unsupported attachment"]
[debug attachment]])))
(comment
; query current results
(-> @state :section/posts :displayed-posts)
; run and time a query on the database
(do
(js/console.log :start (.toISOString (js/Date.)))
(. (->> (db/open-cursor ::db/posts ::db/all)
(db/transduce-cursor (comp (mapcat (j/get :tags))
(map (j/get :name))
(filter #(= "typescript" %))
(take 50)) conj (sorted-set)))
(then (fn [result]
(js/console.log :end (.toISOString (js/Date.)))
(js/console.log :accts result)))))
)
(defn post [{:keys [post]}]
; TODO: handle (:sensitive post)
[:article.post
@ -432,11 +450,11 @@
"Returns a promise which resolves to the smallest or largest internal post id.
This is useful to continue interrupted paginated requests."
[max-or-min]
(->> (db/open-cursor! ::db/posts ::db/all {:index ::db/post-internal-id
:direction (if (= max-or-min :min) :asc :desc)})
(->> (db/open-cursor ::db/posts ::db/all {:index ::db/post-internal-id
:direction (if (= max-or-min :min) :asc :desc)})
(db/first-result (keep (j/get :internal_id)))))
(defn fetch-more-posts! [e]
(defn- fetch-more-posts! [e]
(.preventDefault e)
(. (promise-all [(fetch-application-settings) (internal-post-id :min) (internal-post-id :max)])
(then (fn [[application min-id max-id]]
@ -535,7 +553,7 @@
(defn- convert-internal-ids! []
; TODO figure out when we can remove this again
(. (->> (db/open-cursor! ::db/posts ::db/all)
(. (->> (db/open-cursor ::db/posts ::db/all)
(db/transduce-cursor (filter (comp string? (j/get :internal_id)))))
(then (fn [rows]
(doseq [row rows]