generated from comp1800/web_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
28 lines (22 loc) · 753 Bytes
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Grid from "gridfs-stream";
import mongoose from "mongoose";
import dotenv from "dotenv";
dotenv.config();
const config = {
user: process.env.MONGODB_USER,
pass: process.env.MONGODB_PASSWORD,
host: process.env.MONGODB_HOST,
db: process.env.MONGODB_DATABASE,
option: process.env.MONGODB_OPTION
};
export const mongoUri = `mongodb+srv://${config.user}:${config.pass}@${config.host}/${config.db}?${config.option}`;
let gfs;
mongoose.connection.once("open", () => {
gfs = Grid(mongoose.connection.db, mongoose.mongo);
gfs.collection("uploads");
});
export { gfs };
export default async function connectDB() {
await mongoose.connect(mongoUri);
console.log("Successfully connected to MongoDB at", config.db);
}