                         :port (port)
                         :db   (db))]
     (swap! connection (fn [_] conn))))
+
+
+;; Public API
+(defn connect
+  "Connect ro rethink cluster"
+  []
+  (when (not (connected?))
+    (_connect)))
 
 (defn initialize
   "Initialize the database by creating it and its data structures."

;; Connection atom contains the connection object
(def connection (atom nil))


;; Public API
(defn connect
  "Connect ro rethink cluster"
  []
  (logger/debug (host))
  (logger/debug (port))

(defn create-table
  "Create a table with given name and specs"
  ([name]
   (connect)
   ;; Query execution
   (-> (r/db (db))
       (r/table-create name)
       (r/run @connection)))

  ([name spec-map]
   (connect)
   (let [index-name (get spec-map :index-name)
         index-fn   (get spec-map :index-fn (fn [row] (r/get-field row index-name)))]
     ;; Query execution
     (-> (r/db (db))
         (r/table-create name)
         (when (not (nil? index-name))
           (r/index-create index-name index-fn))
         (r/run @connection)))))

(defn disconnect
  "Disconnect from rethink cluster"
  []
  (when (connected?)
    (logger/debug "Disconnected from rethink")
    (r/close @connection)
    (reset! connection nil)))
