1

I have a Node/Express app that uses Mongoose to talk to a MongoDB database. The Express server is configured in a file called server.js and the schema is in a separate models.js file. Following every project and tutorial I've seen so far, I have the mongoose.connect() configured in both places:

// server.js
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const app = express();
mongoose.connect('mongodb://127.0.0.1/mydb');

// models.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const User = new Schema({
  username: String,
  password: { type: String, select: false },
  name: String,
});
module.exports = mongoose.model('User', User);

My question is, since I'm already importing mongoose in models.js, can I not skip doing so in server.js entirely and just set the connection in the model script itself? What's the point of importing it and configuring a connection along with the rest of the server config when I'm only going to use it when working with the schema? And If the answer is yes, why doesn't anyone do that? Is there a performance benefit at play here?

TheLearner
  • 2,813
  • 5
  • 46
  • 94

3 Answers3

1

You could do mongoose.connect() in your model script but this would not be advisable.

You would have to ensure that this model was loaded before any other scripts that required the mongoose connection.

If you go on to create another model in your application, then this would rely on your User model (model.js) having been loaded first.

You would have to perform the connection in each model just to be sure, but this would be a poor design and unnecessary code duplication.

Therefore, connecting in server.js is the best approach to ensure the connection is established as early as possible.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Steve Holgado
  • 11,508
  • 3
  • 24
  • 32
1

It is not necessary that you require the mongoose in the server.js file. In fact in my project I create a separate file for each connection like connection_one.js, connection_two.js and export the mongoose object. This way when any number of models does a require("./connection_one.js") it will return the same connection ready mongoose for all models exporting it. This is possible due to modules are cached on first time load inside a project. It is what is also happening when you are loading the mongoose module in server.js, it is the same mongoose object in server.js and model.js.

ViKiG
  • 764
  • 9
  • 21
-2

To address you question, first of all you need to understand object-oriented programming. Right now, you have two different files. One if server.js. and another is models.js. And each file has it's own scope.

Even if you imported mongoose in server.js since the two scopes has different scope set, models.js cannot utilize the mongoose service imported in server.js. For example, let say you defined a variable "foo", You cannot use that variable in model.js because their scopes are isolated.

// server.js
const foo = 'bar';

If you want to use only one mongoose imported in a single script and shared by others, you can use global object from Node.js env. Check out the url to know more about this. node.js global variables?

However, I don't really recommned putting mongoose service in gloabl object. Global scope can be easy at the beginning, but it could be significant scalability problem as your application grows bigger in later time.

Thanks.

supergentle
  • 1,001
  • 1
  • 14
  • 33
  • That lesson in OOP, which I think I've been doing for years now, doesn't answer the question. As is evident in the question, I am NOT using the db or its connection anywhere in server.js. The only time I'm invoking it is while working with the schema and I already have a mongoose import along with the connection statement in the models.js file. That's why I asked why we need mongoose or its setup in server.js at all. – TheLearner Aug 07 '17 at 04:54
  • I have another surprise for you. You will have to import mongoose in your route files as well. – JSEvgeny Aug 07 '17 at 05:54