diff --git a/.env.sample b/.env.sample index e8d6d82c..1ba95367 100644 --- a/.env.sample +++ b/.env.sample @@ -26,4 +26,10 @@ KAFKA_URL = "172.31.0.4:9092" KAFKA_GROUP_ID = "projects" // Kafka group id # SUBMISSION TOPIC -SUBMISSION_TOPIC = "dev.sl.projects.submissions" // Kafka topic name for pushing projects submissions \ No newline at end of file +SUBMISSION_TOPIC = "dev.sl.projects.submissions" // Kafka topic name for pushing projects submissions + +# Replica set name required for mongodb replicaSets +REPLICA_SET_NAME = "rs0" + +# Replica set read preference +REPLICA_SET_READ_PREFERENCE = "primary" \ No newline at end of file diff --git a/config/db/mongodb.js b/config/db/mongodb.js index 1ba8f648..f2253813 100644 --- a/config/db/mongodb.js +++ b/config/db/mongodb.js @@ -23,12 +23,19 @@ const DB = function() { mongoose.set('useCreateIndex', true) mongoose.set('useFindAndModify', false) mongoose.set('useUnifiedTopology', true) + + let options = {useNewUrlParser: true}; + + if (process.env.REPLICA_SET_NAME && process.env.REPLICA_SET_NAME !== "") { + options["replset"] = {rs_name: process.env.REPLICA_SET_NAME }; + + if (process.env.REPLICA_SET_READ_PREFERENCE && process.env.REPLICA_SET_READ_PREFERENCE !== "") { + options["replset"]["readPreference"] = process.env.readPreference; + } + } const db = mongoose.createConnection( - process.env.MONGODB_URL, - { - useNewUrlParser: true - } + process.env.MONGODB_URL,options ); db.on("error", console.error.bind(console, "connection error:"));