MongoDB and constructors/classes in node.js (async)


I want to abstract certain MongoDB queries/documents using classes.

Basically I think the way is to somewhere (an own class?) create the connection and use this connection(pool) in various Classes. But how would I do this code-wise. For example I am not sure, when I use a MongoDBConnectionClass and initialize the connection in the constructor how to make sure I wait for the connection to be established to continue using it somewhere else.

I think I am doing something wrong here. Here a schema to maybe better understand what I mean

+---- Class1+ # eg. Book
+---- Class2+ # eg. Library
+---- Class3+ # eg. Dust
            +-- Library1+ # eg. New Book Release
            +-- Library2+ # ...
            +-- Library3+ # ...
                        +-- App1
                        +-- App2
                        +-- App3

Oh an CoffeScript answers would be great, but JS of course works as well.

Problem courtesy of: user1680104


I use this pattern in my tutorial (will be out soon)

If you look at the model and controllers I wrap the actual function with the db object.

Solution courtesy of: christkv


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.