Last active
March 6, 2019 13:31
-
-
Save paulomcnally/0fea84f0c5ed7fce70fd26d134c65c5f to your computer and use it in GitHub Desktop.
Create file server/boot/subdomain.js and edit server/model-config.json to add "tenant": true property on every model. Only work with loopback-connector-postgresql and schema.
This file contains hidden or 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
'use strict'; | |
var modelConfig = require('../model-config.json'); | |
module.exports = function(app) { | |
app.use(function(req, res, next) { | |
// set tenant based subdomain or use public | |
var subdomain = (req.subdomains.length > 1) ? req.subdomains.pop() : req.subdomains[0]; | |
var tenant = req.subdomains.pop() || 'public'; | |
// we go through all the model names from model-config.json | |
Object.keys(modelConfig).forEach(function(modelName) { | |
// validate if model have property tenant | |
if (modelConfig[modelName].hasOwnProperty('tenant')) { | |
// set postgresql schema for model | |
app.models[modelName].definition.settings.postgresql.schema = tenant; | |
} | |
}); | |
next(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i have same problem. I have 2 tenant with model user. If a user is logged in a tenant, other users are dislodged in other tenants