 
 (comment
   ;create store
-  (go (def store (<! (new-fs-store "mystore"))))
-  ;delete store
+  (go (def store (<! (new-fs-store "/tmp/mystore"))))
+
   (delete-store "mystore")
   ;exists?
-  (go (println (<! (-exists? store "ne1w.txt"))))
+  
+  (go (println (<! (-assoc-in store [:new] 43))))
+  (go (println (<! (-exists? store :new)))) 
+  (go (println (<! (-update-in store [:new] inc))))
+  (go (println (<! (-get-in store [:new]))))
+  (go (println (<! (-dissoc store :new))))
+
+
 ;;Input, Outputstream
 
 (def my-obj (clj->js {:key "123" :format :edn}))
 
 (defn- read-key
   [filename key]
-  (when (fs.existsSync path)
-    (let [rs (fs.createReadStream path)]
+  (when (.existsSync fs path)
+    (let [rs (.createReadStream fs path)]
       (.on rs "data" (fn [chunk] (set! scope (.toString chunk))))
       (.on rs "close" #(println "Reading done"))
       (.on rs "error" (fn [err] (println "error: " err))))))
 
 
-(defn write-edn-key [folder {:keys [key] :as meta}]
-  (let [key       (uuid key)
-        temp-file (str folder "/meta/" key ".new")
-        new-file  (str folder "/meta/" key)
-        fd        (fs.openSync new-file "w+")
-        _         (fs.closeSync fd)
-        ws        (fs.createWriteStream temp-file)]
-    (.on ws "close" #(try (fs.renameSync temp-file new-file) (catch js/Object err (println err))))
-    (.write ws (prn-str meta))
-    (.end ws)))
-
-(defn write-edn [folder key value]
-  (let [key       (uuid key)
-        temp-file (str folder "/data/" key ".new")
-        new-file  (str folder "/data/" key)
-        old       (read-key new-file key)
-        fd        (fs.openSync new-file "w+")
-        _         (fs.closeSync fd)
-        ws        (fs.createWriteStream temp-file)]
-    (.on ws "close" #(try
-                       (fs.renameSync temp-file new-file)
-                       (catch js/Object err (println err))))
-    (.write ws (prn-str value))
-    (.end ws)))
-
-(defn read-edn [path key]
-  (let [file-name (str path "/meta/" (uuid key))
-        res-ch    (chan)]
-    (when (fs.existsSync file-name)
-      (let [rs      (fs.createReadStream file-name)]
-        (.on rs "data" (fn [chunk] (put! res-ch  (cljs.reader/read-string (.toString chunk)))))
-        (.on rs "close" #(close! res-ch))
-        (.on rs "error" (fn [err] (close! res-ch) (println "error: " err)))
-        res-ch))))
+
+
+
+
+
+
+
 
 (write-edn-key "/tmp" {:key "123" :format :edn})
+
+
+
+
+
 
 (def rc (chan))
 

(defn delete-store
  "Permanently deletes the folder of the store with all files."
  [folder]
  (if (fs.existsSync folder)
    (try
      (doseq [path [(str folder "/meta") (str folder "/data") folder]]
        (when (fs.existsSync path)
          (doseq [file (fs.readdirSync path)]
            (let [file-path (str path "/" file)]
              (when (fs.existsSync file-path)
                (if (.isDirectory (fs.statSync file-path))
                  (fs.rmdirSync file-path)
                  (fs.unlinkSync file-path)))))))
      (catch js/Object err
        (println err))
      (finally
        (fs.rmdirSync folder)))
    "folder not exists"))

(defn check-and-create-folder
  "Creates Folder with given path."
  [path]
  (let [test-file (str path "/" (random-uuid))]
    (try
      (when-not (fs.existsSync path)
        (fs.mkdirSync path))
      (fs.writeFileSync test-file "test-data")
      (fs.unlinkSync test-file)
      (catch js/Object err
        (println err)))))

(defrecord FileSystemNodejsStore
    [folder serializer read-handlers write-handlers locks config]
  PEDNAsyncKeyValueStore
  (-exists? [this key]
    (let [fn  (uuid key)
          f   (str folder "/data/" key)
          res (chan)]
      (put! res (fs.existsSync f))
      (close! res)
      res)))

(defn new-fs-store
  "Filestore contains a Key and a Data Folder"

            [konserve.core :as k]
            [hasch.core :refer [uuid]]
            [konserve.serializers :as ser]
            [konserve.protocols :refer [PEDNAsyncKeyValueStore -exists? -get-in -update-in -assoc-in -dissoc
                                        PJSONAsyncKeyValueStore -jget-in -jassoc-in -jupdate-in
                                        PBinaryAsyncKeyValueStore -bget -bassoc
                                        -serialize -deserialize]]
            [cljs.core.async :as async :refer (take! <! >! put! close! chan poll!)])
  (:require-macros [cljs.core.async.macros :refer [go go-loop]]))

(defonce fs (node/require "fs"))
