-
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: error handling connection, use dotenv
- Loading branch information
Showing
5 changed files
with
42 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 +1,15 @@ | ||
import dotenv from "dotenv"; | ||
|
||
const CONFIG = dotenv.config(); | ||
const { | ||
parsed: { DB_USER, DB_PASSWORD, ...rest } | ||
} = CONFIG; | ||
|
||
const URI = `mongodb+srv://${DB_USER}:${DB_PASSWORD}@cluster0-diifo.mongodb.net/db_z0?retryWrites=true&w=majority`; | ||
|
||
const config = { | ||
port: 8000, | ||
jwtExpirationSeconds: 300, | ||
JWT_KEY: "my_secret" | ||
URI, | ||
...rest | ||
}; | ||
|
||
export default config; |
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,29 +1,36 @@ | ||
import mongoose from "mongoose"; | ||
import config from "../config"; | ||
|
||
const uri = | ||
"mongodb+srv://agileactors:[email protected]/db_z0?retryWrites=true&w=majority"; | ||
const db = mongoose.connection; | ||
const { URI } = config; | ||
|
||
// setup mongoose | ||
mongoose.Promise = global.Promise; | ||
mongoose.set("useCreateIndex", true); | ||
|
||
const connect = () => { | ||
try { | ||
mongoose.connect(uri, { | ||
const db = mongoose.connection; | ||
const resultP = new Promise((resolve, reject) => { | ||
mongoose.connect(URI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}); | ||
|
||
//bind connection to error event (to get notification of connection errors) | ||
db.on("error", console.error.bind(console, "MongoDB connection error:")); | ||
db.on("error", error => | ||
reject({ | ||
success: false, | ||
message: error | ||
}) | ||
); | ||
|
||
db.once("open", () => { | ||
console.log("database connection: OK"); | ||
resolve({ | ||
success: true, | ||
message: "database connection: OK" | ||
}); | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}); | ||
|
||
return resultP; | ||
}; | ||
|
||
export default connect; |
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