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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dan_koyuki/mongoose-connection-manager

v1.1.2

Published

A lightweight utility to manage multiple Mongoose connections with ease.

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 SIGINT with 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

  1. Add a new connection
await mongooseConnectionManager.addConnection("mainDB", "mongodb://localhost:27017/mydb", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
  1. Get a connection (to use with models, etc.)
const conn = mongooseConnectionManager.getConnection("mainDB");
const User = conn.model("User", new mongoose.Schema({ name: String }));
  1. Get raw MongoDB client
const client = mongooseConnectionManager.getClient("mainDB");
  1. Get connection state
const state = mongooseConnectionManager.getState("mainDB");
// 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting
  1. List connections
const names = mongooseConnectionManager.getAllConnections();
// ['mainDB', 'logsDB', ...]

const details = mongooseConnectionManager.getAllConnections(true);
/*
[
  {
    name: 'mainDB',
    state: 1,
    models: ['User'],
    uri: 'localhost/mydb'
  },
  ...
]
*/
  1. Get active connection count
const count = mongooseConnectionManager.getActiveConnectionCount();
  1. 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.