Skip to content

Instantly share code, notes, and snippets.

@OmkarK45
Created November 8, 2021 13:25
Show Gist options
  • Save OmkarK45/45122376a91f0d0517f08d58c5af1972 to your computer and use it in GitHub Desktop.
Save OmkarK45/45122376a91f0d0517f08d58c5af1972 to your computer and use it in GitHub Desktop.
Connection to MongoDB
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