mongodb-expressions
Version:
MongoDB expressions for fire.js
93 lines (89 loc) • 2.39 kB
JavaScript
function Configurator(moduleConfig, env) {
this.config = moduleConfig
this.env = env
this.connections = []
if(!this.config) return
var connectionNames = Object.keys(this.config)
for(var i = 0; i < connectionNames.length; i++) {
var name = connectionNames[i]
var config = this.config[name]
if(typeof(config) === 'object' && !(config instanceof Array)) {
this.connections.push({
name: name,
config: config
})
}
}
};
Configurator.prototype.getConfig = function() {
for(var i = 0;i < this.cloudProviders.length; i++) {
var provider = this.cloudProviders[i]
provider.configurator = this
for(var j = 0;j < this.connections.length;j++) {
var connection = this.connections[j]
var config = provider.getConfig(connection, this.env)
if(config) {
config['$provider'] = provider.name
config['$name'] = connection.name
return config
}
}
}
return null
}
Configurator.prototype.cloudProviders = []
/*
* VCAP
*/
Configurator.prototype.cloudProviders.push(
{
name: "VCAP",
getConfig: function(connection, env) {
if(connection.config && connection.config.type == 'VCAP') {
if(env.VCAP_SERVICES) {
if(!this.VCAP_SERVICES) {
try{
this.VCAP_SERVICES = JSON.parse(env.VCAP_SERVICES)
this.VCAP_SERVICES_NAMES = Object.keys(this.VCAP_SERVICES)
}catch(ex) {
this.VCAP_SERVICES = []
this.VCAP_SERVICES_NAMES = []
console.error('Invalid VCAP JSON:', ex)
}
}
var serviceNameRegex = new RegExp(connection.config.service || "mongodb*")
var instanceNameRegex = new RegExp(connection.config.instance || "mongodb*")
var serviceNames = this.VCAP_SERVICES_NAMES
for(var i = 0;i < serviceNames.length;i++) {
var serviceName = serviceNames[i]
if(serviceNameRegex.test(serviceName)) {
var serviceInfo = this.VCAP_SERVICES[serviceName]
for(var j = 0;j < serviceInfo.length; j++) {
var instanceInfo = serviceInfo[j]
if(instanceNameRegex.test(instanceInfo.name)) {
return instanceInfo.credentials
}
}
}
}
}
}
return null
}
}
)
/*
* Generic
*/
Configurator.prototype.cloudProviders.push(
{
name: "generic",
getConfig: function(connection, env) {
if(connection.name == 'generic') {
return connection.config
}
return null
}
}
)
module.exports = Configurator