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.
hey @bill, good find 🙂
if these are two different databases, they are two different states
i.e. you can use mount
(defstate from-db :start (connect {...}) :stop ...)
(defstate to-db :start (connect {...}) :stop ...)
thanks @tolitius I need to clarify…
(lame pseudo-code on its way…)
sure, let's see it
(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))
I have a single db access namespace that calls some db library to do the actual interaction
so I’d use Mount to produce/consume the db connection parameters
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...
ya and that state has to be different for each database cnxn
if that's correct, mount
will connect to this database and will be out of your way
ah.. hold on. you are using db access for more than one database in the same runtime?
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))
wanna eliminate the first parameter (`db`) from each function in the db-access
ns
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
you could. by why not people [db]
? 🙂
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/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
you mean like local scoping?