Tuesday, 4 April 2017

How can I dynamically assign a database path in Express.js based on login?

I have a backend service that I would like to use as a single point of entry for my web application, and dynamically assign a database path based on the user login. I realize that this is not a scalable solution. I intend to use it during a testing period with several clients (accessing the ALPHA database), and also setting up a demo (accessing the SAND database). I have the following module that I have written as a simple test to see if the login is for the demo user, all other logins will go to the other resource:

config.js

var express         = require('express');
var app             = express();

module.exports.dbPath = function  (login){
    console.log('login - ', login);
    if (login === 'demo@mysite.com'){
        return process.env.DB_SAND;
    } else {
        return process.env.DB_ALPHA;
    }
};

My question is, how can I manage each unique login and assign a globally accessible reference for that session to direct each user session consistently to the correct database? Am I overcomplicating this? If there is a different approach that would be a better practice I would welcome a suggestion in another direction.



via espressoAndCode

No comments:

Post a Comment