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 🙏

© 2024 – Pkg Stats / Ryan Hefner

afridho-mongodb

v1.2.0

Published

A simple MongoDB client wrapper for easy database operations

Downloads

546

Readme

afridho-mongodb

A simple and easy-to-use MongoDB client wrapper for Node.js applications. This package provides a straightforward interface for performing common database operations such as reading, inserting, updating, and deleting documents in a MongoDB collection.

Table of Contents

Installation

To install the afridho-mongodb package, use npm:

npm install afridho-mongodb

Setup

Create a .env File

Before using the package, create a .env file in your project root with the following variables:

MONGODB_URI=your_mongodb_connection_string
DB_NAME=your_database_name

Usage

Basic Example

Here’s a basic example of how to use the afridho-mongodb package:

const ClientDB = require("afridho-mongodb");

async function main() {
    // Create a client for a specific collection
    const userCollection = new ClientDB("users");

    // Insert a new user
    await userCollection.insert({ name: "Alice", email: "[email protected]" });

    // Read all users
    const users = await userCollection.readAll();
    console.log(users);

    // Update a user
    await userCollection.update({ name: "Alice" }, { age: 25 });

    // Delete a user
    await userCollection.delete({ name: "Alice" });

    // Close the connection
    await userCollection.close();
}

main().catch(console.error);

Methods

The ClientDB class provides the following methods for interacting with your MongoDB collection:

  • connect(): Establishes a connection to the MongoDB database.

  • read(query): Reads a single document from the collection based on the provided query.

    const user = await userCollection.read({ name: "Alice" });
    console.log(user);
  • readAll(): Reads all documents from the collection.

    const users = await userCollection.readAll();
    console.log(users);
  • insert(data): Inserts a new document into the collection.

    await userCollection.insert({ name: "Bob", email: "[email protected]" });
  • insertMany(data): Inserts multiple documents into the collection.

    await userCollection.insertMany([
        { name: "Charlie", email: "[email protected]" },
        { name: "David", email: "[email protected]" },
    ]);
  • update(query, data): Updates a document in the collection based on the provided query.

    await userCollection.update({ name: "Bob" }, { age: 30 });
  • delete(query): Deletes a single document from the collection based on the provided query.

    await userCollection.delete({ name: "Bob" });
  • deleteMany(query): Deletes multiple documents from the collection based on the provided query.

    await userCollection.deleteMany({ age: { $gt: 30 } });
  • find(query): Finds multiple documents in the collection based on the provided query.

    const results = await userCollection.find({ age: { $lt: 30 } });
    console.log(results);
  • getStorageStats(): Gets the storage statistics for the collection.

    const stats = await userCollection.getStorageStats();
    console.log(stats);
  • close(): Closes the MongoDB connection.

    await userCollection.close();

Requirements

  • Node.js 16+
  • MongoDB
  • dotenv

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

afridho
Github

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue. To contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature/YourFeature).
  6. Open a pull request.

Acknowledgments

  • MongoDB for the database.
  • dotenv for environment variable management.