1.1.3 • Published 6 months ago

mongoclienteasywrapper v1.1.3

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

MongoClientEasyWrapper

A simple MongoDB wrapper library for common database operations using the mongodb Node.js driver.

Installation

Install the library via npm:

npm install mongoclienteasywrapper

Configuration

Set up your MongoDB connection URI and database name in your code:

const MongoWraper = require("mongoclienteasywrapper")(
  "mongodb://localhost:27017/yourDatabaseName"
);

Main functions

Inserts a document into the specified collection:

SavetoMongo(objectToSave, collection, databaseName);

Parameters: objectToSave: Document to insert. collection: Name of the collection. databaseName: (Optional) Name of the database. If not provided, uses the default database. Returns: Promise resolving to the result of the insertion.

Finds a single document by its _id from the specified collection:

FindIDOne(Id, collection, databaseName);

Parameters: Id: ObjectId of the document to find. collection: Name of the collection. databaseName: (Optional) Name of the database. If not provided, uses the default database. Returns: Promise resolving to the found document.

Deletes a document by its _id from the specified collection:

DeleteMongoby_id(_id, collection, databaseName);

Parameters: _id: ObjectId of the document to delete. collection: Name of the collection. databaseName: (Optional) Name of the database. If not provided, uses the default database. Returns: Promise resolving to the result of the delete operation.

Usage Example

const { ObjectId } = require("mongodb");
const MongoWraper = require("mongoclienteasywrapper")(
  "mongodb://localhost:27017/yourDatabaseName"
);

// Insert a document
const insertDocument = async () => {
  const result = await MongoWraper.SavetoMongo(
    {
      _id: ObjectId("624e09075bda143a913c5d61"),
      name: "Test Document",
      datetime: new Date(),
    },
    "testCollection",
    "testDB"
  );
  console.log("Insert result:", result);
};

// Find a document by ID
const findDocument = async () => {
  const result = await MongoWraper.FindIDOne(
    "624e09075bda143a913c5d61",
    "testCollection",
    "testDB"
  );
  console.log("Find result:", result);
};

// Delete a document by ID
const deleteDocument = async () => {
  const result = await MongoWraper.DeleteMongoby_id(
    "624e09075bda143a913c5d61",
    "testCollection",
    "testDB"
  );
  console.log("Delete result:", result);
};

Running Tests

To run the tests defined in test.js, use:

npm run test

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

1.1.1

8 months ago

1.1.3

6 months ago

1.1.2

8 months ago

1.0.29

11 months ago

1.0.28

11 months ago

1.0.27

11 months ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.16

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago