@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
43 lines (42 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseRequestFilter = void 0;
const lodash_1 = require("lodash");
const mongodb_1 = require("./mongodb");
const parseRequestFilter = (requestQuery) => {
const { id,
// pagination
skip, limit = 0, page = 1, size = 0,
// options
populate, select, status, sort, // @example: -updatedAt,-createdAt
order, // @example: -updatedAt,-createdAt
download = false, search = false, raw = false, full = false, isDebugging = false,
// others
where = {}, access_token, refresh_token, ...filter } = requestQuery;
// filter
let _filter = id ? { id, ...filter } : filter;
if (_filter.id) {
_filter._id = _filter.id;
delete _filter.id;
}
// manipulate "$or" & "$and" filter:
if (_filter.or) {
_filter.$or = _filter.or;
delete _filter.or;
}
if (_filter.and) {
_filter.$and = _filter.and;
delete _filter.and;
}
// console.log("[2] _filter :>> ", _filter);
/**
* Traverse filter object and transform the values.
* Need to cast valid {ObjectId} string to {ObjectId} since Mongoose "aggregate" doesn't cast them automatically.
* @link https://mongoosejs.com/docs/api/aggregate.html#Aggregate()
*/
return (0, lodash_1.cloneDeepWith)(_filter, function (value) {
if (mongodb_1.MongoDB.isValidObjectId(value))
return mongodb_1.MongoDB.toObjectId(value);
});
};
exports.parseRequestFilter = parseRequestFilter;