UNPKG

@onesy/mongo

Version:

Utils for easier using of mongodb library.

30 lines (29 loc) 1.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OnesyMongo = void 0; const encode_1 = __importDefault(require("@onesy/utils/encode")); const getObjectValue_1 = __importDefault(require("@onesy/utils/getObjectValue")); const Mongo_1 = __importDefault(require("./Mongo")); class OnesyMongo { static get mongo() { return this.Mongo_; } static createPaginator(object, properties, sort = {}, type = 'next') { const value = {}; for (const property of properties) { const value_ = (0, getObjectValue_1.default)(object, property); if (value_ !== undefined) { const sortValue = sort[property]; let operator = type === 'next' ? '$gt' : '$lt'; if (['dsc', 'descending', -1].includes(sortValue)) operator = type === 'next' ? '$lt' : '$gt'; value[property] = { [operator]: value_ }; } } return (0, encode_1.default)(value); } } exports.OnesyMongo = OnesyMongo; OnesyMongo.Mongo_ = new Mongo_1.default(); exports.default = OnesyMongo;