Build RESTful API in Go and MongoDB
Mohamed Labouardy
2873

Connecting to Database

It’s not clearly mentioned when to connect to the database. So, here goes my idea.

If the application is going to hit frequently by a large number of clients, we can connect to the database by calling dao.connect() on main and keeping the connection persistent.

Another way is to connect and disconnect each time you run a query. This may work better in case of less frequent access.

In all the above db helpers, the MoviesDAO object is not used. However keeping the helpers as methods of MoviesDAO gives clarity to code. So instead of calling dao.connect and then the helper each time, the connect can be called inside the helper as m.connect .

Better suggestions are welcomed!!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.