mongoose-advanced-paginate
Version:
A library which makes pagination with search, sorting and filtering a breeze for devs in mongoose.
156 lines (152 loc) • 5.2 kB
JavaScript
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/types.ts
var ElementType = /* @__PURE__ */ ((ElementType2) => {
ElementType2["ID"] = "id";
ElementType2["STRING"] = "string";
return ElementType2;
})(ElementType || {});
var SortOrderDirection = /* @__PURE__ */ ((SortOrderDirection2) => {
SortOrderDirection2["ASC"] = "asc";
SortOrderDirection2["DESC"] = "desc";
return SortOrderDirection2;
})(SortOrderDirection || {});
// src/helper-functions.ts
import { Types, isValidObjectId } from "mongoose";
var isMongoId = (str) => {
return isValidObjectId(str) && str.match(/^[0-9a-fA-F]{24}$/) && str.length === 24;
};
var filterTransform = (filterBy, selectors) => {
const filters = [];
filterBy.forEach((filterElem) => {
const filterValue = selectors == null ? void 0 : selectors[filterElem];
const filterObj = {};
if (filterValue) {
if (typeof filterValue === "string" && isMongoId(filterValue)) {
filterObj[filterElem] = new Types.ObjectId(filterValue);
} else {
filterObj[filterElem] = filterValue;
}
}
if (filterObj[filterElem] !== void 0 && filterObj[filterElem] !== null && filterObj[filterElem] !== "" && filterValue !== "All")
filters.push(filterObj);
});
return filters;
};
var searchTransform = (searchBy, searchText) => {
return searchBy.map((searchElem) => {
const searchObj = {};
if (isMongoId(searchText)) {
searchObj[searchElem] = new Types.ObjectId(searchText);
} else {
searchObj[searchElem] = {
$regex: searchText,
$options: "i"
};
}
return searchObj;
});
};
var isEmptyObject = (obj) => {
return obj === void 0 || Object.keys(obj).length === 0;
};
// src/plugin.ts
var paginatePlugin = (schema) => {
schema.statics.paginate = function(query, options) {
return __async(this, null, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
const page = options.page || 1;
const limit = options.limit;
const filterPipeline = [];
if (!isEmptyObject(query)) {
filterPipeline.push({ $match: query });
}
options.customFilters && filterPipeline.push(...options.customFilters);
if (((_b = (_a = options.filter) == null ? void 0 : _a.filterBy) == null ? void 0 : _b.length) && !isEmptyObject((_c = options.filter) == null ? void 0 : _c.selectors)) {
const filters = filterTransform(
(_d = options.filter) == null ? void 0 : _d.filterBy,
(_e = options.filter) == null ? void 0 : _e.selectors
);
filters.length && filterPipeline.push({
$match: {
$and: filters
}
});
}
((_g = (_f = options.search) == null ? void 0 : _f.searchBy) == null ? void 0 : _g.length) && ((_h = options.search) == null ? void 0 : _h.searchText) !== void 0 && ((_i = options.search) == null ? void 0 : _i.searchText) !== "" && filterPipeline.push({
$match: {
$or: searchTransform(
(_j = options.search) == null ? void 0 : _j.searchBy,
(_k = options.search) == null ? void 0 : _k.searchText
)
}
});
const countPipeline = [
...filterPipeline,
{ $count: "total" }
];
const recordsPipeline = [...filterPipeline];
options.lookups && recordsPipeline.push(...options.lookups);
if ((_l = options.sortOrder) == null ? void 0 : _l.id) {
recordsPipeline.push({
$sort: {
[(_m = options.sortOrder) == null ? void 0 : _m.id]: options.sortOrder.direction === "asc" /* ASC */ ? 1 : -1
}
});
}
if (limit) {
const skip = (page - 1) * limit;
recordsPipeline.push({ $skip: skip });
recordsPipeline.push({ $limit: limit });
}
options.extraStages && recordsPipeline.push(...options.extraStages);
options.project && recordsPipeline.push({ $project: options.project });
const aggregationPipeline = [
{
$facet: {
total: countPipeline,
records: recordsPipeline
}
}
];
const [aggregationResult] = yield this.aggregate(
aggregationPipeline,
options.aggregateOptions
);
const total = ((_n = aggregationResult == null ? void 0 : aggregationResult.total[0]) == null ? void 0 : _n.total) || 0;
const records = (aggregationResult == null ? void 0 : aggregationResult.records) || [];
return {
total,
page,
limit,
records
};
});
};
};
export {
ElementType,
SortOrderDirection,
filterTransform,
isEmptyObject,
paginatePlugin,
searchTransform
};