Tenant

Getting started
Looking for the express middleware? Try express-tenant!
Installation
$ npm i --save tenant
ES5
var Tenancy = require('tenant');
ES6
import { Tenancy, Tenant, Connection } from 'tenant';
Tenancy configuration options
import Tenancy from 'tenant';
import Bluebird from 'bluebird';
let tenancy = new Tenancy({
tenants: {
production: convict({}),
staging: config,
development: {},
},
connections: {
salesforce(config) {
let { username, hostname, password, token } = config.salesforce;
let conn = new jsforce.Connection({
loginUrl: hostname,
accessToken: token,
});
return Bluebird.fromCallback(cb => {
conn.login(username, password + token, cb);
})
},
couch(config) {
return nano(config.couch.url);
},
service(extra, parameters, config) {
},
],
});
Functional initialization
Alternatively you can add connections and tenants functionally. Pass a fully qualified Tenant
object or a name and a configuration object. Order doesn't matter, connections will populate to tenants and vice versa
Example:
import { Tenancy, Tenant } from 'tenant';
let tenancy = new Tenancy();
let staging = new Tenant('staging', stagingConfig);
tenancy
.tenant(staging)
.connection('salesforce', (config) => {
return Promise.reject(new Error('Really? Still Salesforce?'));
})
.tenant('production', prodConfig);
export default tenancy;
Getting tenant configuration
let secret = tenancy.tenant('production').config.sessionSecret;
Getting a tenant connection
let CouchDB = tenancy.tenant('staging').connection('couch');
Passing additional arguments to the connection factory method
new Connection('couch', (tablename, config) => {
return nano(config.couchdb).use(tablename);
});
let CouchDB = tenancy.tenant('staging').connection('couch', ['users']);
API Reference