I have the following Node.js directory structure.
|--app/
|--app.js
|--routers/
|--index.js/
|--models/
|--schemas.js
|--post.js
In app.js, there's a line likes this mongoose.connect('mongodb://localhost/' + config.DB_NAME);
. In 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;
In post.js:
var mongoose = require('mongoose')
, PostSchema = require('./schemas').PostSchema
, PostModel = mongoose.model('Post', PostSchema);
module.exports = PostModel;
And in index.js, there might be a line likes this: var PostModel = require('../models/post');
. All files mentioned above require mongoose. The purpose of schemas.js is for helping programmers to grasp the schema of the database in a single file. However, I wonder if this implement causes redundancy and cause more overhead, for I require mongoose here and there. Should I pass it as an argument around?