Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining Mongoose Models in Separate Module

I would like to separate my Mongoose models in a separate file. I have attempted to do so like this:

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 have attempted to use the model like this:

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 reasonable approach to referencing a model in a separate module?

like image 345
rob_hicks Avatar asked Mar 31 '12 23:03

rob_hicks


People also ask

What defines a data model in Mongoose?

Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

Can you change a Mongoose schema?

There's nothing built into Mongoose regarding migrating existing documents to comply with a schema change. You need to do that in your own code, as needed.

Is Mongoose an ORM or ODM?

Mongoose is an ODM that provides a straightforward and schema-based solution to model your application data on top of MongoDB's native drivers.


1 Answers

I like to define the database outside of the models file so that it can be configured using nconf. Another advantage is that you can reuse the Mongo connection outside of the 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 like this...

var mongoose = require('mongoose'); mongoose.connect(config['database_url']); var models = require('./models')(mongoose); var velvet = new models.Materials({'name':'Velvet'}); 
like image 192
Michael Connor Avatar answered Sep 22 '22 19:09

Michael Connor