express-restify-mongoose
express-restify-mongoose copied to clipboard
Multi Tenant
Hi, this lib supports some multi tenant strategy? Filters or Database connection?
Hi, MultiTenancy is supported by passing a modelFactory in the options when setting up a new model for restify. It's not yet in the documentation, but present since 2018.
We enhanced the functionality by passing the request the the getModel
hook :
app.use((req, res, next) => {
const getModel = options.modelFactory && options.modelFactory.getModel
req.erm = {
model: typeof getModel === 'function' ? getModel(req) : model,
}
next()
})
A possible (pseudo-code) implementation could look like :
modelFactory : {
getModel(request){
let tenantDb = request.get('x-tenant-db'),
tenantModel = request.get('x-erm-model`)
return mobgoose(`mongodb://localhost/${tenantDb}`).model(tenantModel)
}
}
using something like https://www.npmjs.com/package/mobgoose.