Skip to content

Instantly share code, notes, and snippets.

@sylg
Created June 29, 2014 22:07
Show Gist options
  • Save sylg/6f2488b40c2692df4d16 to your computer and use it in GitHub Desktop.
Save sylg/6f2488b40c2692df4d16 to your computer and use it in GitHub Desktop.
trying multi tennt mongo express
/**
* Scoping by tenant_id on every documents
*/
// app.js
app.use(getTenantId())
function getTenantId(req, res, next) {
db.organisation.find({subdomain: req.subdomain}, function(err, doc) {
if(err) next(err);
req.tenantId = {tenant_id: doc._id};
next();
});
}
// userController.js
var User = require('../models/user');
var _ = require('lodash');
exports.index = function (req, res) {
var query = {}
var queryWithTenant = _.merge(query, req.tenantId)
User.find(queryWithTenant, function(err, docs) {
if(err) res.send(404);
res.jsonp(docs)
});
};
/**
* Prefixing every collection's name with tenant id or name ( more readable )
*/
// userController.js
var User = require('../models/user');
exports.index = function (req, res) {
// not sure how to do that in with moongose... but with mongo
var subdomain = req.subdomain;
var colName = subdomain+'_user';
db[colName].find({}, function(err, docs) {
if(err) res.send(404);
res.jsonp(docs)
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment