-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
42 lines (28 loc) · 881 Bytes
/
Copy pathdatabase.js
File metadata and controls
42 lines (28 loc) · 881 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* we will use this to connect/hook to our database */
import mongoose from 'mongoose';
// track the connection status
let isConnected = false;
export const connectToDB = async ()=> {
mongoose.set("strictQuery", true);
// are we currently connected?
/*
remember nextJS routing is serverless, there is no permanent connection
functions get called only when needed.
*/
if (isConnected) {
console.log("mongoDB is already connectd");
return;
}
try {
// connect to your mongoDB database
await mongoose.connect(process.env.MONGODB_URI, {
dbName: "share_prompt",
useNewURLParser: true,
useUnifiedTopology: true
})
// update connection status tracker
isConnected = true;
} catch (error) {
console.log(error);
}
}