JSPM

@kommix/mongoose-activity

1.2.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 14
  • Score
    100M100P100Q85205F
  • License MIT

Production-ready Mongoose plugin for complete activity tracking with deletion support, performance optimization, and enterprise features

Package Exports

  • @kommix/mongoose-activity

Readme

Kommix Logo

@kommix/mongoose-activity

Production-grade activity logging for Mongoose πŸ“ β†’ CRUD, deletions, custom events, feeds.

npm version npm downloads coverage license

A modern, production-ready Mongoose plugin that takes care of activity logging so you don't have to. Create, update, delete β€” everything gets tracked automatically. No more DIY audit tables! ✨

πŸ” Why Not Other Plugins?

Most existing mongoose activity/audit plugins fall short for production use:

πŸ“Š Feature Comparison

Capability Other Plugins @kommix/mongoose-activity
Auto CRUD Hooks ❌ Often manual loggingΒΉ βœ… Full lifecycle hooks + custom events
Deletion Tracking ❌ Rarely explicit; soft-delete onlyΒ² βœ… Hard deletes + field capture pre-delete
Bulk Operations ❌ deleteMany/updateMany gapsΒ³ βœ… Bulk threshold & optimization
Retention/TTL ❌ Generally missing βœ… Auto-cleanup via retentionDays
Performance ❌ Sync-only, no batching βœ… Async logging, event system
TypeScript ❌ Mixed JS/TS support βœ… TypeScript-first
Maintenance ❌ Many stale (2016-2020)⁴ βœ… Active development

πŸ” Specific Gaps We Found:

  • mongoose-activitylog: Manual builder pattern, no auto-hooks, last release May 2020ΒΉ
  • mongoose-user-history-plugin: Missing bulk ops, TTL, performance tuningΒ³
  • mf-mongoose-audittrail: Audit fields only, no central Activity collectionΒ²
  • mongoose-activitylogs: Basic append-style, last published August 2016⁴
  • @hilarion/mongoose-activity-logger: Unmaintained since October 2019⁴

ΒΉmongoose-activitylog Β²mf-mongoose-audittrail Β³mongoose-user-history-plugin ⁴Many plugins last updated between 2016–2020 (see npm/GitHub links)

🎯 What Makes This Different?

  • πŸ”„ Production-Ready Design - Built for enterprise workloads with async logging, TTL cleanup, and smart indexing
  • πŸ—‘οΈ Complete Deletion Coverage - Only plugin with full deleteOne/deleteMany/findOneAndDelete tracking + field capture
  • ⚑ Performance Optimized - Bulk operation thresholds, async logging, configurable batching
  • πŸš€ Modern Stack - TypeScript-first, zero dependencies, Node 18+ support
  • πŸ”§ Battle-Tested - 91.93% test coverage, comprehensive performance benchmarks

πŸ’‘ Built because we got tired of half-maintained plugins. Now you don't have to.

πŸ“š Documentation

πŸš€ Installation

npm install @kommix/mongoose-activity

⚑ Quick Start

πŸ’« 5-Second Demo

// One-liner setup
userSchema.plugin(activityPlugin, { trackedFields: ['name', 'email'] });

await User.create({ name: "Jane" }); // β†’ activity logged automatically!

That's it! Activities now show up automatically. You focus on features, we handle the logs.

What you get:

{
  "type": "users_created",
  "entity": { "type": "users", "id": "652a..." },
  "changes": { "name": "Jane", "email": "jane@example.com" },
  "userId": "auto-detected",
  "timestamp": "2025-09-27T12:34:56Z"
}

1. Plugin Setup - Auto-Track Document Changes

import mongoose from 'mongoose';
import { activityPlugin } from '@kommix/mongoose-activity';

// Your existing schema
const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  status: String
});

// Add the plugin - specify which fields to track
userSchema.plugin(activityPlugin, {
  trackedFields: ['name', 'email', 'status'], // Fields to track
  collectionName: 'users' // Entity type for activities
});

const User = mongoose.model('User', userSchema);

// Now every save/update/delete automatically logs activities!
const user = await User.create({
  name: 'John Doe',
  email: 'john@example.com'
});
// βœ… Automatically logs: { type: 'users_created', entity: { type: 'users', id: user._id }, ... }

user.name = 'Jane Doe';
await user.save();
// βœ… Automatically logs: { type: 'document_updated', changes: { name: { from: 'John Doe', to: 'Jane Doe' } }, ... }

await User.deleteOne({ _id: user._id });
// βœ… Automatically logs: { type: 'users_deleted', meta: { deletedFields: { name: 'Jane Doe', email: 'john@example.com' } }, ... }

2. Manual Logging - Custom Business Events

import { logActivity } from '@kommix/mongoose-activity';

// Log custom business events
await logActivity({
  userId: customerId,
  entity: { type: 'order', id: orderId },
  type: 'order_shipped',
  meta: {
    trackingNumber: 'ABC123',
    carrier: 'FedEx',
    estimatedDelivery: '2024-01-15'
  }
});

3. Query Activity Feeds

import { getActivityFeed, getEntityActivity } from '@kommix/mongoose-activity';

// Get user's activity feed
const feed = await getActivityFeed(userId, {
  limit: 20,
  entityType: 'order' // Optional filters
});

// Get complete history for a specific entity
const orderHistory = await getEntityActivity('order', orderId);

4. Express/Koa Middleware - Auto-Capture Context

import { activityContextMiddleware } from '@kommix/mongoose-activity';

// Basic usage (works with any framework)
app.use(activityContextMiddleware({
  extractUserId: (req) => req.user?.id,
  extractRequestId: (req) => req.headers?.['x-request-id']
}));

// Now all activities automatically include request context β€” magic! ✨

🎯 Enhanced TypeScript Support

import { Request, Response } from 'express';
import { Context } from 'koa';

// Express with full type safety and IntelliSense
app.use(activityContextMiddleware<Request, Response>({
  extractUserId: (req) => req.user?.id,  // Full autocomplete
  extractRequestId: (req) => req.headers['x-request-id'],
}));

// Koa with type safety
app.use(koaActivityContextMiddleware<Context>({
  extractUserId: (ctx) => ctx.state.user?.id,
  extractIp: (ctx) => ctx.ip,
}));

// Custom framework integration
interface MyCustomRequest {
  authenticatedUser: { userId: string };
  customHeaders: Record<string, string>;
}

app.use(activityContextMiddleware<MyCustomRequest>({
  extractUserId: (req) => req.authenticatedUser.userId,
  extractRequestId: (req) => req.customHeaders.requestId,
}));

✨ Key Features

πŸ—‘οΈ Deletion Tracking

userSchema.plugin(activityPlugin, {
  trackDeletions: true,          // Enable deletion tracking
  deletionFields: ['name', 'email'], // Fields to capture before deletion
  bulkDeleteThreshold: 100       // Auto-optimize for bulk operations
});

πŸ”„ Event System

import { activityEvents } from '@kommix/mongoose-activity';

// React to activities in real-time β€” hook into your business logic
activityEvents.on('activity:logged', (activity) => {
  if (activity.type === 'order_shipped') {
    notificationService.send(activity.userId, 'Your order has shipped!');
  }
});

βš™οΈ Global Configuration

import { activityConfig } from '@kommix/mongoose-activity';

activityConfig.configure({
  asyncLogging: true,     // Fire-and-forget for performance
  retentionDays: 90,      // Auto-cleanup with TTL
  throwOnError: false     // Silent error handling
});

🧩 Compatibility

  • Node.js: 18+ (matches engines field in package.json)
  • MongoDB: 4.0+ (for TTL and indexes)
  • Mongoose: 6.0+ or 7.0+ or 8.0+
  • TypeScript: 4.0+ (full type support with enhanced middleware IntelliSense)
  • Frameworks: Express, Koa, or any compatible middleware framework

πŸ§ͺ Development

# Install dependencies
npm install

# Run tests (91.93% coverage)
npm test

# Build (CJS + ESM)
npm run build

# Development mode
npm run dev

🀝 Contributing

We'd love your help to make this even better! PRs, issues, and feature ideas are always welcome πŸš€

Before submitting:

  1. Run npm run lint and npm run format
  2. Add tests for new features
  3. Update documentation if needed

πŸ‘ Looking to build something cool? Reach out β€” we're friendly and love crazy ideas!

πŸ“„ License

MIT Β© Kommix


πŸ“¦ npm Β· πŸ’» GitHub Β· πŸ“– Full Documentation