Fork me on GitHub
#sql
<
2018-03-27
>
souenzzo12:03:44

In #datomic is a good pratice pass db as parameter of every function, once the "db" immutable (db/list-users db) But in SQL, what is the good pratice? pass db as parameter? or just define the db value in the namespace? and use (db/list-users)?

jumar12:03:04

I'd say it's same as with everything else; better to pass explicitly as an argument. However, db is often defined as a global variable (e.g. when use #mount)

๐Ÿ‘ 4
jumar12:03:46

Other frameworks/libraries like component or integrant encourage passing it as an explicit param.

donaldball14:03:31

FWIW if you donโ€™t pass it an an explicit argument, you will have a not good time if you ever realize you need more than one database.

seancorfield16:03:33

@souenzzo As someone who originally used a global for my db-specs, I can say with absolutely certainty, you're better off passing your db-spec as an argument to every function that may want to do database operations! ๐Ÿ˜†

๐Ÿ‘ 4
seancorfield16:03:00

Nowadays, we construct the connection pool for each DB "spec" (we have half a dozen different ones in our code now!) in the start of a Database component (Stuart Sierra's Component), and we pass enough of the overall Application Component around the app for everything to be able to get at the various connections as needed

(-> app :database :pooled-db)
for the main DB connection
(-> app :database :pooled-slave)
is another (directed at the DB proxy layer to read from either master or slave).

souenzzo16:03:20

I'm also interested in this "application map" In this example, where you decide if you will use pooled-db or pooled-slave? inside db functions (db/list-users app) or on the service (db/list-users (:pooled-db app))?

seancorfield17:03:22

Usually in the caller (and just pass the connection), but some code is passed the whole Database Component so it can decide which connection to use, and other code is passed the Application Component since that also includes our Caches Component -- for functions that might hit the DB and/or cache results.

seancorfield17:03:56

This is a fairly simple example of the sort of thing we do at work: https://github.com/framework-one/fw1-clj/blob/master/examples/usermanager/main.clj

โค๏ธ 4