1

I'm building a web app that I would like to use with two databases based on a GET query. These two databases have the same schema, the only difference is one has live data and the other is scrubbed (or test) data.

This works fine, but I'm wondering if this is the proper way to go about solving this problem.

I'm referencing a model with a schema: names_model.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var namesSchema = new Schema({
name: String,
createdAt: String
});

module.exports = mongoose.model('names', namesSchema);

And this is my main file. If the param query is 1 it will connect to the first db, else it will go connect to the second db.

var mongoose = require('mongoose');

var db = mongoose.createConnection('mongodb://localhost/database1');
var db2 = db.useDb('database2');
var NamesDB = require('./names_model.js');

var Connect = db.model('names', NamesDB);
var Connect2 = db2.model('names', NamesDB);

exports.getData = function(dbName, sendBack) {

    console.log(dbName);
    if (dbName == 1) {
        var Names = Connect;
    }
    else {
        Names = Connect2;
    }

    Names.find({}, function (err, docs) {
        if (err) {
            console.log(err)
        }
        else {
            sendBack(docs);
        }
    });
};

Like I mentioned above, this does work, though I feel that I might be making extra steps for myself, but I'm not quite sure. I'm hoping someone might be able to tell me if theres an easier way.

Thanks! T

3
  • You have to develop with both the schemas or one is for production and the other one for development only? Because you could use an environment variable to choose what to use without add extra params to each call. Commented Jan 5, 2016 at 16:51
  • @Michelem - It would be one for production and one for development. I thought this way would be easier so I wouldn't need to adjust code when creating a release. However, I'm open to suggestions. How would an environment variable be used in this situation? Commented Jan 5, 2016 at 16:56
  • So let me add an answer for that. Commented Jan 5, 2016 at 17:02

1 Answer 1

1

You could use an environment variable to define if you are on "live/production" site or the development one.

Define an env variable in your systems, usually it is NODE_ENV=<env-name> and then use a condition on process.env.NODE_ENV to define what to use for each env:

var dbName;

if (process.env.NODE_ENV === 'development') {
    // Define the development db
    dbName = 'database1';
 } else if (process.env.NODE_ENV === 'production') {
    // Define the production db
    dbName = 'database2';
 }

 var db = mongoose.createConnection('mongodb://localhost/' + dbName);
 var NamesDB = require('./names_model.js');

 var Connect = db.model('names', NamesDB);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.