I tend to organize my API's src code into multiple directories and sub-files based on logical groupings, when it comes to database operations these end up split up into logical directories too
FWIW these are nodejs lambdas, but I think this question is language agnostic
There seem like 3 options and I'm not sure how to rank them:
- Instantiate a dynamodb document client
let dynamo = new AWS.DynamoDB.DocumentClient({...})
in each file that needs one.
- Cons: seems like unneeded duplication, risk of configuration drift (unless I use env vars)
- Pros: easy to implement
- Instantiate a dynamodb client in a parent file and import it as needed
- Cons: Possible implications for webpack, managing relative file paths could become cumbersome
- Pros: Centralized config
- Instantiate a dynamodb client in the files controlling logic, add as input param to all functions requiring db access
- Cons: Adding the param to the functions seems bloated, still creates multiple clients, albeit not as many as option 1, possible config drift
- Pros: Very explicit scoping of clients
I'm leaning towards option 2, but I'd really appreciate any insights as to how ya'll are rationalizing this.