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

@tamasha/mongo-connection

v1.0.0

Published

Promise-based MongoDB connection helper using mongoose with configurable read preference

Readme

@tamasha/mongo-connection

Promise-based MongoDB connection helper built on top of mongoose. It keeps a singleton connection, exposes lifecycle logs, and lets you configure the readPreference (and other mongoose options) per project.

Installation

npm install @tamasha/mongo-connection

Requires Node.js 18+ and MongoDB 4.4+.

Quick Start

import { mongoDbConnection } from "@tamasha/mongo-connection";

async function bootstrap() {
  await mongoDbConnection({
    uri: process.env.MONGODB_CONNECTION_URL,
    readPreference: "secondaryPreferred",
    autoIndex: false,
  });

  // Your mongoose models can be registered here
}

bootstrap().catch((error) => {
  console.error("Failed to start application:", error);
  process.exit(1);
});

API

mongoDbConnection(options?: MongoDbConnectionOptions): Promise<typeof mongoose>

Creates (or reuses) a singleton connection. The promise resolves once the connection is ready. Subsequent calls reuse the same connection.

| Option | Type | Description | |--------|------|-------------| | uri | string | Connection string. Defaults to process.env.MONGODB_CONNECTION_URL. | | readPreference | ConnectOptions["readPreference"] | Preferred read preference. Defaults to "secondaryPreferred". | | autoIndex | boolean | Enable or disable automatic index building. Defaults to false. | | options | ConnectOptions | Additional mongoose.connect options. | | reconnectOnDisconnect | boolean | Auto-reconnect if the connection drops. Defaults to true. |

disconnectMongo(): Promise<void>

Gracefully closes the current connection and resets the singleton.

Connection Events

The package logs key connection events: connecting, connected, open, error, disconnected, and reconnected. When reconnectOnDisconnect is enabled, the package will automatically retry connection attempts using the provided configuration.

Real-World Usage Example

See examples/usage.ts for a complete walkthrough, including:

  • Setting up the connection with environment variables.
  • Registering models and running queries.
  • Gracefully shutting down on process signals.

External Project Integration

  1. Add @tamasha/mongo-connection to your project.
  2. Initialize the connection during application bootstrap (before defining models or starting HTTP servers).
  3. Use your mongoose models as usual—the helper ensures the underlying connection is ready and shared across the app.
  4. During shutdown, call disconnectMongo() (e.g. on SIGTERM / SIGINT) to close connections cleanly.

This keeps connection logic clean and consistent across multiple services.