Skip to content

Commit

Permalink
feat: backup mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukaYamamoto0 committed May 9, 2024
1 parent a9171ca commit 4db70b4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
3 changes: 2 additions & 1 deletion locart.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default {
},
database: {
// The URL to connect to MongoDB
mongodb_url: "mongodb://localhost",
database_url:
"mongodb+srv://admim:95941412@cluster0.39vdz0z.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0",
},
// storage settings
storage: {
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"cors": "^2.8.5",
"express": "^4.19.2",
"helmet": "^7.1.0",
"mongoose": "^8.3.3",
"mongoose": "^8.3.4",
"multer": "^1.4.5-lts.1",
"simpl.db": "^2.13.0"
},
Expand Down
19 changes: 19 additions & 0 deletions src/core/scripts/db.connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { connect } from "mongoose";
import configs from "../../../locart.config.js";

export default async function connectDatabase() {
const url = configs.database["database_url"];

if (!url) {
console.error("[Database] - Unable to find database connection url");
process.exit(1);
}

try {
await connect(url);
console.log("[Database] - Connected successfully");
} catch (error) {
console.error("[Database] - Error trying to connect:", error);
process.exit(1);
}
}
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import express from "express";
import configs from "../locart.config.js";
import cors from "cors";
import helmet from "helmet";

import configs from "../locart.config.js";
import imageRouter from "./routes/images.router.js";
import connectDatabase from "./core/scripts/db.connection.js";

const app = express();
const port = configs.server.port || 3000;

await connectDatabase();

// Express configuration
app.use(cors());
app.use(helmet());
Expand Down

0 comments on commit 4db70b4

Please sign in to comment.