2.1.2 • Published 9 years ago

tenant v2.1.2

Weekly downloads
46
License
ISC
Repository
github
Last release
9 years ago

Tenant

npm npm CircleCI Coveralls

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']);

API Reference

class Tenancy

class Tenant

class Connection

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.1

10 years ago