@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
98 lines (97 loc) • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePagination = exports.parseFilterAndOptions = void 0;
const class_validator_1 = require("class-validator");
const object_1 = require("diginext-utils/dist/object");
const lodash_1 = require("lodash");
const services_1 = require("../services");
const mongodb_1 = require("./mongodb");
const parse_request_filter_1 = require("./parse-request-filter");
function parseFilterAndOptions(req) {
const { download = false, skip, limit = 0, page = 1, size: page_size = 0, populate, select, status, sort, // @example: -updatedAt,-createdAt
order, // @example: -updatedAt,-createdAt
search = false, raw = false, full = false, where = {}, access_token, refresh_token, isDebugging = false, ...filter } = req.query;
// parse "populate" & "select"
const _populate = populate ? (0, lodash_1.trim)(populate.toString(), ",") : "";
const _select = select ? (0, lodash_1.trim)(select.toString(), ",") : "";
const options = {
isDebugging,
download,
full,
status,
populate: _populate == "" ? [] : _populate.indexOf(",") > -1 ? _populate.split(",") : [_populate],
select: _select == "" ? [] : _select.indexOf(",") > -1 ? _select.split(",") : [_select],
};
// parse "search"
if (search === true) {
Object.entries(filter).forEach(([key, val]) => {
filter[key] =
(0, lodash_1.isString)(val) &&
!(0, mongodb_1.isValidObjectId)(val) &&
!(0, lodash_1.isBoolean)(val) &&
!(0, lodash_1.isDate)(val) &&
!(0, lodash_1.isNumber)(val) &&
!(0, class_validator_1.isBooleanString)(val) &&
!(0, class_validator_1.isNumberString)(val)
? { $regex: (0, lodash_1.trim)(val), $options: "i" }
: val;
});
}
// parse "sort" (or "order") from the query url:
let _sortOptions;
if (sort)
_sortOptions = sort.indexOf(",") > -1 ? sort.split(",") : [sort];
if (order)
_sortOptions = order.indexOf(",") > -1 ? order.split(",") : [order];
const sortOptions = {};
if (_sortOptions)
_sortOptions.forEach((s) => {
const isDesc = s.charAt(0) === "-";
const key = isDesc ? s.substring(1) : s;
const sortValue = isDesc ? -1 : 1;
sortOptions[key] = sortValue;
});
if (!(0, lodash_1.isEmpty)(sortOptions))
options.order = sortOptions;
if (raw === "true" || raw === true)
options.raw = true;
// parse "pagination"
if (page && page_size) {
options.skip = ((page !== null && page !== void 0 ? page : 1) - 1) * page_size;
options.limit = page_size;
}
if (limit > 0)
options.limit = limit;
if (skip)
options.skip = skip;
// assign to controller:
return { options, filter: (0, parse_request_filter_1.parseRequestFilter)({ ...filter }) };
}
exports.parseFilterAndOptions = parseFilterAndOptions;
async function parsePagination(service, req) {
if (!service)
return;
let total_items = 0, total_pages = 0, current_page = 1, page_size = 0;
const { id, download, skip, limit = 0, page = 1, size = services_1.DEFAULT_PAGE_SIZE, populate, select, status, sort = "createdAt", search = false, full = false, isDebugging = false, access_token, refresh_token, ...filter } = req.query;
const pageOptions = { skip: (0, object_1.toInt)(skip), limit: (0, object_1.toInt)(limit), page: (0, object_1.toInt)(page), size: (0, object_1.toInt)(size) };
// log(`pageOptions >>`, pageOptions);
total_items = await service.count(filter);
total_pages = limit == 0 ? 1 : Math.ceil(total_items / pageOptions.limit);
if (pageOptions.size > 0)
page_size = pageOptions.size;
if (pageOptions.page > 0)
current_page = pageOptions.page;
// const totalSkip = skip > 0 ? pageOptions.skip : current_page > 0 ? (current_page - 1) * page_size : undefined;
const totalLimit = pageOptions.limit > 0 ? pageOptions.limit : page_size > 0 ? page_size : undefined;
if (totalLimit)
total_pages = Math.ceil(total_items / totalLimit);
// if (totalSkip) page_size = totalSkip;
// log(`totalSkip >>`, totalSkip);
return {
total_items,
total_pages,
current_page,
page_size,
};
}
exports.parsePagination = parsePagination;