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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@denis_bruns/nosql-mongodb

v0.1.2

Published

> **A MongoDB service for clean architecture projects, featuring filter expressions, pagination, and safe validations.**

Readme

@denis_bruns/nosql-mongodb

A MongoDB service for clean architecture projects, featuring filter expressions, pagination, and safe validations.

NPM Version TypeScript License: MIT GitHub


Overview

@denis_bruns/nosql-mongodb provides a MongoDB-specific data service based on clean architecture principles. It extends @denis_bruns/database-core to offer:

  • Filter expression construction (MongoExpressionBuilder), converting filters to MongoDB queries
  • Offset-based pagination and in-memory transformations
  • Type-safe mapping of _id to id for convenience
  • Validation utilities to mitigate NoSQL injection attempts
  • Seamless integration with MongoDB’s native Collection API

This library aims to simplify the boilerplate involved in typical CRUD or query operations on MongoDB while keeping your business logic clean and testable.


Key Features

  1. MongoDB-Specific Expression Builder

    • Converts common filter queries (IFilterQuery) into a structured MongoDB conditions object.
    • Supports operators like <, <=, >, >=, =, !=, in, not in, like, and not like.
  2. Pagination & Sorting

    • Applies limit, skip (offset), and sort automatically based on your query.
    • Offers page-based and offset-based pagination in one approach.
  3. Type-Safe Results

    • Converts Mongo’s _id to a string id, enabling a more consistent domain model.
    • Allows further overrides of processResults for custom transformations if desired.
  4. Built-in Validation

    • Ensures safe field names and values (validateValue) to help guard against potential injection patterns.
    • Checks pagination parameters (validatePagination) to confirm integer inputs.
  5. Extensible Architecture

    • Extends the BaseDatabaseService so you can override or customize query building, error handling, or result processing.

Installation

With npm:

npm install @denis_bruns/nosql-mongodb

Or with yarn:

yarn add @denis_bruns/nosql-mongodb

You’ll also need MongoDB types and driver:

npm install mongodb

Basic Usage

Below is a simple usage example. In a real-world application, you might integrate this into a domain-specific repository or service layer.

import { MongoClient } from "mongodb";
import { fetchWithFiltersAndPaginationMongoDb, MongoDBService } from "@denis_bruns/nosql-mongodb";
import { IGenericFilterQuery } from "@denis_bruns/core";

interface User {
  id: string;
  name: string;
  email: string;
}

async function example() {
  // 1) Connect to MongoDB
  const client = new MongoClient("mongodb://localhost:27017");
  await client.connect();
  const collection = client.db("my-database").collection("users");

  // 2) Build a filter query
  const query: IGenericFilterQuery = {
    filters: [
      { field: "email", operator: "=", value: "[email protected]" }
    ],
    pagination: { page: 1, limit: 5, sortBy: "name" }
  };

  // 3) Option A: Direct Helper Function
  const directResult = await fetchWithFiltersAndPaginationMongoDb<User>(
    "users", // tableName
    query,
    collection
  );
  console.log("Direct Helper:", directResult.data);

  // 4) Option B: MongoDBService instance
  const service = new MongoDBService("users");
  const serviceResult = await service.fetchWithFiltersAndPagination<User>(query, collection);
  console.log("Service Class:", serviceResult.data);

  client.close();
}

example().catch((err) => console.error("Mongo Example Error:", err));

In this snippet:

  • fetchWithFiltersAndPaginationMongoDb is a quick helper if you just need a one-off query.
  • MongoDBService allows for deeper customization or extension in your codebase.

Core Concepts

  1. Filter Expressions
    Each filter has field, operator, and value. Operators like "in", "not in", "like", and "not like" are mapped to Mongo’s $in, $nin, $regex, and $not respectively.

    filters: [
      { field: "status", operator: "=", value: "active" },
      { field: "name", operator: "like", value: "john" }
    ];
  2. Pagination

    • page, limit, offset are all supported.
    • sortBy and sortDirection let you sort on a specific field in ascending or descending order.
  3. Validation

    • validateValue checks for suspicious patterns in strings or objects (to reduce injection attacks).
    • validatePagination ensures page, limit, and offset are valid integers.
  4. ID Mapping

    • If your filters or results use "id", it’s automatically mapped to or from _id so you can keep a consistent domain model.

Related Packages

  • @denis_bruns/core
    NPM
    GitHub
    Contains the fundamental interfaces and types used in this library (e.g., IFilterQuery, IGenericFilterQuery, etc.).

  • @denis_bruns/database-core
    NPM
    GitHub
    The abstract service this library extends to handle common database logic, such as error handling and pagination utilities.


Contributing

Contributions, bug reports, and feature requests are welcome! Please feel free to open an issue or submit a pull request on GitHub.


License

This project is MIT licensed.