I have a Node REST API using Express and JavaScript. I created a Database
class that is able to connect, disconnect and execute the queries. My app.js (the entry file) creates an instance database
of this and launches the express server. On requests I have the following flow:
- requests call the middleware functions and finally the controller
- the controller calls some other classes and passes in the data
- Lastly these classes call the query files to access the database
The query files themselves need to access the instance of the database class. They can call the query function and pass in their prepared statement. I am not sure how to transport the instance database
from my app.js (entry file) all the way to those query files.
I found multiple solutions:
using global variables
add the instance to the request object
add the instance to the response locals
add the instance to the app locals
What is the best way/practice to transport variables from one file to the whole application?