-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtenant.js
46 lines (34 loc) · 1.33 KB
/
tenant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var fs = require('fs');
var path = require('path');
var util = require('util');
var DataSource = require('loopback-datasource-juggler').DataSource;
exports = module.exports = function tenant(app) {
app.use('/:tenant/', function(req, res, next) {
var tenant = req.params.tenant;
var dataSourceFileName = util.format('datasources.%s.json', tenant);
var dataSourcePath = path.resolve(__dirname, 'server/', dataSourceFileName);
if (fs.existsSync(dataSourcePath)) {
var dataSourceObj = JSON.parse(fs.readFileSync(dataSourcePath, 'utf8'));
Object.keys(dataSourceObj).forEach(function(dataSource) {
app.dataSources[dataSource].adapter.settings = dataSourceObj[dataSource];
app.dataSources[dataSource].adapter.clientConfig = dataSourceObj[dataSource];
app.dataSources[dataSource].settings = dataSourceObj[dataSource];
app.dataSources[dataSource].connector.settings = dataSourceObj[dataSource];
app.dataSources[dataSource].connector.clientConfig = dataSourceObj[dataSource];
});
next();
}
else {
// Invalid tenant
res.json({
'error': {
'name':'Error',
'status':404,
'message':'Invalid tenant',
'statusCode':404,
'stack':'https://www.npmjs.com/package/loopback-tenant'
}
});
}
});
};