I need to export my mongoose database module, so I could use my defined models from every module in my program.
For example, my database.js module looks something like that:
var mongoose = require('mongoose'),
db = mongoose.createConnection('mongodb://localhost/newdb'),
Schema = mongoose.Schema;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log("Connected to database newdb");
var dynamicUserItemSchema = new mongoose.Schema({
userID: Number,
rank: Number,
});
var staticUserItemSchema = new mongoose.Schema({
_id: Schema.Types.Mixed,
type: Schema.Types.Mixed,
});
var DynamicUserItem = db.model('DynamicUserItem', dynamicUserItemSchema);
var StaticUserItem = db.model('StaticUserItem', staticUserItemSchema);
});
I want to be able adding var db = require('../my_modules/database'); to any other module my program - so I will be able to use the models like that:
db.DynamicUserItem.find(); or item = new db.DynamicUserItem({});
Is it possible doing that using "exports" or "module exports" ? Thanks.