2 ms·
What I tend to do is create a database file (db.js) which exports an object with getters for mongoose, redis, etc. The getter connects to the db and returns the
by jameswyse 14y ago
What I tend to do is create a database file (db.js) which exports an object with getters for mongoose, redis, etc. The getter connects to the db and returns the connection the first time and returns a cached copy on subsequent calls.
One big tip for mongoose is to use the .lean() function on your queries whenever you don't need to use any special mongoose features (statics, virtuals, etc), which returns a standard JS Object instead of a mongoose collection instance. It can be quite a bit faster!