在单独的模块中定义Mongoose模型

时间:2012-03-31 23:05:20

标签: node.js express mongoose

我想在一个单独的文件中分离我的Mongoose模型。我试图这样做:

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;

然后,我试图使用这样的模型:

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' });
};

这是在单独模块中引用模型的合理方法吗?

2 个答案:

答案 0 :(得分:73)

我喜欢在模型文件之外定义数据库,以便可以使用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;
}

然后你会这样称呼它......

var mongoose = require('mongoose');
mongoose.connect(config['database_url']);
var models = require('./models')(mongoose);
var velvet = new models.Materials({'name':'Velvet'});

答案 1 :(得分:8)

基本方法看起来很合理。

作为一种选择,您可以考虑集成了模型和控制器功能的“提供者”模块。这样你就可以让app.js实例化提供程序,然后所有控制器函数都可以由它执行。 app.js必须仅指定具有相应控制器功能的路由。

为了进一步整理你还可以考虑将路由分支到一个单独的模块中,app.js作为这些模块之间的粘合剂。