mirror of
https://github.com/MedzikUser/gatsby-pingbot
synced 2024-08-15 03:20:27 +00:00
27 lines
462 B
TypeScript
27 lines
462 B
TypeScript
import { connect } from 'mongoose'
|
|
|
|
import type { CachedMongo } from '../types/mongo'
|
|
|
|
const { MONGODB_URI } = process.env
|
|
|
|
let cached: CachedMongo = global.mongo
|
|
|
|
if (!cached) {
|
|
cached = global.mongo = {
|
|
conn: null,
|
|
promise: null
|
|
}
|
|
}
|
|
|
|
export default async function dbConnect() {
|
|
if (cached.conn)
|
|
return cached.conn
|
|
|
|
if (!cached.promise)
|
|
cached.promise = connect(MONGODB_URI)
|
|
|
|
cached.conn = await cached.promise
|
|
|
|
return cached.conn
|
|
}
|