Skip to content

Instantly share code, notes, and snippets.

@christkv
Created August 1, 2012 19:57
Show Gist options
  • Save christkv/3230153 to your computer and use it in GitHub Desktop.
Save christkv/3230153 to your computer and use it in GitHub Desktop.
A bit simpler
bc = {
auth:{
configuration_type: 'replicaset',
replicaset: {
db_name: 'auth',
servers: [
{host:'localhost',port:4000,options:{auto_reconnect:true}},
{host:'localhost',port:4001,options:{auto_reconnect:true}}
,{host:'localhost',port:4002,options:{auto_reconnect:true}}
],
options: {
rs_name: 'replset0',
read_secondary: true,
socketOptions: { timeout: 0, keepAlive: 1 }
}
},
standalone: {
db_name:'auth',
host:'UAT-NJ-MONGO-01',
port:36108,
options:{
auto_reconnect: true,
poolSize: 3,
socketOptions: { keepAlive: 1 }
}
}
},
session:{
configuration_type: 'standalone',
replicaset: {
db_name: 'sessions',
servers: [
{host:'UAT-NJ-MONGO-01',port:36108,options:{auto_reconnect:true}},
{host:'UAT-NJ-MONGO-02',port:36108,options:{auto_reconnect:true}}
,{host:'UAT-NJ-MONGO-03',port:36108,options:{auto_reconnect:true}}
],
options: {
rs_name: 'LANDER',
read_secondary: true,
socketOptions: { timeout: 0, keepAlive: 1 }
}
},
standalone: {
db_name:'sessions',
host:'UAT-NJ-MONGO-01',
port:36108,
options:{
auto_reconnect: true,
poolSize: 3,
socketOptions: { keepAlive: 1 }
}
}
},
sso:{
configuration_type: 'replicaset',
replicaset: {
db_name: 'sso',
servers: [
{host:'UAT-NJ-MONGO-01',port:36108,options:{auto_reconnect:true}},
{host:'UAT-NJ-MONGO-02',port:36108,options:{auto_reconnect:true}}
,{host:'UAT-NJ-MONGO-03',port:36108,options:{auto_reconnect:true}}
],
options: {
rs_name: 'LANDER',
read_secondary: true,
socketOptions: { timeout: 0, keepAlive: 1 }
}
},
standalone: {
db_name:'sso',
host:'UAT-NJ-MONGO-01',
port:36108,
options:{
auto_reconnect: true,
poolSize: 3,
socketOptions: { keepAlive: 1 }
}
}
}
};
var connect = function connect(configurationName, configurationList, callback) {
var config = configurationList[configurationName];
var configType = config[config.configuration_type];
var db = null;
switch(configType) {
case 'replicaset':
var servers = config[configType].servers.map(function(properties) { return new Server(properties.host, properties.port)});
var replicaset = new ReplSet(servers, config[configType].options);
db = new Db(config[configType].db_name, replicaset);
break;
case 'standalone':
db = new Db(config[configType].db_name, new Server(config[configType].host, config[configType].port, config[configType].options));
}
// Open db
db.open(callback);
}
connect("auth", bc, function(err, db) {
db.on("error", function(err) {
});
db.on("close", function(err) {
});
db.on("timeout", function(err) {
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment