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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoconnect-ts

v1.0.4

Published

MongoDb handlers.

Readme

MongoDB Utility Library (mongoconnect-ts)

A lightweight TypeScript utility for MongoDB connections, optimized for use with MongoDB Atlas and self-hosted MongoDB. Designed for simplicity, reusability, and schema validation.


Features

  • Reusable MongoDB Connection: Efficient connection handling.
  • TypeScript Support: Fully typed for modern projects.
  • Schema Validation: Easily create collections with JSON schema validation.
  • Batch Data Fetching: Fetch all collections and documents in a structured format.
  • Error Handling: Clear error messages and logging.

Installation

Prerequisites

  • Node.js (v14 or later)
  • MongoDB Atlas or self-hosted MongoDB instance
  • mongodb Node.js driver (v4+)

Install via npm

npm install mongodb

Copy the library code into your project or use as a module.


Usage

1. Import and Instantiate

import Mongo from 'mongoconnect-ts';

const mongo = new Mongo(process.env.MONGO_CSTRING, 'yourDatabaseName');

2. Use the Library

Note: There is currently only this method for retrieval. When I have time i will expand.

Fetch All Collections and Documents

await mongo.client.connect();
const data = await mongo.getAll();
await mongo.client.close();

Create a Collection with Schema Validation

const schema = {
  validator: {
    \$jsonSchema: {
      bsonType: "object",
      required: ["name", "email"],
      properties: {
        name: { bsonType: "string" },
        email: { bsonType: "string" }
      }
    }
  }
};
const newCollection = await mongo.createSchemaCollection('users', schema);

API Reference

Mongo

Constructor

new Mongo(connectionString: string, databaseName?: string | null)
  • connectionString: MongoDB connection URI (e.g., mongodb+srv://user:[email protected]/dbName).
  • databaseName: (Optional) Database name. If omitted, uses the database specified in the connection string.

Methods

  • createSchemaCollection(collection: string, schema: object): Promise<Collection>: Creates a collection with schema validation.
  • getAll(): Promise<AllCollections>: Fetches all collections and documents as a structured object.

Best Practices

  • Reuse the Connection: Connect once and reuse the client for all operations.
  • Environment Variables: Store your connection string in environment variables.
  • Error Handling: Always handle errors with try/catch blocks.
  • Indexes: Ensure your collections are properly indexed for performance.

Troubleshooting

Common Issues

  • Connection Errors: Ensure your IP is whitelisted in MongoDB Atlas and your connection string is correct.
  • Authentication Errors: Double-check your username, password, and database name.
  • Slow Performance: Reuse the connection and avoid connecting/closing for each operation.

Debugging

  • Enable debug logging for the MongoDB driver:
    DEBUG=mongodb node your-app.js

License

MIT