@dan_koyuki/mongoose-connection-manager
v1.1.2
Published
A lightweight utility to manage multiple Mongoose connections with ease.
Maintainers
Readme
Readme not updated, however some function is stay same!
Mongoose Connection Manager
A lightweight and modular class for managing multiple MongoDB connections using Mongoose. Supports easy creation, retrieval, monitoring, and cleanup of named connections.
✨ Features
- 🔌 Add and store multiple named Mongoose connections
- 📡 Get connection state, active connection count, or MongoDB client
- 🧹 Graceful shutdown on
SIGINTwith auto-close of all connections - 🧾 Optional detailed connection listing (models, URIs, states)
- ✅ Clean API for integration in larger applications
📦 Installation
npm install mongoose-connection-manager🚀 Usage
- Add a new connection
await mongooseConnectionManager.addConnection("mainDB", "mongodb://localhost:27017/mydb", {
useNewUrlParser: true,
useUnifiedTopology: true,
});- Get a connection (to use with models, etc.)
const conn = mongooseConnectionManager.getConnection("mainDB");
const User = conn.model("User", new mongoose.Schema({ name: String }));- Get raw MongoDB client
const client = mongooseConnectionManager.getClient("mainDB");- Get connection state
const state = mongooseConnectionManager.getState("mainDB");
// 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting- List connections
const names = mongooseConnectionManager.getAllConnections();
// ['mainDB', 'logsDB', ...]
const details = mongooseConnectionManager.getAllConnections(true);
/*
[
{
name: 'mainDB',
state: 1,
models: ['User'],
uri: 'localhost/mydb'
},
...
]
*/- Get active connection count
const count = mongooseConnectionManager.getActiveConnectionCount();- Close one or all connections
await mongooseConnectionManager.closeConnection("mainDB");
await mongooseConnectionManager.closeAllConnections();
💡 All connections are automatically closed when the process receives a SIGINT (e.g., Ctrl+C).🧪 Example: Define models with specific connection
const conn = mongooseConnectionManager.getConnection("mainDB");
const User = conn.model("User", new mongoose.Schema({
username: String,
createdAt: { type: Date, default: Date.now }
}));
await User.create({ username: "dan" });📚 API Summary
| Method | Description | | -------|-------------| | addConnection(name, url, options?) |Creates and stores a new Mongoose connection. | getConnection(name)| Retrieves a specific connection. getClient(name) |Retrieves the underlying MongoClient instance. getState(name) |Returns the readyState of a connection. getAllConnections(withDetails?) |Returns names or full details of all connections. getActiveConnectionCount() |Returns the number of active connections. closeConnection(name) |Closes a specific connection. closeAllConnections() |Closes all active connections.
