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

View file

@ -1,4 +1,5 @@
(ns computersandblues.lodestone.database) (ns computersandblues.lodestone.database
(:refer-clojure :exclude [count get]))
(defonce +db+ (atom nil)) (defonce +db+ (atom nil))
@ -72,15 +73,15 @@
request (.put store (clj->js object))] request (.put store (clj->js object))]
(promisify request)))) (promisify request))))
(defn get! (defn get
([store-id k] (get! @+db+ store-id k)) ([store-id k] (get @+db+ store-id k))
([db store-id k] ([db store-id k]
(let [store (open-store db store-id "readonly") (let [store (open-store db store-id "readonly")
request (.get store k)] request (.get store k)]
(promisify request)))) (promisify request))))
(defn get-all! (defn get-all
([store-id key-range] (get-all! @+db+ store-id key-range)) ([store-id key-range] (get-all @+db+ store-id key-range))
([db store-id key-range] ([db store-id key-range]
(let [store (open-store db store-id "readonly") (let [store (open-store db store-id "readonly")
request (.getAll store key-range)] request (.getAll store key-range)]
@ -94,23 +95,23 @@
(promisify request)))) (promisify request))))
(defn delete! (defn delete!
([store-id k] (get! @+db+ store-id k)) ([store-id k] (get @+db+ store-id k))
([db store-id k] ([db store-id k]
(let [store (open-store db store-id "readwrite") (let [store (open-store db store-id "readwrite")
request (.delete store k)] request (.delete store k)]
(promisify request)))) (promisify request))))
(defn count! (defn count
([store-id] (count! @+db+ store-id)) ([store-id] (count @+db+ store-id))
([db store-id] ([db store-id]
(let [store (open-store db store-id "readonly") (let [store (open-store db store-id "readonly")
request (.count store)] request (.count store)]
(promisify request)))) (promisify request))))
(defn open-cursor! (defn open-cursor
([store-id key-range] (open-cursor! @+db+ store-id key-range {:direction :asc})) ([store-id key-range] (open-cursor @+db+ store-id key-range {:direction :asc}))
([store-id key-range opts] (open-cursor! @+db+ store-id key-range opts)) ([store-id key-range opts] (open-cursor @+db+ store-id key-range opts))
([db store-id key-range {:keys [direction index]}] ([db store-id key-range {:keys [direction index]}]
(let [store (open-store db store-id "readonly") (let [store (open-store db store-id "readonly")
key-range (if (= key-range ::all) key-range (if (= key-range ::all)
@ -161,13 +162,13 @@
(resolve @result))))))))) (resolve @result)))))))))
(defn first-result [xform cursor-req] (defn first-result [xform cursor-req]
(transduce-cursor (comp xform (take 1)) (fn [_ x] x) cursor-req)) (transduce-cursor (comp xform (take 1)) (fn [_ x] x) nil cursor-req))
(comment (comment
(let [re (js/RegExp. "user" "i")] (let [re (js/RegExp. "user" "i")]
(do (print "starting…" (js/Date.)) (do (print "starting…" (js/Date.))
(-> (open-cursor! ::posts ::all) (-> (open-cursor ::posts ::all)
(transduce-cursor (comp (filter #(re-find re (.-content %))) (transduce-cursor (comp (filter #(re-find re (.-content %)))
(take 50) (take 50)
(map #(js->clj % :keywordize-keys true)))) (map #(js->clj % :keywordize-keys true))))