Express Tenant
Getting started
Looking for the framework agnostic version? Wanna build your own middleware? Try tenant!
Installation
$ npm i --save express-tenant
ES5
var Tenancy = ;
ES6
;
Tenancy configuration options
;; let tenancy = middlewares = {} tenantPath = 'tenant' requestKey = 'ENV' parse = requestParser defaultTenant: processenvNODE_ENV || 'development' tenants: production: // use some library staging: config // a custom module development: {} // a plain object!? middlewares: { return { // Do some tenant specific stuff here ; }; } connections: // I apologize. { let username hostname password token = configsalesforce; let conn = loginUrl: hostname accessToken: token ; return Bluebird } // Less gross. { return ; } // ...other tenanted connections );
Functional initialization
Alternatively you can add connections and tenants functionally
Example:
; let tenancy = ; let staging = 'staging' stagingConfig; tenancy ; ;
Getting tenant configuration
let secret = tenancyconfigsessionSecret;
Getting a tenant connection
let results = tenancy ;
Using the middleware
;; let app = ; // Must come before other tenant middlewaresapp; // Uses tenanted middlewaresapp; // tenant is available on the defined `tenantPath`app;
API Reference
Tenancy
Methods
constructor(params)
params Object
Example
// Path on the request object that the current tenant will be set. tenantPath: 'tenant' // Key that the default request parser looks for requestKey: 'ENV' // Request parser. Determines the tenant for a request { if !key return; return req || req || reqquerykey || reqquerykey; } // Tenant configurations tenants: staging: /* Staging config */ // Tenanted middlewares middlewares: { return ; } // Tenanted connections connections: { return Promise; } // Default tenant if none is provided defaultTenant: processenvNODE_ENV || 'development';
tenant(tenant)
tenant Tenant
Example
tenancy;
tenant([name])
name String
(optional)
Returns a tenant by the name or the default tenant if none is provided
tenant(name, config)
name String
config Object
Example
tenancy;
connection(name, factory)
name String
Key associated with a connection factory.
factory Function
Connection factories are functions with tenant configuration as the last argument. Connection factory function must return a promise, an object, or throw an error.
Example
tenancy;
middleware(name)
Returns a tenanted middleware by the name.
middleware(name, factory)
name String
Key associated with a middleware factory.
factory Function
Middleware factories are functions with tenant configuration as the only argument. Middleware factory function must return an Express middleware.
Example
tenancy;
Tenant
Methods
constructor(name, configuration, connectionsMap)
name String
Key used to retrieve this tenant
configuration Object
Configuration object that gets passed to connection factories.
connectionsMap Object
Key-Value pairs of connections names and factory methods
connection(name)
name String
Returns a promise that will resolve with the tenanted connection
Properties
name
Tenant name
config
Tenant configuration