npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

clean-nodejs-mongodb

v0.0.3

Published

A robust MongoDB database connection module with circuit breaker, retry logic, and utilities for Node.js applications

Readme

Clean Node.js MongoDB

A robust MongoDB database connection module with circuit breaker pattern, retry logic, and comprehensive utilities for Node.js applications.

Features

  • 🚀 Circuit Breaker Pattern: Automatic failure detection and recovery
  • 🔄 Retry Logic: Exponential backoff with configurable retry attempts
  • 📊 Connection Monitoring: Real-time metrics and health monitoring
  • 🛡️ Type Safety: Full TypeScript support with comprehensive interfaces
  • 📝 Advanced Logging: Structured logging with Winston integration
  • 🏗️ Repository Pattern: Base repository class for CRUD operations
  • 🔧 Flexible Configuration: Environment variables + explicit configuration

Installation

npm install clean-nodejs-mongodb

Quick Start

Environment Variables (Recommended for Production)

# .env
DATABASE_URI=mongodb://localhost:27017/myapp
DATABASE_RETRY_ATTEMPTS=5
DATABASE_POOL_MIN=2
DATABASE_POOL_MAX=20
import { DatabaseService, createDatabaseConfig } from 'clean-nodejs-mongodb';

// Initialize with environment variables
const dbService = await DatabaseService.init(createDatabaseConfig());

Explicit Configuration (Recommended for Testing)

import { DatabaseService } from 'clean-nodejs-mongodb';

const config = {
  uri: 'mongodb://localhost:27017/myapp',
  retryAttempts: 5,
  retryBaseDelay: 1000,
  circuitBreakerFailureThreshold: 3,
  connectionTimeout: 30000,
  poolSize: { min: 2, max: 20 }
};

const dbService = await DatabaseService.init(config);

Hybrid Approach (Environment + Overrides)

import { DatabaseService, createDatabaseConfig } from 'clean-nodejs-mongodb';

// Load from env vars but override specific settings
const dbService = await DatabaseService.init(createDatabaseConfig({
  retryAttempts: 10, // Override retry attempts
  uri: 'mongodb://prod-server:27017/prod-db' // Override URI
}));

Configuration Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | uri | string | Required | MongoDB connection URI | | retryAttempts | number | 3 | Number of retry attempts | | retryBaseDelay | number | 1000 | Base delay between retries (ms) | | retryMaxDelay | number | 30000 | Maximum delay between retries (ms) | | circuitBreakerFailureThreshold | number | 5 | Failures before opening circuit | | circuitBreakerRecoveryTimeout | number | 60000 | Recovery timeout (ms) | | connectionTimeout | number | 30000 | Connection timeout (ms) | | poolSize.min | number | 1 | Minimum pool size | | poolSize.max | number | 10 | Maximum pool size |

Environment Variables

| Variable | Default | Description | |----------|---------|-------------| | DATABASE_URI | mongodb://localhost:27017/defaultdb | MongoDB connection URI | | MONGO_URI | - | Alternative MongoDB URI | | DATABASE_RETRY_ATTEMPTS | 3 | Retry attempts | | DATABASE_RETRY_BASE_DELAY | 1000 | Base retry delay | | DATABASE_RETRY_MAX_DELAY | 30000 | Max retry delay | | DATABASE_CIRCUIT_BREAKER_THRESHOLD | 5 | Circuit breaker threshold | | DATABASE_CIRCUIT_BREAKER_RECOVERY | 60000 | Recovery timeout | | DATABASE_CONNECTION_TIMEOUT | 30000 | Connection timeout | | DATABASE_POOL_MIN | 1 | Min pool size | | DATABASE_POOL_MAX | 10 | Max pool size |

Usage Examples

Basic Repository Usage

import { BaseRepository } from 'clean-nodejs-mongodb';
import { Schema, model, Document } from 'mongoose';

interface IUser extends Document {
  name: string;
  email: string;
  createdAt: Date;
}

const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  createdAt: { type: Date, default: Date.now }
});

const UserModel = model<IUser>('User', userSchema);

class UserRepository extends BaseRepository<IUser> {
  constructor() {
    super(UserModel);
  }

  async findByEmail(email: string): Promise<IUser | null> {
    return this.model.findOne({ email }).exec();
  }
}

// Usage
const userRepo = new UserRepository();
const user = await userRepo.create({ name: 'John Doe', email: '[email protected]' });

Connection Management

import { DatabaseService, createDatabaseConfig } from 'clean-nodejs-mongodb';

// Initialize
const dbService = await DatabaseService.init(createDatabaseConfig());

// Check status
const status = dbService.connectionManager.getStatus();
console.log('Connected:', status.isConnected);
console.log('Circuit State:', status.circuitState);

// Force reconnect if needed
await dbService.connectionManager.forceReconnect();

// Graceful shutdown
await dbService.connectionManager.disconnect();

Error Handling

import {
  DatabaseConnectionError,
  DatabaseCircuitBreakerError,
  DatabaseQueryError
} from 'clean-nodejs-mongodb';

try {
  const dbService = await DatabaseService.init(config);
} catch (error) {
  if (error instanceof DatabaseConnectionError) {
    console.error('Connection failed:', error.message);
  } else if (error instanceof DatabaseCircuitBreakerError) {
    console.error('Circuit breaker open:', error.message);
  }
}

Advanced Features

Circuit Breaker States

  • CLOSED: Normal operation
  • OPEN: Failing fast due to repeated failures
  • HALF_OPEN: Testing if service recovered

Connection Monitoring

The module provides comprehensive metrics:

  • Connection attempts and success/failure counts
  • Average connection time
  • Downtime tracking
  • Circuit breaker state

Automatic Reconnection

The module automatically handles:

  • Connection drops
  • Network failures
  • Server restarts
  • Replica set failovers

Testing

import { DatabaseService } from 'clean-nodejs-mongodb';

// Use explicit config for predictable testing
const testConfig = {
  uri: 'mongodb://localhost:27017/test',
  retryAttempts: 1, // Faster tests
  connectionTimeout: 5000
};

const dbService = await DatabaseService.init(testConfig);
// ... run tests
await dbService.connectionManager.disconnect();

API Reference

DatabaseService

  • static init(config: DatabaseModuleConfig): Promise<DatabaseService>
  • connectionManager: DatabaseConnectionManager

DatabaseConnectionManager

  • connect(): Promise<void>
  • disconnect(): Promise<void>
  • isConnected(): boolean
  • getStatus(): ConnectionStatus
  • forceReconnect(): Promise<void>
  • resetCircuitBreaker(): void

BaseRepository

  • findById(id: string): Promise<T | null>
  • findAll(filter?: any): Promise<T[]>
  • create(data: Partial<T>): Promise<T>
  • update(id: string, data: Partial<T>): Promise<T | null>
  • delete(id: string): Promise<boolean>
  • count(filter?: any): Promise<number>
  • exists(id: string): Promise<boolean>

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License - see the LICENSE file for details.

Support

For issues and questions, please open an issue on GitHub.