From 07f14440009cf9b8573f49acda663eb7e98f0f59 Mon Sep 17 00:00:00 2001 From: Austin Date: Wed, 27 Mar 2019 09:11:39 -0700 Subject: [PATCH] Pass in a mongo connection string rather than trying to build it --- src/MigrationJob.js | 4 ++-- src/connectors/MongoDBConnector.js | 11 +++++------ src/dao/MongoDBDAO.js | 14 ++++++-------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/MigrationJob.js b/src/MigrationJob.js index 13ce32b..b0e939c 100644 --- a/src/MigrationJob.js +++ b/src/MigrationJob.js @@ -13,7 +13,7 @@ class MigrationJob { this.mapperFunction = (item) => { return item; }; this.filterFunction = () => { return true; }; this.dynamoDBDAO = new DynamoDBDAO(sourceTableName,sourceConnectionOptions.region,sourceConnectionOptions.accessKeyId,sourceConnectionOptions.secretAccessKey); - this.mongoDBDAO = new MongoDBDAO(this.targetTableName, this.targetDbName,targetConnectionOptions.host, targetConnectionOptions.user, targetConnectionOptions.password); + this.mongoDBDAO = new MongoDBDAO(this.targetTableName, this.targetDbName, targetConnectionOptions.connectionString); this.dynamodbEvalLimit = dynamodbEvalLimit || 100; this.filterExpression = null; this.expressionAttributeNames = null; @@ -90,4 +90,4 @@ class MigrationJob { } } -module.exports = MigrationJob; \ No newline at end of file +module.exports = MigrationJob; diff --git a/src/connectors/MongoDBConnector.js b/src/connectors/MongoDBConnector.js index bbcbec0..667d887 100644 --- a/src/connectors/MongoDBConnector.js +++ b/src/connectors/MongoDBConnector.js @@ -9,16 +9,15 @@ let client; class MongoDBConnector { - static getConnection(database,host,user,password) { + static getConnection(databaseName, connectionString) { return new Promise(async (resolve, reject) => { try { if (client) { - resolve(client.db(database)); + resolve(client.db(databaseName)); } else { - let url = 'mongodb://' + user + ':' + password + '@' + host; - client = new MongoClient(url,{ useNewUrlParser: true }); + client = new MongoClient(connectionString); await client.connect(); - resolve(client.db(database)); + resolve(client.db(databaseName)); } } catch (error) { console.error(error); @@ -29,4 +28,4 @@ class MongoDBConnector { } } -module.exports = MongoDBConnector; \ No newline at end of file +module.exports = MongoDBConnector; diff --git a/src/dao/MongoDBDAO.js b/src/dao/MongoDBDAO.js index ce060cf..b371e98 100644 --- a/src/dao/MongoDBDAO.js +++ b/src/dao/MongoDBDAO.js @@ -3,16 +3,14 @@ const lodash = require('lodash'); const MongoDBConnector = require('./../connectors/MongoDBConnector'); class MongoDBDAO { - constructor(tableName, databaseName,host,user,password) { + constructor(tableName, databaseName, connectionString) { this.tableName = tableName; - this.databaseName = databaseName; - this.host = host; - this.user = user; - this.password = password; - } + this.databaseName = databaseName + this.connectionString = connectionString; + } async intertOrUpdateItems(items) { - let dbConn = await MongoDBConnector.getConnection(this.databaseName,this.host,this.user,this.password); + let dbConn = await MongoDBConnector.getConnection(this.databaseName, this.connectionString); let collection = dbConn.collection(this.tableName); let bulkWriteReqArray = lodash.map(items, (item) => { return { @@ -29,4 +27,4 @@ class MongoDBDAO { } } -module.exports = MongoDBDAO; \ No newline at end of file +module.exports = MongoDBDAO;