   "create entity with attrs, return entity"
   [attrs]
   (let [new-id (d/tempid :entities)
-        {:keys [db-after tempids]} @(d/transact *conn*
+        {:keys [db-after tempids]} @(d/transact conn
                                                 [(assoc attrs :db/id new-id)])]
     (->> (d/resolve-tempid db-after tempids new-id)
          (d/entity db-after))))
 
 (defn user-get-preferences
-  [user-id]
-  (->> (d/pull (d/db *conn*)
+  [conn user-id]
+  (->> (d/pull (d/db conn)
                [{:user/preferences [:user.preference/key :user.preference/value]}]
                [:user/id user-id])
        :user/preferences

              [?u :user/id ?user-id]
              [?u :user/email ?email]
              [?i :invite/to ?email]]
            (d/db *conn*) user-id)
       (map (comp db->invitation first))))

(defn get-open-thread-ids-for-user
  [user-id]
  (d/q '[:find [?thread-id ...]
                :in $ ?user-id
                :where
                [?e :user/id ?user-id]
                [?e :user/open-thread ?thread]
                [?thread :thread/id ?thread-id]]
       (d/db *conn*)
       user-id))

(defn get-group
  [group-id]
  (-> (d/pull (d/db *conn*) group-pull-pattern [:group/id group-id])
      db->group))

(defn get-groups-for-user [user-id]
  (->> (d/q '[:find [?g ...]
              :in $ ?user-id
              :where
              [?u :user/id ?user-id]
              [?g :group/user ?u]]
            (d/db *conn*)
            user-id)
       (d/pull-many (d/db *conn*) group-pull-pattern)
       (map (comp #(dissoc % :users) db->group))
       set))

(defn get-users-in-group [group-id]
  (->> (d/q '[:find (pull ?u pull-pattern)
              :in $ ?group-id pull-pattern
              :where
              [?g :group/id ?group-id]
              [?g :group/user ?u]]
            (d/db *conn*)
            group-id
            user-pull-pattern)
       (map (comp db->user first))
       set))

(defn get-user-visible-tag-ids
  [user-id]
  (->> (d/q '[:find ?tag-id
              :in $ ?user-id
              :where

                [?u :user/id ?user-id]
                [?g :group/user ?u]
                [?t :tag/group ?g]]
              (d/db conn) user-id)
         (map (fn [[tag]]
                (db->tag (merge tag (tag-stats (tag :tag/id))))))
         (into #{}))))
