-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
29 lines (23 loc) · 837 Bytes
/
db.ts
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
import mongoose from "mongoose"
export const { MONGODB_CONNECTION_STRING } = process.env
if (!MONGODB_CONNECTION_STRING) throw "MONGODB_CONNECTION_STRING is not set"
export const redactedConnectionString = MONGODB_CONNECTION_STRING.replace(
/:.*@/,
"://***:***@"
)
export const connect = () =>
new Promise((resolve, reject) => {
console.log(`[MongoDB] Connecting to ${redactedConnectionString}...`)
mongoose
.connect(MONGODB_CONNECTION_STRING)
.then(() => {
console.log("[Mongoose] Initial connection successful")
resolve("")
})
.catch((error: Error) => {
console.log("[Mongoose] Initial connection failed, retrying...")
console.error(error)
setTimeout(connect, 5000)
})
})
export const getConnectionState = () => mongoose.connection.readyState