syia-mcp-vessel-accounts
Version:
MCP server for vessel account management including EyeShare API integration, vessel expenses, and purchase orders
200 lines • 6.8 kB
JavaScript
import { MongoClient } from 'mongodb';
import { logger } from './logger.js';
export class MongoDBManager {
constructor(config) {
this.client = null;
this.db = null;
this.config = config;
}
async connect() {
try {
const options = {
maxPoolSize: this.config.options?.maxPoolSize || 10,
serverSelectionTimeoutMS: this.config.options?.serverSelectionTimeoutMS || 5000,
socketTimeoutMS: this.config.options?.socketTimeoutMS || 45000,
connectTimeoutMS: this.config.options?.connectTimeoutMS || 10000,
};
this.client = new MongoClient(this.config.uri, options);
await this.client.connect();
this.db = this.client.db(this.config.database);
logger.info(`Connected to MongoDB database: ${this.config.database}`);
return true;
}
catch (error) {
logger.error(`MongoDB connection failed: ${error}`);
return false;
}
}
async disconnect() {
try {
if (this.client) {
await this.client.close();
this.client = null;
this.db = null;
logger.info('MongoDB connection closed');
}
}
catch (error) {
logger.error(`Error closing MongoDB connection: ${error}`);
}
}
async executeAggregation(collectionName, pipeline) {
const startTime = Date.now();
try {
if (!this.db) {
const connected = await this.connect();
if (!connected) {
return {
success: false,
error: 'Failed to connect to MongoDB',
};
}
}
if (!this.db) {
throw new Error('Database connection not established');
}
const collection = this.db.collection(collectionName);
// Execute the aggregation pipeline
const cursor = collection.aggregate(pipeline);
const results = await cursor.toArray();
const executionTime = Date.now() - startTime;
return {
success: true,
data: results,
count: results.length,
executionTime,
};
}
catch (error) {
const executionTime = Date.now() - startTime;
logger.error(`MongoDB aggregation failed: ${error}`);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
executionTime,
};
}
}
async executeFind(collectionName, filter = {}, options = {}) {
const startTime = Date.now();
try {
if (!this.db) {
const connected = await this.connect();
if (!connected) {
return {
success: false,
error: 'Failed to connect to MongoDB',
};
}
}
if (!this.db) {
throw new Error('Database connection not established');
}
const collection = this.db.collection(collectionName);
// Execute the find operation
const cursor = collection.find(filter, options);
const results = await cursor.toArray();
const executionTime = Date.now() - startTime;
return {
success: true,
data: results,
count: results.length,
executionTime,
};
}
catch (error) {
const executionTime = Date.now() - startTime;
logger.error(`MongoDB find operation failed: ${error}`);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
executionTime,
};
}
}
async testConnection() {
try {
if (!this.db) {
const connected = await this.connect();
if (!connected) {
return false;
}
}
if (!this.db) {
return false;
}
// Test with a simple ping
await this.db.admin().ping();
return true;
}
catch (error) {
logger.error(`MongoDB connection test failed: ${error}`);
return false;
}
}
async listCollections() {
try {
if (!this.db) {
const connected = await this.connect();
if (!connected) {
return {
success: false,
error: 'Failed to connect to MongoDB',
};
}
}
if (!this.db) {
throw new Error('Database connection not established');
}
const collections = await this.db.listCollections().toArray();
return {
success: true,
data: collections,
count: collections.length,
};
}
catch (error) {
logger.error(`Failed to list collections: ${error}`);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
async getCollectionStats(collectionName) {
try {
if (!this.db) {
const connected = await this.connect();
if (!connected) {
return {
success: false,
error: 'Failed to connect to MongoDB',
};
}
}
if (!this.db) {
throw new Error('Database connection not established');
}
const collection = this.db.collection(collectionName);
// Get basic collection info instead of stats
const count = await collection.countDocuments();
const info = {
name: collectionName,
count: count
};
return {
success: true,
data: [info],
count: 1,
};
}
catch (error) {
logger.error(`Failed to get collection stats: ${error}`);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
}
//# sourceMappingURL=mongodb.js.map