diff --git a/src/computersandblues/lodestone/app.cljs b/src/computersandblues/lodestone/app.cljs index 079e47f..6e096ff 100644 --- a/src/computersandblues/lodestone/app.cljs +++ b/src/computersandblues/lodestone/app.cljs @@ -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] diff --git a/src/computersandblues/lodestone/database.cljs b/src/computersandblues/lodestone/database.cljs index 05fcc60..406c82a 100644 --- a/src/computersandblues/lodestone/database.cljs +++ b/src/computersandblues/lodestone/database.cljs @@ -1,4 +1,5 @@ -(ns computersandblues.lodestone.database) +(ns computersandblues.lodestone.database + (:refer-clojure :exclude [count get])) (defonce +db+ (atom nil)) @@ -72,15 +73,15 @@ request (.put store (clj->js object))] (promisify request)))) -(defn get! - ([store-id k] (get! @+db+ store-id k)) +(defn get + ([store-id k] (get @+db+ store-id k)) ([db store-id k] (let [store (open-store db store-id "readonly") request (.get store k)] (promisify request)))) -(defn get-all! - ([store-id key-range] (get-all! @+db+ store-id key-range)) +(defn get-all + ([store-id key-range] (get-all @+db+ store-id key-range)) ([db store-id key-range] (let [store (open-store db store-id "readonly") request (.getAll store key-range)] @@ -94,23 +95,23 @@ (promisify request)))) (defn delete! - ([store-id k] (get! @+db+ store-id k)) + ([store-id k] (get @+db+ store-id k)) ([db store-id k] (let [store (open-store db store-id "readwrite") request (.delete store k)] (promisify request)))) -(defn count! - ([store-id] (count! @+db+ store-id)) +(defn count + ([store-id] (count @+db+ store-id)) ([db store-id] (let [store (open-store db store-id "readonly") request (.count store)] (promisify request)))) -(defn open-cursor! - ([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)) +(defn open-cursor + ([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)) ([db store-id key-range {:keys [direction index]}] (let [store (open-store db store-id "readonly") key-range (if (= key-range ::all) @@ -161,13 +162,13 @@ (resolve @result))))))))) (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 (let [re (js/RegExp. "user" "i")] (do (print "starting…" (js/Date.)) - (-> (open-cursor! ::posts ::all) + (-> (open-cursor ::posts ::all) (transduce-cursor (comp (filter #(re-find re (.-content %))) (take 50) (map #(js->clj % :keywordize-keys true))))