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 🙏

© 2025 – Pkg Stats / Ryan Hefner

monguard

v0.13.0

Published

A lightweight, zero-boilerplate toolkit designed to enhance MongoDB models with production-ready features

Downloads

44

Readme

🛡️ monguard – The Guardian of Your MongoDB Records

CI codecov NPM Version npm downloads

Note: The API is subject to change, please follow the release note to migration document, please report any issues or suggestions.

monguard is a lightweight, zero-boilerplate toolkit designed to enhance MongoDB models with production-ready features:

  • 🗑️ Soft Delete — Mark records as deleted without removing them from the database
  • ⏱️ Auto Timestamps — Automatically manage createdAt and updatedAt fields
  • 🕵️ Audit Logging — Track every create, update, and delete action with detailed metadata
  • 🎯 Delta Mode Auditing — Reduce audit log storage by 70-90% with smart field-level change tracking
  • 🚀 Transaction-Aware Auditing — In-transaction or outbox patterns for different consistency needs
  • 🧠 TypeScript First — Fully typed for safety and great DX
  • ⚙️ Plug-and-Play — Minimal setup, maximum control

✨ Why monguard?

In real-world apps, deleting data is rarely the end of the story. Whether it's rollback, audit compliance, or just better traceability — monguard has your back.

With just a single call, you can guard your collections against accidental data loss while keeping every change accountable.

📦 Installation

npm install monguard

🔐 Use-case Highlights

  • CRM systems with user-deletable data
  • E-commerce with order history tracking
  • Admin dashboards needing full audit trail
  • Financial systems requiring strict audit compliance
  • High-throughput applications with eventual consistency needs
  • Any app where "delete" doesn't really mean delete 😉

Guard your data. Track the truth. Sleep better. — with monguard 🛡️

Installation

npm install monguard
# or
yarn add monguard
# or
pnpm add monguard

Important: You must install a MongoDB driver separately, as Monguard has zero runtime dependencies:

npm install mongodb
# or any MongoDB-compatible driver

Quick Start

import { MongoClient } from 'mongodb';
import { MonguardCollection, MonguardAuditLogger } from 'monguard';

// Define your document interface
interface User {
  _id?: any;
  name: string;
  email: string;
  age?: number;
  createdAt?: Date;
  updatedAt?: Date;
  deletedAt?: Date;
  createdBy?: ObjectId;
  updatedBy?: ObjectId;
  deletedBy?: ObjectId;
}

// Connect to MongoDB
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('myapp');

// Create audit logger with delta mode for 70-90% storage reduction
const auditLogger = new MonguardAuditLogger(db, 'audit_logs', {
  storageMode: 'delta',        // Enable delta mode
  maxDepth: 3,                 // Track nested changes up to 3 levels
  arrayDiffMaxSize: 20,        // Smart array handling
});

// Create a Monguard collection
const users = new MonguardCollection<User>(db, 'users', {
  auditLogger,
  concurrency: { transactionsEnabled: true },
  auditControl: {
    mode: 'inTransaction',     // Strong audit consistency
    failOnError: false         // Graceful error handling
  }
});

// Create a user with audit logging
try {
  const user = await users.create({
    name: 'John Doe',
    email: '[email protected]'
  }, {
    userContext: { userId: 'admin-123' }
  });
  
  console.log('User created:', user);
} catch (error) {
  console.error('Failed to create user:', error.message);
}

Core Features

🔍 Audit Logging

  • Automatic tracking of all create, update, and delete operations
  • Delta mode auditing with 70-90% storage reduction via field-level change tracking
  • Transaction-aware audit control with in-transaction and outbox modes
  • Flexible error handling with fail-fast or resilient strategies
  • Customizable audit collection names and logger interfaces
  • Rich metadata including before/after states and field changes
  • Reference ID validation with configurable error handling
  • Support for custom logging services (Winston, Pino, etc.)

🗑️ Soft Deletes

  • Safe deletion that preserves data integrity
  • Option for hard deletes when needed
  • Automatic filtering of soft-deleted documents

👤 User Tracking

  • Track who created, updated, or deleted each document
  • Flexible user ID types (string, ObjectId, custom objects)
  • Automatic timestamp management

Concurrency Control

  • Transaction-based strategy for MongoDB replica sets
  • Optimistic locking strategy for standalone/Cosmos DB
  • Automatic fallback handling

🎯 Type Safety

  • Full TypeScript support with strict typing
  • MongoDB-compatible type definitions
  • Zero runtime dependencies on MongoDB driver

Documentation

For detailed documentation, including configuration options, API reference, and best practices, please refer to the Monguard User Manual

License

MIT License © 2025 Created by @thaitype