mqtt_manager_common
Version:
Common functions for the manager
56 lines (44 loc) • 1.32 kB
JavaScript
const MONGOOSE = require('mongoose');
class MongooseUtils{
constructor(){
this.mongooseConnect();
}
mongooseConnect() {
MONGOOSE.connect('mongodb://localhost:27017/weather_ai', { keepAlive: 120, poolSize: 4 });
MONGOOSE.connection.on('connected', this.onConnection);
MONGOOSE.connection.on('disconnect', this.onDisconnect);
MONGOOSE.connection.on('close', this.onConnectionClose);
MONGOOSE.connection.on('error', this.onError)
}
mongooseDisconnect() {
MONGOOSE.disconnect();
}
onConnection() {
this.isConnected = true;
process.on('SIGINT', function() {
MONGOOSE.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
}
onDisconnect() {
this.isConnected = false;
}
onConnectionClose() {
this.isConnected = false;
}
onError(err) {
console.error(err);
console.error("Make sure that mongodb is running");
process.exit(0)
}
isConnected() {
return this.isConnected;
}
get mongoose() {
return MONGOOSE;
}
}
var instance = new MongooseUtils();
module.exports = instance;