我有以下 Node.js 目录结构。
|--app/
|--app.js
|--routers/
|--index.js/
|--models/
|--schemas.js
|--post.js
在app.js中,有这样一行mongoose.connect('mongodb://localhost/' + config.DB_NAME);
。在schema.js 中:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var PostSchema = new Schema({
title: String
, author: String
, body: String
, creataAt: {
type: Date
, default: Date.now
}
});
// other schemas goes here
module.exports.PostSchema = PostSchema;
在post.js 中:
var mongoose = require('mongoose')
, PostSchema = require('./schemas').PostSchema
, PostModel = mongoose.model('Post', PostSchema);
module.exports = PostModel;
在index.js中,可能会有这样一行:var PostModel = require('../models/post');
. 上面提到的所有文件都需要mongoose。schemas.js的目的是帮助程序员在单个文件中掌握数据库的模式。但是,我想知道这个工具是否会导致冗余并导致更多开销,因为我到处都需要猫鼬。我应该把它作为一个论点传递吗?