-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.js
More file actions
40 lines (32 loc) · 795 Bytes
/
connector.js
File metadata and controls
40 lines (32 loc) · 795 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
import mongoose from 'mongoose'
const MONGODB_URI = process.env.MONGODB_URL
if (!MONGODB_URI) {
throw new Error(
'Please define the MONGODB_URI environment variable inside .env.local',
)
}
let cached = global.mongoose
if (!cached) {
cached = global.mongoose = {conn: null, promise: null}
}
async function dbConnect() {
if (cached.conn) {
return cached.conn
}
if (!cached.promise) {
const opts = {
minPoolSize: 4,
maxPoolSize: 50,
useNewUrlParser: true,
useUnifiedTopology: true,
connectTimeoutMS: 10000,
socketTimeoutMS: 45000,
}
cached.promise = mongoose.connect(MONGODB_URI, opts).then(mongoose => {
return mongoose
})
}
cached.conn = await cached.promise
return cached.conn
}
export default dbConnect