node.js proper design for creating db connection

I have a module for the DB connection i want to re factor the code to export it properly my current code looks like this and it work just fine :

var mongoose = require('mongoose'),
    crypto =  require('crypto');


    mongoose.connect('mongodb://localhost/mydb');
    var db = mongoose.connection;

    db.on('error', console.error.bind(console, 'DB connection error...'));

    db.once('open',function callback(){
        console.log('Vascular DB is open');
    });


    //module.exports = mongoose.connection;

    // define user schema
    var userSchema = mongoose.Schema({
        firstName: String,
        lastName: String,
        userName: String,
        salt: String,
        hashed_pwd: String
    });
    userSchema.methods = {
        authenticate : function(passwordToMatch){
            return hashPwd(this.salt, passwordToMatch) === this.hashed_pwd;
        }
    }
    // define user model
    var User = mongoose.model('User',userSchema);

    User.find({}).exec(function(err, collection){
        if(collection.length === 0){
            var salt, hash;
            salt = createSalt();
            hash = hashPwd(salt, 'mhd');
            User.create({firstName:'name1',lastName:'lastname1',userName:'mhd', salt:salt , hashed_pwd:hash});
            salt = createSalt();
            hash = hashPwd(salt, 'ahd');
            User.create({firstName:'name2',lastName:'lastname2',userName:'ahd', salt:salt , hashed_pwd:hash});
        }
    })

    function createSalt(){
        return crypto.randomBytes(128).toString('base64');
    }
    function hashPwd(salt, pwd){
        var hmac = crypto.createHmac('sha1', salt);
        return hmac.update(pwd).digest('hex');
    }

module.exports = db; 

however, i put all my configuration port, db connection and the rootPath in a module called config and i wanted to pass config argument to my db so i can switch between development and production mod so i re factored my code like this :

var mongoose = require('mongoose'),
    crypto =  require('crypto');

module.exports = function(config){
    mongoose.connect('mongodb://localhost/mydb');
    var db = mongoose.connection;

    db.on('error', console.error.bind(console, 'DB connection error...'));

    db.once('open',function callback(){
        console.log('Vascular DB is open');
    });


    //module.exports = mongoose.connection;

    // define user schema
    var userSchema = mongoose.Schema({
        firstName: String,
        lastName: String,
        userName: String,
        salt: String,
        hashed_pwd: String
    });
    userSchema.methods = {
        authenticate : function(passwordToMatch){
            return hashPwd(this.salt, passwordToMatch) === this.hashed_pwd;
        }
    }
    // define user model
    var User = mongoose.model('User',userSchema);

    User.find({}).exec(function(err, collection){
        if(collection.length === 0){
            var salt, hash;
            salt = createSalt();
            hash = hashPwd(salt, 'mhd');
            User.create({firstName:'name1',lastName:'lastname1',userName:'mhd', salt:salt , hashed_pwd:hash});
            salt = createSalt();
            hash = hashPwd(salt, 'ahd');
            User.create({firstName:'name1',lastName:'lastname1',userName:'ahd', salt:salt , hashed_pwd:hash});
        }
    })
}// exports 
    function createSalt(){
        return crypto.randomBytes(128).toString('base64');
    }
    function hashPwd(salt, pwd){
        var hmac = crypto.createHmac('sha1', salt);
        return hmac.update(pwd).digest('hex');
    }

but i got this error:

  C:\Users\M-Mouallem\Desktop\mySpace\sandBox\project\node_modules\mongoose\lib\index.js:322
          throw new mongoose.Error.MissingSchemaError(name);
                ^
    MissingSchemaError: Schema hasn't been registered for model "User".
 Use mongoose.model(name, schema)

i re factored almost all the parts of my app.js file thats way and it works just fine so i wondering is it something to do with mongoose ?