Sorry if it's a wrong place to post this question, but I was not able to find a sufficient answer on the web or in the examples tab on the website.
I have a variable db that represents a database connection. I also have a /register rout represented by registerHandler() that needs to be able to save a new user to the database. How do I pass the db into the registerHandler()?
I've found 2 solutions to this problem:
- Have the
registerHandler()as a method of someMyGlobalAPIContextstruct that has all the fields that API routes may need. The issue is that this would result in a so-called "god object" (and it just feels wrong). - Use middleware that would set
context.Keys["database] = db(link to docs) before every call to/registerrout. This seems fine, but I am very concerned about the performance of creating and populating thisKeysobject every time you access this rout. What if you have many calls to it? I feel like this might have its toll on performance.
Doing something like that seems like a very common thing you'd need to do. What is the most common approach?
Comment From: synalice
I've found a good article that seems to cover all my questions.