Defining Mongoose Models in a Separate Module

I would like to separate Mongoose coins in a separate file. I tried to do it as follows:

var mongoose = require("mongoose"); var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId; var Material = new Schema({ name : {type: String, index: true}, id : ObjectId, materialId : String, surcharge : String, colors : { colorName : String, colorId : String, surcharge : Number } }); var SeatCover = new Schema({ ItemName : {type: String, index: true}, ItemId : ObjectId, Pattern : String, Categories : { year : {type: Number, index: true}, make : {type: String, index: true}, model : {type: String, index: true}, body : {type: String, index: true} }, Description : String, Specifications : String, Price : String, Cost : String, Pattern : String, ImageUrl : String, Materials : [Materials] }); mongoose.connect('mongodb://127.0.0.1:27017/sc'); var Materials = mongoose.model('Materials', Material); var SeatCovers = mongoose.model('SeatCover', SeatCover); exports.Materials = Materials; exports.SeatCovers = SeatCovers; 

Then I tried to use the model as follows:

 var models = require('./models'); exports.populateMaterials = function(req, res){ console.log("populateMaterials"); for (var i = 0; i < materials.length; i++ ){ var mat = new models.Materials(); console.log(mat); mat.name = materials[i].variantName; mat.materialId = materials[i].itemNumberExtension; mat.surcharge = materials[i].priceOffset; for (var j = 0; j < materials[i].colors.length; j++){ mat.colors.colorName = materials[i].colors[j].name; mat.colors.colorId = materials[i].colors[j].itemNumberExtension; mat.colors.surcharge = materials[i].colors[j].priceOffset; } mat.save(function(err){ if(err){ console.log(err); } else { console.log('success'); } }); } res.render('index', { title: 'Express' }); }; 

Is this a smart approach to linking to a model in a separate module?

+49
mongoose express
Mar 31 '12 at 23:05
source share
2 answers

The basic approach seems reasonable.

As an option, you can consider the supplier module with integrated model and controller functions. Thus, you could force app.js to create an instance of the provider, and then all the functions of the controller can be performed by it. The app.js application should only specify routes with the appropriate controller functionality.

To clean up a little bit, you can also consider branching routes into a separate module with app.js as a glue between these modules.

+8
Apr 01 2018-12-12T00:
source share

I like to define the database outside the model file so that it can be configured using nconf. Another advantage is that you can reuse the Mongo connection outside of models.

 module.exports = function(mongoose) { var Material = new Schema({ name : {type: String, index: true}, id : ObjectId, materialId : String, surcharge : String, colors : { colorName : String, colorId : String, surcharge : Number } }); // declare seat covers here too var models = { Materials : mongoose.model('Materials', Material), SeatCovers : mongoose.model('SeatCovers', SeatCover) }; return models; } 

and then you would call it that ...

 var mongoose = require('mongoose'); mongoose.connect(config['database_url']); var models = require('./models')(mongoose); var velvet = new models.Materials({'name':'Velvet'}); 
+70
Aug 06 '13 at 21:24
source share



All Articles