 
 (defn store-reference
   "Store reference between tweet and another entity"
-  [source target type]
+  [source target type db]
   (mc/insert-and-return
-   @db
+   db
    "references"
    {:source source
     :target target

    :ts (t/now)}))


(defn store-hashtag [text]
  (mc/insert-and-return
   @db
   "hashtags-2"
   {:text text
    :ts (t/now)}))


(defn store-author [{:keys [screen_name id created_at]}]
  (let [date (f/parse custom-formatter created_at)]
    (mc/insert-and-return
     @db
     "authors"
     {:id id
      :name screen_name

      :ts (t/now)})))


(defn store-url [url db]
  (mc/insert-and-return
   db
   "urls-2"
   {:path url
    :ts (t/now)}))


(defn store-message
  [text source tid db]
  (mc/insert-and-return
   db
   "messages"
   {:text text
    :source source
    :ts (t/now) :tweet tid}))



(comment

  (set-db "athena")

  ;; migration db
  (def db2 (let [^MongoOptions opts (mg/mongo-options :threads-allowed-to-block-for-connection-multiplier 300)
                 ^ServerAddress sa  (mg/server-address (or (System/getenv "DB_PORT_27017_TCP_ADDR") "127.0.0.1") 27017)]
             (mg/get-db (mg/connect sa opts) "demeter")))


  ;; migrate users
  (->> (mc/find-maps @db "publications")
       (pmap (fn [{:keys [url tweet user hashtags type]}]
               (let [text (:text (mc/find-map-by-id @db "tweets" tweet))
                     mid (-> (store-message text (= type "source") tweet db2)
                             from-db-object
                             :_id)]
                 (map #(store-reference mid % "tag" db2) hashtags)
                 (store-reference mid user "pub" db2)
                 (when url
                   (store-reference mid url "url" db2)))
                 ))))

  (->> (mc/find-maps @db "publications")
       (take 100)
       (map :type)
       (into #{}))
  )
