UNPKG

outers

Version:

outers - a all in one package for your day to day use

68 lines 3.71 kB
"use strict"; /* 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 }; }; 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