node.js 在单独的模块中定义猫鼬模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9960486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defining Mongoose Models in Separate Module
提问by rob_hicks
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?
这是在单独的模块中引用模型的合理方法吗?
采纳答案by almypal
The basic approach looks reasonable.
基本方法看起来很合理。
As an option you could consider a 'provider' module with model and controller functionality integrated. That way you could have the app.js instantiate the provider and then all controller functions can be executed by it. The app.js has to only specify the routes with the corresponding controller functionality to be implemented.
作为一种选择,您可以考虑使用集成了模型和控制器功能的“提供者”模块。这样你就可以让 app.js 实例化提供者,然后所有的控制器功能都可以由它执行。app.js 只需指定具有要实现的相应控制器功能的路由。
To tidy up a bit further you could also consider branching out the routes into a separate module with app.js as a glue between these modules.
为了进一步整理,您还可以考虑将路由分支到一个单独的模块中,使用 app.js 作为这些模块之间的粘合剂。
回答by Michael Connor
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.
我喜欢在模型文件之外定义数据库,以便可以使用 nconf 对其进行配置。另一个优点是您可以在模型之外重用 Mongo 连接。
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'});

