在单独的模块中定义猫鼬模型 [英] Defining Mongoose Models in Separate Module

查看:22
本文介绍了在单独的模块中定义猫鼬模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

基本方法看起来很合理.

The basic approach looks reasonable.

作为一个选项,您可以考虑集成模型和控制器功能的提供者"模块.这样你就可以让 app.js 实例化提供者,然后所有的控制器功能都可以由它执行.app.js 只需指定具有相应控制器功能的路由即可实现.

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 作为这些模块之间的粘合剂.

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.

这篇关于在单独的模块中定义猫鼬模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆