<commit_before>(ns lokate.db)

(def db (atom nil))

(defn error [e]
  (.error js/console "An IndexedDB error has occured!" e))

(defn new [cb]
  (let [version 1
        request (.open js/indexedDB "lokate" version)]
    (set! (.-onupgradeneeded request) (fn [e]
                                        (reset! db (.. e -target -result))
                                        (set! (.. e -target -transaction -onerror) error)
                                        (.createObjectStore @db "collection" #js {:keyPath "id"})
                                        (.createObjectStore @db "resource" #js {:keyPath "id"})
                                        (.createObjectStore @db "setting" #js {:keyPath "id"})
                                        (.createObjectStore @db "history")))
    (set! (.-onsuccess request) (fn [e]
                                  (reset! db (.. e -target -result))
                                  (cb)))
    (set! (.-onerror request) error)))

(defn add
  ([db-name data]
   (add db-name data nil))
  ([db-name data key]
   (let [transaction (.transaction @db #js [db-name] "readwrite")
         store (.objectStore transaction db-name)
         request (if key
                   (.put store (clj->js data) key)
                   (.put store (clj->js data)))]
     (set! (.-onerror request) error))))

(defn delete [db-name key]
  (let [transaction (.transaction @db #js [db-name] "readwrite")
        store (.objectStore transaction db-name)
        request (.delete store (if (keyword? key) (name key) key))]
    (set! (.-onerror request) error)))

(defn get-all [db-name for-each cb]
  (let [transaction (.transaction @db #js [db-name] "readonly")
        store (.objectStore transaction db-name)
        keyRange (.lowerBound js/IDBKeyRange 0)
        cursorRequest (.openCursor store keyRange)]
    (set! (.-onsuccess cursorRequest)
      (fn [e]
        (if-let [result (.. e -target -result)]
          (do
            (for-each result)
            (.continue result))
          (cb))))))

(defn init-remoteStorage []
  (.defineModule js/RemoteStorage "collections"
    (fn [privClient pubClient]

      (.declareType privClient "collection"
        (clj->js
          {:description "a collection of units"
           :type "object"
           :properties {:id {:type "string"
                             :format "id"}
                        :title {:type "string"}}}))
      (clj->js
        {:exports
         {:addCollection (fn [collection]
                            (.storeObject privClient "collection"
                              (name (:id collection)) collection))
          :getCollections (fn []
                            (.getAll privClient "collection"
                              #(.log js/console (str "yea! " %))
                              #(.log js/console (str "error " %))))}})))

  (-> js/remoteStorage .-access (.claim "collections" "rw"))
  (.on js/remoteStorage "connecting" #(.log js/console "connecting"))
  (.on js/remoteStorage "authing" #(.log js/console "authing"))
  (.on js/remoteStorage "error" #(.log js/console "error"))
  (.on js/remoteStorage "connected" #(.log js/console "connected"))<commit_msg>remoteStorage works well and already uses indexeddb for caching & sync when offline<commit_after>(ns lokate.db
  (:require [goog.object :as gobject]))

(def db (atom nil))

(defn error [e]
  (.error js/console "An IndexedDB error has occured!" e))

(defn new [cb]
  (let [version 1
        request (.open js/indexedDB "lokate" version)]
    (set! (.-onupgradeneeded request) (fn [e]
                                        (reset! db (.. e -target -result))
                                        (set! (.. e -target -transaction -onerror) error)
                                        (.createObjectStore @db "collection" #js {:keyPath "id"})
                                        (.createObjectStore @db "resource" #js {:keyPath "id"})
                                        (.createObjectStore @db "setting" #js {:keyPath "id"})
                                        (.createObjectStore @db "history")))
    (set! (.-onsuccess request) (fn [e]
                                  (reset! db (.. e -target -result))
                                  (cb)))
    (set! (.-onerror request) error)))

(defn add
  ([db-name data]
   (add db-name data nil))
  ([db-name data key]
   (let [transaction (.transaction @db #js [db-name] "readwrite")
         store (.objectStore transaction db-name)
         request (if key
                   (.put store (clj->js data) key)
                   (.put store (clj->js data)))]
     (set! (.-onerror request) error))))

(defn delete [db-name key]
  (let [transaction (.transaction @db #js [db-name] "readwrite")
        store (.objectStore transaction db-name)
        request (.delete store (if (keyword? key) (name key) key))]
    (set! (.-onerror request) error)))

(defn get-all [db-name for-each cb]
  (let [transaction (.transaction @db #js [db-name] "readonly")
        store (.objectStore transaction db-name)
        keyRange (.lowerBound js/IDBKeyRange 0)
        cursorRequest (.openCursor store keyRange)]
    (set! (.-onsuccess cursorRequest)
      (fn [e]
        (if-let [result (.. e -target -result)]
          (do
            (for-each result)
            (.continue result))
          (cb))))))

(defn init-remoteStorage []
  (.defineModule js/RemoteStorage "collections"
    (fn [privClient pubClient]

      (.declareType privClient "collection"
        (clj->js
          {:description "a collection of units"
           :type "object"
           :properties {:id {:type "string"
                             :format "id"}
                        :title {:type "string"}}}))
      (clj->js
        {:exports
         {:addCollection (fn [collection]
                            (.storeObject privClient "collection"
                              (name (:id collection)) (clj->js collection)))
          :getCollections (fn []
                            (-> privClient
                              (.getAll "")
                              (.then
                                #(.log js/console (pr-str (js->clj %)))
                                #(.log js/console (str "error " %)))))}})))

  (-> js/remoteStorage .-access (.claim "collections" "rw"))
  (.on js/remoteStorage "connecting" #(.log js/console "connecting"))
  (.on js/remoteStorage "authing" #(.log js/console "authing"))
  (.on js/remoteStorage "error" #(.log js/console (str "error " %)))
  (.on js/remoteStorage "connected" #(.log js/console "connected"))
{'fraction_non_alphanumeric': 0.14506828528072838, 'fraction_numerical': 0.0006069802731411229, 'mean_word_length': 1.6533816425120773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7284946', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1794, 'n_words': 552}