Skip to content

Instantly share code, notes, and snippets.

@wookets
Last active December 27, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wookets/7372339 to your computer and use it in GitHub Desktop.
Save wookets/7372339 to your computer and use it in GitHub Desktop.
Multi tenant simple config for node.js. Based on TJ's config gist.
#
# Server config (based on tenant config)
#
# load the config from root of project
config = require __dirname + '/../../config.json'
#
# Apply defaults to every tenant up front to make things easier later.
#
for own tenantKey, tenantConfig of config
if tenantKey.indexOf('_') is 0 then continue
for own configKey, configValue of config[tenantKey]
config[tenantKey][configKey] ?= config._default[configKey]
#
# Return configKey from tenantKey.
# e.g. ('meow', 'dbUrl') -> return 'mongo://meow:pants/catbox'
# We can also return the whole config or a tenant specific configuration
# e.g. () -> return config // returns the whole config.json
# e.g. ('meow') -> return config.meowTenant // returns the json object under the tenant
#
module.exports = (tenantKey, configKey) ->
if not tenantKey then return config
if not config[tenantKey] then throw new Error('invalid tenant "' + tenantKey + '"')
if not configKey then return config[tenantKey]
if not config[tenantKey][configKey] then throw new Error('invalid config key "' + tenantKey + ':' + configKey + '"')
return config[tenantKey][configKey]
{
"_define": {
"dbUrl": "the mongodb url with username and password and database name (mongolab)",
"sendEmailOnError": "if the app should send an email when an error is encountered (node-mailer)",
"sessionSecret": "the key to hash session tokens and restore them (used by connect / express)",
},
"_default": {
"sendEmailOnError": true
"sessionSecret": "MeowSuperDog!"
},
"meow": {
"dbUrl": "mongodb://user:pass@happy.mongohq.com:9277/rawr"
},
"woof": {
"dbUrl": "mongodb://user:pass@happy.mongohq.com:9277/rawr",
"sendEmailOnError": false
},
"test": {
"dbUrl": "mongodb://user:pass@happy.mongohq.com:9277/rawr",
"sendEmailOnError": false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment