vinc-mongo-db
v1.0.0
Published
Shared MongoDB connection pool for VINC applications
Downloads
103
Maintainers
Readme
@vinc/mongo-db
Shared MongoDB connection pool for multi-tenant applications using Mongoose.
Features
- LRU-cached connection pool with automatic eviction
- Efficient database switching via
useDb()(shares TCP connections) - Concurrent-safe connection creation
- Configurable pool size and TTL
- Graceful shutdown support
Installation
npm install @vinc/mongo-db mongoose lru-cacheUsage
import { getPooledConnection, closeAllConnections, getPoolStats } from '@vinc/mongo-db';
// Get a connection to a specific database
const connection = await getPooledConnection('my-database');
// Use the connection with Mongoose models
const MyModel = connection.model('MyModel', mySchema);
const docs = await MyModel.find();
// Check pool statistics
const stats = getPoolStats();
console.log(stats); // { active: 1, max: 50, pending: 0, baseConnected: true }
// Graceful shutdown
await closeAllConnections();Configuration
Set these environment variables:
| Variable | Default | Description |
|----------|---------|-------------|
| MONGO_URL | mongodb://localhost:27017 | MongoDB connection string |
| POOL_MAX_CONNECTIONS | 50 | Maximum pooled connections |
| POOL_PER_DB_SIZE | 10 | Connection pool size per database |
| POOL_TTL_MS | 1800000 (30 min) | Connection TTL in milliseconds |
License
MIT
