Created
June 29, 2014 22:07
-
-
Save sylg/6f2488b40c2692df4d16 to your computer and use it in GitHub Desktop.
trying multi tennt mongo express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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