How the DAOs objects works if they never get instantiated?

Hey there everyone, i hope all you bee good this days.
I just landed the course and i have learn a lot so far just with the structure that the app stands, im studying the patterns follewed so i will love to know how the dao objects interact with the controllers.
I have seen that the new ‘static’ type of properties are available for the whole class, beyond that a method of an instance. But still , how the DAO object that injects to itself the client, at the beginning in the “load” secuence ( src/index.js ) it’s the same object that its used by the controllers ?
Like there is not evident instantiation I haven’t seen yet how this is happening ?

Hi @Dago_A_Carralero,

Very interesting question. I like your Enthusiasm on learning about our application. :+1:

I am not a NodeJS expert, but I think it is working like below:

  1. First, if you see src/server.js file, you will see movies are imported from src/api/movies.route

  2. In src/api/movies.route, you will see this code calling the controller:

  3. In src/api/movies.controller.js, you can see DAO file imported on the very top and the code calls function from DAO file.

    const { moviesList, totalNumMovies } = await MoviesDAO.getMovies()

So, in the beginning only when we are loading the application, it will collect all the movies.

Route -> Contoller -> DAO

I hope I understood your query and this helps, please correct me and let me know if you need more help.


Hi there @kanikasingla, thanks for your response, i have a few questions if you dont mind :smile:.
I have seen very close the code flow of the app and see your explanation, the thing is i have the idea of how it wroks but can’t see the full picture with the following.
Dao objs like movieDao are classes wit a bunch of static methods, (some of them waiting to be filled by me); they get a conected mongo client injected at booting time of the app in src/index. Later on when a controler imports them (this is when Js stops been clear and starts to make magic ) somehow that dao obj is alive, and has a mongo client injected indeed.
I never see static methods before and im on it, but a litle of help here to unveil the mistery of the static-classes-behavior-trought-different-modules will be perfect. Thanks again and stay safe.


As you’ve noticed, the db connection is injected into each DAO class individually in index.js just before app.listen(). When this happens, handles for the relevant collections are saved in variables that are global to each DAO file respectively, and remain open for as long as the server is running.

The static keyword simply specifies that those methods can be accessed on the DAO class itself without having to actually create instances of it.


Thanks @jbarth1986_N_A that was clarifying.
I was suspecting this, but looks like “magic” at a first look since the DAO clasess are imported and changed in different points of the flow and still they maintain the global state between them.
Im studying and adopting this pattern.

1 Like