Created
November 8, 2021 13:25
-
-
Save OmkarK45/45122376a91f0d0517f08d58c5af1972 to your computer and use it in GitHub Desktop.
Connection to MongoDB
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
import mongoose from 'mongoose' | |
import log from '../utils/logger' | |
/** | |
* @description - This function creates connection to the database with given options. | |
* Resolves to connection with DB or Failure with an error message | |
* */ | |
export const makeConnection = async () => { | |
await mongoose | |
.connect( | |
process.env.NODE_ENV === 'development' | |
? process.env.DB_LOCAL! | |
: process.env.DB_ATLAS!, | |
{ | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
// feel free to change this to true if u use fineOneAndUpdate() | |
useFindAndModify: false, | |
useCreateIndex: true, | |
} | |
) | |
.then(() => { | |
log.info(`[${process.env.NODE_ENV}]` + '📀 Connected to Database') | |
}) | |
.catch((error: Error) => { | |
log.error( | |
`There was an error while connecting to database. | |
You likely forgot to include mongoDB connection URL or it is invalid.`, | |
error | |
) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment