Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to make mongoose connection / any database connection

I have read several stackoverflow documents and mongoose documentations regarding mongoose.connect() and mongoose.createConnection(), but there does not seem to be a clear explanation on the latter, specifically where to make the connection. I am unsure of where the best place to make mongoose or any database connections are but I usually do them in my server.js file.

Is it okay to make connections in the model itself? When using mongoose.createConnection(), it seems like making the connection in the model is the only option, since if I make a connection using createConnection in server file, I cannot export the connection object that is returned and import it in my model file.

When I use just mongoose.connect() I usually have this set up in my server file,

var databaseUri = "mongodb://localhost/sampledatabse1020";

if (process.env.MONGODB_URI) {
    mongoose.connect(process.env.MONGODB_URI);
} else {
    mongoose.connect(databaseUri)
}

var database = mongoose.connection;

database.on("error", function(err) {
  console.log("Mongoose Error: ", err);
});

database.once("open", function() {
  console.log("Mongoose connection successful.");
});

but as I previously stated, if I changed the above code and used

var connection = mongoose.createConnection()

I cannot export the connection variable which hold the connection object returned from mongoose.createConnection(). So would I have to make it in the model it self such as

var mongoose = require("mongoose");

var connection = mongoose.createConnection('mongodb://localhost/food');

var Schema = mongoose.Schema;

var UserSchema = new Schema({
  first_name: {
    type: String,
    trim: true,
    required: "First Name is Required"
  },
  last_name: {
    type: String,
    trim: true,
    required: "Last Name is Required"
  },
  email: {
    type: String,
    trim: true,
    required: "Email is Required"
  }
});

var User = connection.model('User', UserSchema);

module.exports = User;

I also understand another option is to have a separate file to make all database connections rather than in server file, but if I do it this way, how can I do it so that connections are only made when it is necessary?

UPDATE: I added the createConnection() inside the model and it seems to be working well. However, I am aware that creating connections inside the model is not ideal, but if that is so, where can I set up the createConnection(), since I can't export it and import it in the models when its defined in another file for example in server.js. Any suggestions would be appreciated.

like image 391
henhen Avatar asked Jan 29 '23 11:01

henhen


1 Answers

You could make a mongoose.js file like so:

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI, {useMongoClient: true});

module.exports = {mongoose};

Which you then require in your mongoose model files like so:

const mongoose = require('mongoose');

That way you only write the connection code once, and are able to import it to wherever you see fit.

like image 64
OArnarsson Avatar answered Feb 01 '23 23:02

OArnarsson