This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ♻️ função mongoose connect em db.js
- Loading branch information
1 parent
58fa41f
commit b6c3334
Showing
2 changed files
with
19 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
const mongoose = require('mongoose') | ||
require('dotenv').config() | ||
|
||
mongoose.connect(process.env.MONGO_URI) | ||
const connect = () => { | ||
mongoose.connect(process.env.MONGO_URI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
serverSelectionTimeoutMS: 5000 | ||
}) | ||
.then(() => console.log('Successful MongoDB connection!')) | ||
.catch((err) => { | ||
console.log('MongoDB connection error: ', err) | ||
setTimeout(connect, 5000) | ||
}) | ||
} | ||
|
||
const db = mongoose.connection | ||
mongoose.connection.on('disconnected', () => { | ||
console.log('MongoDB disconnected! Reconnecting...') | ||
setTimeout(connect, 5000) | ||
}) | ||
|
||
module.exports = { db } | ||
connect() | ||
|
||
module.exports = { db: mongoose.connection } |