Last active
July 18, 2018 21:13
-
-
Save jewelsjacobs/eafc0626acff25082397 to your computer and use it in GitHub Desktop.
[ETL Scripts] - DBConnections #etl #mongodb #nodejs #db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Julia Jacobs | |
* @version 1.0.0 | |
* @description MongoDB remote and local connection singletons | |
* @module dbConnections | |
*/ | |
var LocalClient = require('mongodb').MongoClient; | |
var RemoteClient = require('mongodb').MongoClient; | |
// local MongoDB connection | |
var localConnectionInstance; | |
// remote MongoDB connection | |
var remoteConnectionInstance; | |
/** | |
* @description singleton connection to local mongodb | |
* @type {{url: string, connection: Function}} | |
*/ | |
exports.localDb = { | |
//default local connection | |
url : 'mongodb://localhost:2017', | |
connection : function (callback) { | |
// if already we have a connection, don't connect to database again | |
if (localConnectionInstance) { | |
callback(localConnectionInstance); | |
return; | |
} | |
LocalClient.connect(this.url, function(error, databaseConnection) { | |
if (error) throw new Error(error); | |
localConnectionInstance = databaseConnection; | |
callback(databaseConnection); | |
}); | |
} | |
}; | |
/** | |
* @description singleton connection to remote mongodb | |
* @type {{url: null, connection: Function}} | |
*/ | |
exports.remoteDb = { | |
// required remote url string | |
url : null, | |
connection : function (callback) { | |
// if already we have a connection, don't connect to database again | |
if (remoteConnectionInstance) { | |
callback(remoteConnectionInstance); | |
return; | |
}; | |
if (!this.url) { | |
throw new Error("Remote url connection string required.") | |
} else { | |
RemoteClient.connect(this.url, function (error, databaseConnection) { | |
if (error) throw new Error(error); | |
remoteConnectionInstance = databaseConnection; | |
callback(databaseConnection); | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment