From 14ae4d44fefaf70d17ef46f2c2401d0da14d1fe8 Mon Sep 17 00:00:00 2001 From: Michel Pereira Date: Sat, 23 Oct 2021 14:08:47 -0300 Subject: [PATCH] Reconfigura models/connection.js --- .gitignore | 2 +- .vscode/settings.json | 2 ++ models/connection.js | 20 ++++++++------------ package-lock.json | 2 +- package.json | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index b299fba..44fc308 100644 --- a/.gitignore +++ b/.gitignore @@ -120,7 +120,7 @@ fabric.properties .LSOverride # Icon must end with two \r -Icon +Icon # Thumbnails ._* diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/models/connection.js b/models/connection.js index e167e7b..e691381 100644 --- a/models/connection.js +++ b/models/connection.js @@ -1,29 +1,25 @@ const { MongoClient } = require('mongodb'); +require('dotenv').config(); const OPTIONS = { useNewUrlParser: true, useUnifiedTopology: true, }; - -// Para usar com o avaliador: -// const MONGO_DB_URL = 'mongodb://mongodb:27017/StoreManager'; - -// Para uso local: - -const MONGO_DB_URL = 'mongodb://localhost:27017/StoreManager'; - -// const DB_NAME = 'StoreManager'; // outra maneira +const MONGO_DB_URL = process.env.DB_URL || 'mongodb://mongodb:27017/StoreManager'; let db = null; - + const connection = () => (db ? Promise.resolve(db) : MongoClient.connect(MONGO_DB_URL, OPTIONS) .then((conn) => { - // db = conn.db(DB_NAME); // outra maneira db = conn.db(); return db; + }) + .catch((err) => { + console.log(err); + process.exit(1); })); - + module.exports = connection; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d3f9b8d..8620ebc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@hapi/boom": "^9.1.0", "@hapi/joi": "^17.1.1", "body-parser": "^1.19.0", - "dotenv": "^8.2.0", + "dotenv": "^8.6.0", "express": "^4.17.1", "express-rescue": "^1.1.26", "faker": "^4.1.0", diff --git a/package.json b/package.json index 3d94cb4..66c8409 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@hapi/boom": "^9.1.0", "@hapi/joi": "^17.1.1", "body-parser": "^1.19.0", - "dotenv": "^8.2.0", + "dotenv": "^8.6.0", "express": "^4.17.1", "express-rescue": "^1.1.26", "faker": "^4.1.0",