express-restify-mongoose icon indicating copy to clipboard operation
express-restify-mongoose copied to clipboard

Multi Tenant

Open ctaity opened this issue 5 years ago • 1 comments

Hi, this lib supports some multi tenant strategy? Filters or Database connection?

ctaity avatar May 20 '19 21:05 ctaity

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.

OliverFischer avatar Nov 27 '20 11:11 OliverFischer