1.2.0 • Published 6 months ago

mongo-query-utils v1.2.0

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

šŸ”’ mongo-query-utils

A simple mongo query builder for node.js for Node/Express.js.

šŸš€ Features

āœ… Create Documents – Create a new document and save to database
āœ… Fetch Documents – Collect all or necessary documents from the database
āœ… Update Documents – Update all or specific document(s) from the database
āœ… Delete Documents – Remove documents from the database
āœ… Join Documents – Join documents from different tables


šŸ“¦ Installation

npm install mongo-query-utils

šŸš€ Usage

1ļøāƒ£ Import the Module

const {
  fetchAll,
  fetchOne,
  generate,
  fetchById,
  patchOneAndUpdate,
  patchByIdAndUpdate,
  patchOne,
  patchMany,
  replaceOne,
  removeOne,
  removeMany,
  summarize,
} = require("mongo-query-utils");

Create a New Document

const Table = require("your-schema");
const document = {
  first_name: "Sample",
  last_name: "Test",
};
const newDoc = await generate(document, Table);
console.log(Table);

šŸ” Fetch a Single Document

Fetch a single document based on the provided query.

const query = { email: "user@example.com" };
const user = await fetchOne(query, User);
console.log(user); // { _id: 123, email: "user@example.com", name: "John Doe" }

šŸ†” Fetch Document by ID

Retrieve a document using its unique _id.

const id = "60d5f9e8f1c2b4a3d8f5e7c6";
const document = await fetchById(id, Table);
console.log(document);

āœļø Update Documents

Update the first document that matches the query.

Update a Single Document

const query = { email: "user@example.com" };
const update = { name: "Updated Name" };
const updatedDoc = await patchOneAndUpdate(query, update, User);
console.log(updatedDoc);

Update a Document by ID

const id = "60d5f9e8f1c2b4a3d8f5e7c6";
const update = { status: "Active" };
const updatedDoc = await patchByIdAndUpdate(id, update, Table);
console.log(updatedDoc);

Update Multiple Documents

const query = { status: "Pending" };
const update = { status: "Completed" };
const updatedDocs = await patchMany(query, update, Table);
console.log(updatedDocs);

šŸ”„ Replace a Document

Replace an entire document with new data.

const query = { email: "user@example.com" };
const newDocument = { email: "user@example.com", name: "New Name" };
const replacedDoc = await replaceOne(query, newDocument, User);
console.log(replacedDoc);

šŸ—‘ļø Delete Documents

Delete a Single Document

const query = { email: "user@example.com" };
const deletedDoc = await removeOne(query, User);
console.log(deletedDoc);

Delete Multiple Documents

const query = { status: "Inactive" };
const deletedDocs = await removeMany(query, User);
console.log(deletedDocs);

šŸ”— Join Collections

Join documents from two collections using $lookup.

const pipeline = [
  {
    $match: {_id: 'sample_id'}
    $lookup: {
      from: "orders",        // Collection to join
      localField: "_id",      // Field in primary collection
      foreignField: "userId", // Field in foreign collection
      as: "userOrders"        // Output array
    }
  }
];
const usersWithOrders = await summarize(pipeline, User);
console.log(usersWithOrders);

šŸ› ļø Options

Each function accepts an optional third parameter for customization:

const options = { sort: { createdAt: -1 }, limit: 10 };
const latestUsers = await fetchAll({}, User, options);
console.log(latestUsers);

šŸ“œ License

MIT License. See LICENSE for details.

šŸ“Œ Author: Ryan Charles Alcaraz (github)


1.2.0

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago