mongosuper
Version:
mongosuper is a superset of mongoose. It is a wrapper around mongoose. It manage your mongoose connection and keep it alive always. It also provide you some extra features like CRUD operations, etc.
81 lines • 4.42 kB
JavaScript
/* This File is used to Find Data From Database. */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "../Validator/FindValidator"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Find = void 0;
const FindValidator_1 = __importDefault(require("../Validator/FindValidator")); // Import the findValidator function from the FindValidator.js file.
/**
* The Find function takes in a filter, model, and limit, validates the filter, and returns the result
* of finding documents in the model based on the filter and limit.
* @param Filter - The filter is an array of objects that specifies the conditions that the documents
* must meet in order to be returned by the find function.
* @param model - The model parameter is a reference to a Mongoose model that represents a MongoDB
* collection. It is used to perform database operations such as finding documents that match a certain
* criteria.
* @param limit - The limit parameter is used to specify the maximum number of documents to be returned
* by the find function. It limits the number of documents that will be returned in the result set.
* @returns The Find function is returning either an empty array if the Filter is not valid, or an
* array of documents that match the Filter and are limited by the specified limit.
*/
// Main Find Function
function Find(type, Filter, model, limit, skip) {
return __awaiter(this, void 0, void 0, function* () {
try {
const validate = yield (0, FindValidator_1.default)(Filter); // Validate the Filter
switch (validate) {
case false:
return [];
case true:
switch (Filter.length) {
case 0:
const result = yield model
.find({})
.limit(limit)
.skip(skip); // find the document
return result; // return the result
default:
if (type === "OR") {
const orResult = yield model
.find({ $or: Filter })
.limit(limit)
.skip(skip); // find the document
return orResult; // return the result
}
else if (type === "AND") {
const andResult = yield model
.find({ $and: Filter })
.limit(limit)
.skip(skip); // find the document
return andResult; // return the result
}
}
}
}
catch (err) {
console.log(err);
} // catch any errors
});
} // Export the Find function.
exports.Find = Find;
});
//# sourceMappingURL=Read.js.map