2023-08-26 11:03:14 -04:00
|
|
|
import TClient from '../client';
|
2023-08-24 12:06:39 -04:00
|
|
|
import mongoose from 'mongoose';
|
2023-09-01 00:32:11 -04:00
|
|
|
import LogPrefix from '../helpers/LogPrefix.js';
|
2023-08-24 12:06:39 -04:00
|
|
|
|
2023-09-01 00:32:11 -04:00
|
|
|
const connection:mongoose.Connection = mongoose.connection;
|
|
|
|
export default class DatabaseServer {
|
|
|
|
static connect(client: TClient) {
|
2023-09-03 04:56:22 -04:00
|
|
|
const Logger = (logType:'log'|'error', msg:string)=>console[logType](client.logTime(), LogPrefix('DATABASE'), msg);
|
2023-08-24 12:06:39 -04:00
|
|
|
|
2023-09-01 00:32:11 -04:00
|
|
|
connection.set('strictQuery', true);
|
|
|
|
connection.openUri(client.tokens.mongodb_uri, {
|
|
|
|
replicaSet: 'toastyy',
|
|
|
|
autoIndex: true,
|
|
|
|
authMechanism: 'SCRAM-SHA-256',
|
|
|
|
authSource: 'admin',
|
|
|
|
serverSelectionTimeoutMS: 15000,
|
|
|
|
waitQueueTimeoutMS: 50000,
|
|
|
|
socketTimeoutMS: 30000,
|
|
|
|
tls: false,
|
|
|
|
family: 4
|
|
|
|
});
|
2023-08-24 12:06:39 -04:00
|
|
|
|
2023-09-01 00:32:11 -04:00
|
|
|
connection
|
|
|
|
.on('connecting', ()=>Logger('log','Establishing connection to MongoDB'))
|
|
|
|
.on('connected', ()=>Logger('log','Connection to MongoDB has been established'))
|
|
|
|
.on('disconnecting', ()=>Logger('log','Disconnecting from MongoDB'))
|
|
|
|
.on('disconnected', ()=>Logger('log','Disconnected from MongoDB'))
|
|
|
|
.on('close', ()=>Logger('log','MongoDB has closed the connection'))
|
|
|
|
.on('all', ()=>Logger('log','Successfully established a connection to all members'))
|
|
|
|
.on('fullsetup', ()=>Logger('log','Successfully established a connection to Primary server & atleast one member'))
|
|
|
|
.on('error', (err:mongoose.Error)=>Logger('error',`Encountered an error in MongoDB: ${err.message}`));
|
|
|
|
}
|
2023-08-24 12:06:39 -04:00
|
|
|
}
|