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

pretty-mongo

v0.1.7

Published

A set of helper tools to test mongo-based projects

Downloads

8

Readme

Get up and running with MongoDB in a few lines of code!

Pretty Mongo offers a set of handy methods that abstract away many of the complexities of using MongoDB in your project. Written in Typescript.

The why

The Native MongoDB driver is awesome! But in the era of serverless and microservices, we find ourselves having to constantly write boilerplate code to get up and running.

This library allows you to easily establish one or more database connections, and will also provide an abstraction layer that works really well with Typescript and gives you a set of nicely-named methods that'll cover the use cases of the majority of projects out there.

Installation

NPM Info

Build Status

Example usage

import { initializeMongoDB, getDb, MongoCollectionMethods } from 'pretty-mongo'
const run = async () => {
  const uri = `mongodb://localhost/myApp`
  /*
   * We set the name here, allowing us to later create different connections
   * and give them different labels to fetch them.
   */
  const connectionName = 'myMainDbConnection'
  await initializeMongoDB({ uri, connectionName })

  // Now we could easily access that connection from a different module synchronously.
  const myMainDb = getDb({ connectionName })

  // This will return the native MongoDB `Collection` object.
  const userCollection = myMainDb.collection('Users')

  /*
   * The library also offers a set of methods that will make building your
   * app much easier. And it works really well with Typescript! By defining
   * your Model here, you'll be getting typed objects when for example scanning the DB.
   */
  interface UserDbModel {
    _id: ObjectId
    firstName: string
    lastName: string
    createdAt: Date
    balance: number
    isAdmin?: boolean
  }
  const usersCollectionMethods = new MongoCollectionMethods<UserDbModel>(userCollection)

  // When creating a new object, it will give you an autocomplete feature in your IDE.
  const created = await usersCollectionMethods.createOne({
    _id: new ObjectId(), // it will be created automatically if you do not include it
    firstName: 'Roger',
    lastName: 'Smith',
    balance: 0,
    createdAt: new Date(),
  })

  /*
   * And typescript will nag you if you tried to pass a field
   * that does not exist or provided the wrong type for a field
   */
  const created = await usersCollectionMethods.createOne({
    _id: new ObjectId(), // it will be created automatically if you do not include it
    lastName: 'Smith',
    balance: 0,
    createdAt: new Date(),
  }) // ts error, missing required field

  const created = await usersCollectionMethods.createOne({
    _id: new ObjectId(), // it will be created automatically if you do not include it
    firstName: 2,
    lastName: 'Smith',
    balance: 0,
    createdAt: new Date(),
  }) // ts error, firstName must be a string

  // It also gives you a set of new methods like
  const totalUserCount = await usersCollectionMethods.getTotalCount()
}

run()

Using multiple database connections

const dbConnections = [
  {
    uri: 'mongodb://localhost/myOlapDB',
    connectionName: 'OLAP_DB',
  },
  {
    uri: 'mongodb://localhost/myOltpDB',
    connectionName: 'OLTP_DB',
  },
]
await Promise.all(dbConnections.map(initializeMongoDB))

// Now in your modules get a db by its name
const myOlapDb = getDb({ connectionName: 'OLAP_DB' })

// Will throw if you tried to get a db that you did not initialize
const myNonExistingDb = getDb({ connectionName: 'SANTA' }) // throws: Database SANTA not yet initialized!

Full API documentation here

Author

Ehab Khaireldin Feel free to get in touch, raise issues or submit feature requests!

License

This project is licensed under the MIT License