dmfay / massive-js

A data mapper for Node.js and PostgreSQL.
2.49k stars 158 forks source link

When to connect in Express application #576

Closed robinvdvleuten closed 6 years ago

robinvdvleuten commented 6 years ago

I am trying out Massive in my Express application and was wondering when to connect to the database. I've used the PG library before and they explicitly tell to connect within the routes. Is this also the case for Massive?

dmfay commented 6 years ago

That doesn't sound right -- connecting every time adds a ton of overhead compared to even a shared active connection, let alone a proper connection pool. And the examples in the official node-pg docs have a module (db/index.js) caching a connection pool and exposing a query function for the routes.

Application routes can be hit by many users simultaneously. Generally you want them to be as light as possible, which means doing your init outside as much as possible.

Massive's docs have an Express example too.