   "Resources are any thing stored in the open company platform: companies, reports"
   (:require [clojure.string :as string]
             [clojure.set :as cset]
+            [clojure.core.async :as async]
             [schema.core :as s]
             [clojure.walk :refer (keywordize-keys)]
             [clj-time.format :as format]

          :created-at (:created-at original-resource)
          :updated-at timestamp})
        update (pool/with-pool [conn pool/rethinkdb-pool]
                 (-> (r/table table-name)
                     (r/get (original-resource primary-key-name))
                     (r/replace timed-resource)
                     (r/run conn)))]
  (if (or (= 1 (:replaced update)) (= 1 (:unchanged update)))
    timed-resource
    (throw (RuntimeException. (str "RethinkDB update failure: " update)))))))

  "Delete the specified resource and return `true`."
  ([table-name primary-key-value]
   (let [delete (pool/with-pool [conn pool/rethinkdb-pool]
                  (with-timeout default-timeout
                    (-> (r/table table-name)
                        (r/get primary-key-value)
                        (r/delete)
                        (r/run conn))))]
    (if (= 1 (:deleted delete))
      true
      (throw (RuntimeException. (str "RethinkDB delete failure: " delete))))))

  ([table-name key-name key-value]
   (let [delete (pool/with-pool [conn pool/rethinkdb-pool]
                  (with-timeout default-timeout
                    (-> (r/table table-name)
                        (r/get-all [key-value] {:index key-name})
                        (r/delete)
                        (r/run conn))))]
     (if (zero? (:errors delete))
      true
      (throw (RuntimeException. (str "RethinkDB delete failure: " delete)))))))
