Mongoose Schema&重构

时间:2014-09-29 14:01:36

标签: mongodb mongoose

予。这项工作:

// dbModuleSchema.js

var mongoose = require('mongoose');

function getDbModuleSchema() {
    var dbModuleSchema = mongoose.Schema({
    roles : [rolesSchema] <--------------------------- from local variable
   };

    ///////////////////////////////////
    // local variable for roles schema 
    ///////////////////////////////////
    var rolesSchema = mongoose.Schema({ //<------------ local variable
        name : {type : String},
        description : {type: String}
    };
    return dbModuleSchema;
};

exports.getDbModuleSchema = getDbModuleSchema;

II。这不是:

// dbModuleSchema.js

var mongoose = require('mongoose'),
    securitySchema = require('./../security/securitySchema');

function getDbModuleSchema() {
    var dbModuleSchema = mongoose.Schema({
    roles : [securitySchema.getRolesSchema] <-- from separate securitySchema module
   }
    return dbModuleSchema;
};

exports.getDbModuleSchema = getDbModuleSchema;

// securitySchema.js

var mongoose = require('mongoose');

function getRolesSchema() { //<------ separate securitySchema module
    var rolesSchema = mongoose.Schema({
        name : {type : String},
        description : {type: String}
    };
    return rolesSchema;
};

exports.getRolesSchema = getRolesSchema;

III。以此作为我的json:

{
    "moduleStyleId" : "style-0",
    "name" : "My Dashboard",
    "roles" : [{"name" : "Employee", "description" : "Everyone"}]
},

IV。 I&amp;和I&amp; II。

在I中,我在同一个模块中声明了角色模式,在II中,我将它重构为它自己的单独模块,并在moduleSchema中调用它的函数。

我不被允许这样做吗?

1 个答案:

答案 0 :(得分:0)

想出来,关键是首先将远程模块传递给变量:

var mongoose = require('mongoose'),
    securitySchema = require('../security/securitySchema');

function getDbModuleSchema() {
    var rolesSchema = securitySchema.getRolesSchema(); <--- pass remote schema to variable first
    var dbModuleSchema = mongoose.Schema({
        roles : [rolesSchema],
       };
    return dbModuleSchema;
};

exports.getDbModuleSchema = getDbModuleSchema;

谢谢,伙计们,不能用“youse”来完成它! ;)