tenant
v2.1.2
Published
Unopinionated tenanted connection and configuration management
Downloads
105
Maintainers
Readme
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({}), // use some library
staging: config, // a custom module
development: {}, // a plain object!?
},
connections: {
// I apologize.
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);
})
},
// Less gross.
couch(config) {
return nano(config.couch.url);
},
service(extra, parameters, config) {
// Return whatever you want, promise, function, object, (spatula?)
},
// ...other tenanted connections
],
});
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);
// Chainable. Order doesn't matter.
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
// Define your factory method with additional arguments
new Connection('couch', (tablename, config) => {
return nano(config.couchdb).use(tablename);
});
// Call connection with an array of additional arguments
let CouchDB = tenancy.tenant('staging').connection('couch', ['users']);