Hey all
I have an app which serves multiple orgs with individual databases, by way of
subdomain, ORGNAME.myapp.foo . My current method of attaching the database to
the ExpressJS requests via middleware is like this:
app.use(function(req, res, next) {
const orgname = req.headers.host.split('.')[0].toLowerCase();
req.couch = nano.db.use(orgname);
next();
}
I often wonder if this is the best way of doing it. Is there a better way?
I could for instance, attach it just once, to the session (stored in Redis):
app.use(function(req, res, next) {
if(!req.session.couch) {
const orgname = req.headers.host.split('.')[0].toLowerCase();
req.session.couch = nano.db.use(orgname);
}
next();
}
But I guess that depends on size of the Couch instance (is it just metadata, or
something more tangible?), and other aspects which I perhaps haven’t considered.
I’d love to hear some thoughts on this?
Thank you!
R