I have the following Node.js. directory structure
|
|
|
|
|
|
|
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
}
});
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?