Node.js - Does the mongoose demand here and there cause redundancy?

I have the following Node.js. directory structure

|--app/
   |--app.js
   |--routers/
      |--index.js/
   |--models/
      |--schemas.js
      |--post.js

In app.js you will like the line 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 index.js such command can be: var PostModel = require('../models/post');. All the files mentioned above require mongoose . The purpose of schemas.js is to help programmers understand the database schema in a single file. However, I wonder if this implementation causes redundancy and additional overhead, since I need mongoose here and there. Should I pass it as an argument around?

+5
2

, . http://nodejs.org/docs/latest/api/modules.html#modules_caching:

. ( ), require('foo') , .

require('foo') . . . , " " , , .

, , .

, requiring Mongoose ; , , Mongoose.

+8

All Articles