diff --git a/backend/.gitignore b/backend/.gitignore index 05647d5..dbe5e16 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -30,6 +30,7 @@ yarn-error.* # local env files .env*.local +.env # typescript *.tsbuildinfo diff --git a/backend/src/databases/mongo.ts b/backend/src/databases/mongo.ts index 95644e7..b47ed77 100644 --- a/backend/src/databases/mongo.ts +++ b/backend/src/databases/mongo.ts @@ -2,18 +2,37 @@ import mongoose from "mongoose"; import dotenv from 'dotenv' dotenv.config(); const user = process.env.MG_USER -const password = process.env.MG_PASS -const db = process.env.MG_DB +const password = process.env.MG_PASS +const db = process.env.MG_DB const myHost = process.env.HOST -const connectToDatabase = async () => { - try { - await mongoose.connect(`mongodb://${user}:${password}@${myHost}:27017/${db}`); - console.log("Conectado ao MongoDB"); - } catch (error) { - console.error("Erro ao conectar ao MongoDB", error); - process.exit(1); // Sair do processo em caso de falha na conexão - } -}; -export default connectToDatabase; +const uri = `mongodb://${user}:${password}@${myHost}/${db}`; +const uriLocal = "mongodb://localhost:27017/dnutri" + +export default function connectToDatabase() { + mongoose.connection.on("connected", () => console.log("Mongo conectado com sucesso!")); + mongoose.connection.on("disconnected", () => console.log("Mongo Desconectado com sucesso!")); + + + mongoose.connect(uriLocal, { + serverSelectionTimeoutMS: 5000, + maxPoolSize: 10, + serverApi: { version: '1', strict: true, deprecationErrors: true } + }) + .catch((e) => { + console.error("Connection Failed", e.message) + }) + + process.on("SIGINT", async () => { + try { + console.log("Connection closed"); + await mongoose.connection.close(); + process.exit(0); + } + catch (error) { + console.error("Problems to close the connection", error); + process.exit(1); + } + }) +} \ No newline at end of file diff --git a/backend/src/index.ts b/backend/src/index.ts index 54e8bc2..ab29a93 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -3,17 +3,17 @@ import createPostgresTables from "./models/postgres"; import dotenv from "dotenv"; import router from "./routes/userRoutes"; import CreateMongoDbCollections from "./models/mongo"; -import insertUser from "./controllers/createController"; dotenv.config(); const app = express(); const PORT = process.env.PORT || 3002; app.use(express.json()); -//createPostgresTables(); +createPostgresTables(); CreateMongoDbCollections(); //insertUser(); +app.use(router) app.listen(PORT, () => { console.log(`Servidor rodando na porta ${PORT}`); }); diff --git a/backend/src/models/mongo/index.ts b/backend/src/models/mongo/index.ts index 85aceea..c20f37c 100644 --- a/backend/src/models/mongo/index.ts +++ b/backend/src/models/mongo/index.ts @@ -4,8 +4,8 @@ import connectToDatabase from '../../databases/mongo' export default async function CreateMongoDbCollections() { try { connectToDatabase(); - /* await Data.createCollection() - await User.createCollection() */ + await Data.createCollection() + await User.createCollection() } catch (error: any) { console.error("Erro ao criar collections no MongoDB:", error.message)