1.3.2 • Published 5 months ago

afridho-mongodb v1.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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: "alice@example.com" });

    // 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: "bob@example.com" });
  • insertMany(data): Inserts multiple documents into the collection.

    await userCollection.insertMany([
        { name: "Charlie", email: "charlie@example.com" },
        { name: "David", email: "david@example.com" },
    ]);
  • 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.
1.3.2

5 months ago

1.3.1

5 months ago

1.2.0

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.1.0

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago