mongoose-aggregation-wrapper
Version:
š TypeScript wrapper for debugging MongoDB/Mongoose aggregation pipelines stage-by-stage. Debug complex aggregations, optimize performance, and understand data flow with detailed execution timing and sample results.
116 lines (110 loc) ⢠4.12 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exampleUsage = exampleUsage;
exports.simpleExample = simpleExample;
// Example usage file showing how to use the Wrapper function
// Note: This assumes you already have a connected Mongoose model
const index_1 = __importDefault(require("./index"));
// Example function showing how to use the Wrapper
// You would call this function with your own connected model
async function exampleUsage(materialModel) {
// Example aggregation pipeline (same as your example)
const pipeline = [
// Sort stage
{ $sort: { createdAt: -1 } },
// Pagination (example)
{ $skip: 0 },
{ $limit: 10 },
// Lookup createdBy
{
$lookup: {
from: 'users', // Use actual collection name
localField: 'createdBy',
foreignField: '_id',
as: 'createdBy'
}
},
{ $unwind: { path: '$createdBy', preserveNullAndEmptyArrays: true } },
// Lookup products
{
$lookup: {
from: 'products', // Use actual collection name
localField: 'products.productId',
foreignField: '_id',
as: 'productDetails'
}
},
// Add product details to each product in products array
{
$addFields: {
products: {
$map: {
input: { $ifNull: ['$products', []] },
as: 'product',
in: {
$mergeObjects: [
'$$product',
{
productDetails: {
$arrayElemAt: [
{
$filter: {
input: '$productDetails',
cond: { $eq: ['$$this._id', '$$product.productId'] }
}
},
0
]
}
}
]
}
}
}
}
},
{ $match: { deleted: false } },
{ $unset: 'productDetails' }
];
console.log('Running aggregation with Wrapper function...\n');
try {
// Use the Wrapper function - it will execute each stage and show results
const results = await (0, index_1.default)(materialModel, pipeline, {
debug: true, // Enable debug mode (step by step execution)
logResults: true, // Log sample results after each stage
allowDiskUse: true // MongoDB option for large datasets
});
console.log('\nš Final Results Summary:');
console.log(`Total documents: ${results.length}`);
return results;
}
catch (error) {
console.error('ā Error in aggregation:', error);
throw error;
}
}
// Simple example for basic usage
async function simpleExample(yourModel) {
const simplePipeline = [
{ $match: { deleted: false } },
{ $sort: { createdAt: -1 } },
{ $limit: 5 }
];
return await (0, index_1.default)(yourModel, simplePipeline);
}
/*
Usage in your application:
import { exampleUsage } from 'mongoose-aggregation-wrapper/dist/example';
import { MaterialModel } from './your-models';
// In your service/controller
const results = await exampleUsage(MaterialModel);
Or directly:
import Wrapper from 'mongoose-aggregation-wrapper';
const results = await Wrapper(YourModel, yourPipeline, {
debug: true,
logResults: true
});
*/