feat: V2 with TypeScript

This commit is contained in:
Ben Elferink
2024-12-14 16:14:14 +02:00
parent f6c0a52ab5
commit a41cab872f
63 changed files with 13390 additions and 35925 deletions

37
server/src/utils/mongo.ts Normal file
View File

@@ -0,0 +1,37 @@
import mongoose from 'mongoose'
import { MONGO_URI, MONGO_OPTIONS } from '../constants/index'
class Mongo {
instance: typeof mongoose = mongoose
mongoUri: string
mongoOptions: mongoose.ConnectOptions
isConnected: boolean
constructor() {
this.mongoUri = MONGO_URI
this.mongoOptions = MONGO_OPTIONS
this.isConnected = false
}
async connect() {
if (this.isConnected) return
try {
console.log('⏳ Connecting to MongoDB')
const db = await this.instance.connect(this.mongoUri, this.mongoOptions)
const connection = db.connection
this.isConnected = connection.readyState === 1
if (this.isConnected) console.log('✅ MongoDB connected')
connection.on('connected', () => console.log('✅ MongoDB connected')) // re-connected
connection.on('disconnected', () => console.log('❌ MongoDB disconnected')) // disconnected
connection.on('error', (error) => console.log('❌ MongoDB connection error', error)) // listen for errors during the session
} catch (error: any) {
console.log('❌ MongoDB connection error:', error.message)
}
}
}
export default new Mongo()