Often I have functions that need data from either a config file or a database. I would prefer pure functions at much as possible and something that can be unit tested.
I have no idea how to do this correctly, as I don't have a CS degree. The options I can see are:
Example 1:
function myFunc(p1, p2, db, configFile) { ... }
but that means all other functions that calls myFunc() also needs to have db, configFile as arguments.
Example 2:
function myFunc(p1, p2) {
const config = readConfig();
dbConn = openDb();
...
}
I suppose this make unit testing impossible?
Example 3:
const struct = {
config: readConfig(),
dbConn: openDb(),
...
};
function myFunc(p1, p2, struct) { ... }
This have the down side, that when other functions data also gets stored in this global struct then each function have access to more than what they need.
Question
Are there other possibilities? What is the recommended approach?