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

mongodb-kit

v0.0.2

Published

mongodb

Readme

mongodb-kit

A lightweight, high-performance MongoDB framework for Node.js and TypeScript.

mongodb-kit provides a repository abstraction, metadata-driven object mapping, search framework, optimistic locking, batch operations, streaming utilities, and health checks on top of the official MongoDB driver.

It is designed for enterprise applications while remaining simple enough to use directly in small services.

Examples:


Features

  • Lightweight wrapper around the MongoDB driver
  • Repository pattern
  • Metadata-driven document mapping
  • Generic CRUD repository
  • Search repository with pagination and sorting
  • Dynamic query builder
  • Optimistic locking
  • Batch insert/update utilities
  • Bulk operations
  • Field projection
  • Import/Export helpers
  • Health checker for Kubernetes
  • TypeScript-first
  • No decorators required
  • Minimal runtime overhead

Installation

npm install mongodb-kit

or

yarn add mongodb-kit

Quick Start

Define a model

export interface User {
    id: string;
    name: string;
    email: string;
    age: number;
}

Create a repository

import { MongoClient } from "mongodb";
import { Repository } from "mongo-repository";

const client = await MongoClient.connect(connectionString);

const database = client.db("sample");

const repository = new Repository<User>(
    database,
    "users"
);

Create

await repository.create({
    id: "u01",
    name: "John",
    email: "[email protected]",
    age: 30
});

Find by id

const user = await repository.load("u01");

Update

await repository.update({
    id: "u01",
    name: "John Smith",
    email: "[email protected]",
    age: 31
});

Delete

await repository.delete("u01");

Searching

Define a search model.

export interface UserFilter {
    name?: string;
    age?: number;
    page?: number;
    size?: number;
}

Search

const result = await repository.search({
    name: "John",
    page: 1,
    size: 20
});

Custom Query Builder

The repository allows replacing the default query generation.

const repository = new Repository<User, string, UserFilter>(
    database,
    "users",
    undefined,
    buildUserQuery
);

Example

function buildUserQuery(filter: UserFilter) {
    const query: any = {};

    if (filter.name) {
        query.name = {
            $regex: filter.name,
            $options: "i"
        };
    }

    if (filter.age) {
        query.age = filter.age;
    }

    return query;
}

Pagination

The library supports server-side pagination.

const users = await repository.search({
    page: 2,
    size: 50
});

Sorting

Sort behavior can be customized.

function buildSort(sort?: string) {
    if (!sort) {
        return { name: 1 };
    }

    if (sort === "-createdAt") {
        return { createdAt: -1 };
    }

    return { [sort]: 1 };
}

Metadata Mapping

Application models do not have to match MongoDB documents.

MongoDB

{
    "first_name": "John"
}

Application

{
    firstName: "John"
}

Metadata automatically maps between them.


BSON Conversion

The repository supports conversion between application objects and MongoDB BSON.

Example:

new Repository(
    database,
    "users",
    metadata,
    buildQuery,
    toBson,
    fromBson
);

This is useful for:

  • Value Objects
  • UUID
  • DateOnly
  • Decimal
  • Money
  • Custom domain types

Search Repository

For read-only services, use SearchRepository.

const repository = new SearchRepository<User>(
    database,
    "users"
);

Supported operations include:

  • search
  • load
  • exists
  • count

CRUD Repository

For full CRUD operations:

const repository = new Repository<User>(
    database,
    "users"
);

Supported operations:

  • insert
  • update
  • patch
  • delete
  • load
  • search
  • exists
  • count

Batch Operations

The library includes helpers for bulk operations.

Examples include:

  • insert many
  • update many
  • delete many

These operations reduce round trips and improve performance.


Audit Logging

Audit logging can be integrated through AuditLogWriter.

Typical audit information includes:

  • user
  • action
  • timestamp
  • entity
  • old value
  • new value

Architecture

      Application
            │
            ▼
Repository / SearchRepository
            │
            ▼
MongoWriter / MongoLoader
            │
            ▼
      Mongo Helpers
            │
            ▼
      MongoDB Driver

Why mongodb-kit?

Compared with using the MongoDB driver directly, this library provides:

  • Generic repositories
  • Reusable search logic
  • Pagination
  • Sorting
  • Metadata mapping
  • BSON conversion
  • Reduced boilerplate
  • Cleaner architecture

Compared with ODM frameworks:

  • No decorators
  • No runtime reflection
  • No Active Record
  • Better separation of concerns
  • Closer to native MongoDB

Suitable For

  • Clean Architecture
  • Domain Driven Design (DDD)
  • Hexagonal Architecture
  • Microservices
  • Enterprise applications
  • REST APIs
  • GraphQL APIs
  • Backend services

Requirements

  • Node.js 18+
  • TypeScript 5+
  • MongoDB 5+

License

MIT