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

@dev-nat/mongoose

v1.0.0

Published

A lightweight utility for managing Mongoose connections with caching and retries.

Readme

@dev-nat/mongoose

A simple Mongoose utility for initializing database connections.

Features

  • Cached Mongoose connections (global singleton)
  • Retry mechanism with configurable delay
  • Supports multiple MongoDB connections
  • Works well in serverless environments

Install

Install using the following commands:

npm install @dev-nat/mongoose
yarn add @dev-nat/mongoose
pnpm add @dev-nat/mongoose
bun add @dev-nat/mongoose

Basic Usage

Initialize a Mongoose connection:

// your-db-connection-file.ts
import { createMongooseConnection } from '@dev-nat/mongoose';

export const db = await createMongooseConnection({ uri: 'mongodb://localhost:27017/your-db' });

Initialize Mongoose models:

// your-db-models-file.ts
import { Types, Schema, type Model } from 'mongoose';
import { db } from '@your-db-connection-file';

export interface IPerson {
  _id: Types.Object.Id;
  name: string;
  birthDate: Date;
  createdAt: Date;
  updatedAt: Date;
}

const personSchema = new Schema<IPerson>(
  {
    name: { type: String, required: true },
    birthDate: { type: Date, required: true },
  },
  {
    timestamps: true,
    collection: 'person',
  },
);

// export the model, db.models checks if the model is already initialized
// db.model initializes the model if not yet
export const Person: Model<IPerson> = db.models.person || db.model('person', personSchema);

Initialize multiple connections for multiple MongoDB databases:

// your-db-connection-file.ts
import { createMongooseConnection } from '@dev-nat/mongoose';

export const db1 = await createMongooseConnection({ uri: 'mongodb://localhost:27017/your-db-1' });
export const db2 = await createMongooseConnection({ uri: 'mongodb://localhost:27017/your-db-2' });

Start a session and transaction:

// your-database-service-logic.ts
import { db } from '@your-db-connection-file';
import { Person } from '@your-db-models-file';

export async function updateName(personId: string, newName: string) {
  // no need to call mongoose.connect()

  const session = await db.startSession();

  try {
    // do not forget to start a transaction
    // requires replicaSet to be enabled in MongoDB configuration
    session.startTransaction();

    // perform the operation
    await Person.findByIdAndUpdate(personId, { $set: { name: newName } });

    // save changes
    await session.commitTransaction();
    await session.endSession();
  } catch (err) {
    console.error(err);
    // do not save changes when there is error
    await session.abortTransaction();
    await session.endSession();
  }
}

Better Auth integration

To integrate with better-auth, get the MongoClient like the following:

import { betterAuth } from 'better-auth';
import { mongodbAdapter } from 'better-auth/adapters/mongodb';

// you can rename the import to avoid confusion
import { db as connection } from '@your-db-connection-file';

// get the MongoClient
const client = connection.getClient();
const db = client.db();

export const auth = betterAuth({
  database: mongodbAdapter(db, { client }),
  // the rest of your bettter-auth config
});