Fork me on GitHub
#mount
<
2017-02-01
>
bill21:02:17

just found mount 🎉 been looking for ways to manage state without passing an extra parameter to every function (so Component’s out) Question: if I want to use Mount to e.g. connect to two databases at once, would I use Yurt for that? Say I want to run a database upgrade, reading from one database and writing to another.

tolitius21:02:01

hey @bill, good find 🙂

tolitius21:02:19

if these are two different databases, they are two different states

tolitius21:02:30

i.e. you can use mount

tolitius21:02:20

(defstate from-db :start (connect {...}) :stop ...)
(defstate to-db :start (connect {...}) :stop ...)

bill21:02:22

thanks @tolitius I need to clarify…

bill21:02:21

(lame pseudo-code on its way…)

tolitius21:02:46

sure, let's see it

bill21:02:08

(ns db-access
  (:require [my-ns :refer [db]]))

(defn people
  (query db "select * from people"))

(defn people-add [peeps]
  (bulk-insert db "insert into people" peeps))

bill21:02:37

I have a single db access namespace that calls some db library to do the actual interaction

bill21:02:05

so I’d use Mount to produce/consume the db connection parameters

tolitius21:02:53

yea, unless I am missing something, you are using a single state (i.e. a single connection, a single resource) that you use to perform different actions: select, insert, update...

bill22:02:16

ya and that state has to be different for each database cnxn

tolitius22:02:16

if that's correct, mount will connect to this database and will be out of your way

tolitius22:02:12

ah.. hold on. you are using db access for more than one database in the same runtime?

bill22:02:10

yes, here is what it would look like with naive code:

(ns db-access)

(defn people [db]
  (query db "select * from people"))

(defn people-add [db peeps]
  (bulk-insert db "insert into people" peeps))

(ns top
  (:require [db-access :refer [people people-add]]))

(def db-from “something-something”)
(def db-to “something-else-entirely”)

(defn transfer [db-from db-to])
  (people-add db-to (people db-from))

bill22:02:39

wanna eliminate the first parameter (`db`) from each function in the db-access ns

bill22:02:40

seems like I’d have to spin up two Yurts and make transfer call into the first one to get the people and then call into the second one to insert the people

tolitius22:02:41

you could. by why not people [db]? 🙂

tolitius22:02:49

I would recommend using the args:

(defn people [db]
  (query db "select * from people"))
since no matter how you manage your state:
(defn people []
  (query db "select * from people"))
will always use the same db (the same connection / resource). i.e. not really mount or really mount or component specific there is one gotcha to this, which is, in testing, or for different use case, you can use mount/start-with: https://www.dotkam.com/2016/01/17/swapping-alternate-implementations-with-mount/

bill22:02:55

ok thanks @tolitius. I clearly misunderstand Mount and need to go study more. I thought the whole point was to eliminate the configuration parameter (`db` in this case) from my argument lists, allowing me to refer to the configuration parameter(s) (from within my function bodies) as if they were in the enclosing environment

tolitius22:02:35

you mean like local scoping?