So I have a node express app using nano with couchdb as the backend, this is running fine. I'm now looking to learn how I would expand it to multiple organisations.
So for instance, a wildcard DNS record allowing https://customername.myapp.com for each customer. I will then check the req.headers.host in the main database, along with checking session cookies etc in each request.
What I'm struggling to get my head around though, is how the backend will work. I think I understand that the correct method is to use a database for each organisation, and copy the design from a template database.
But if this is correct, I don't understand how this translates to my code using nano. I currently use this:
var dbname = 'customer1';
var nano = require('nano')(config.dbhost);
var couch = nano.db.use(dbname);
and then in my functions:
couch.get(somevalue, function(err, body) {
// do stuff
});
But that won't work when the database itself is a variable. Should I be looking at moving the query to a lower level, eg nano.get('dbname', query... or something else?
EDIT
Hoping someone can give me an example of how to use middleware to change the database name dependent on the host header. I have this so far:
app.use(function(req,res,next) {
var couch = nano.db.use(req.header.host);
next();
});
But I don't understand how to pass the couch object through ('couch' is unknown in the rest of my routing). I have tried passing it back through in the 'next(couch)' but this breaks it...