mongodb-connection-lib
Version:
A comprehensive MongoDB connection library with CRUD operations, connection pooling, and model abstraction
35 lines (29 loc) • 847 B
JavaScript
const { MongoClient } = require("mongodb");
require("dotenv").config();
class DatabaseConfig {
constructor() {
this.uri = process.env.MONGODB_URI;
this.dbName = process.env.MONGODB_DB_NAME;
this.options = {
minPoolSize: parseInt(process.env.MONGODB_MIN_POOL_SIZE) || 5,
maxPoolSize: parseInt(process.env.MONGODB_MAX_POOL_SIZE) || 10,
serverSelectionTimeoutMS:
parseInt(process.env.MONGODB_SERVER_SELECTION_TIMEOUT) || 5000,
socketTimeoutMS: 45000,
connectTimeoutMS: 10000,
heartbeatFrequencyMS: 10000,
retryWrites: true,
retryReads: true,
};
}
getConnectionString() {
return this.uri;
}
getOptions() {
return this.options;
}
getDatabaseName() {
return this.dbName;
}
}
module.exports = new DatabaseConfig();