2025-07-09 01:04:18 +02:00

23 lines
675 B
TypeScript

import { Request, Response, NextFunction } from 'express';
import { Model } from 'mongoose';
export function MongoQuery(model: Model<any>) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (req: Request, res: Response, next: NextFunction) {
try {
const documents = await model.find({ ...req.body });
req.mongoQuery = documents;
} catch (error) {
logging.error(error);
return res.status(400).json(error);
}
return originalMethod.call(this, req, res, next);
};
return descriptor;
};
}