mongoose-query-builders
Version:
A lightweight, chainable query builder utility for Mongoose that supports search, filter, sort, field selection, and pagination.
93 lines (91 loc) • 2.8 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var QueryBuilder = class {
constructor(queryModel, query) {
this.modelQuery = queryModel;
this.query = query;
}
search(searchableFields) {
const searchTerm = this?.query?.searchTerm;
if (searchTerm) {
this.modelQuery = this.modelQuery.find({
$or: searchableFields.map((field) => {
return {
[field]: { $regex: searchTerm, $options: "i" }
};
})
});
}
return this;
}
filter() {
const queryObject = { ...this.query };
const excludeFields = [
"searchTerm",
"sort",
"page",
"limit",
"fields"
];
excludeFields.forEach((field) => delete queryObject[field]);
const queryStr = JSON.stringify(queryObject).replace(
/\b(gte|gt|lte|lt|ne|in|nin)\b/g,
(match) => `$${match}`
);
this.modelQuery = this.modelQuery.find(JSON.parse(queryStr));
return this;
}
sort() {
const sort = this.query?.sort || "-createdAt";
this.modelQuery = this.modelQuery.sort(sort);
return this;
}
paginate() {
const limit = Number(this?.query?.limit) || 10;
const page = Number(this?.query?.page) || 1;
const skip = (page - 1) * limit;
this.modelQuery = this.modelQuery.skip(skip).limit(limit);
return this;
}
fields() {
const fields = this?.query?.fields?.split(",")?.join(" ");
this.modelQuery = this.modelQuery.select(fields);
return this;
}
async countTotal() {
const totalQueries = this.modelQuery.getFilter();
const total = await this.modelQuery.model.countDocuments(totalQueries);
const page = Number(this?.query?.page) || 1;
const limit = Number(this?.query?.limit) || 10;
const totalPage = Math.ceil(total / limit);
return {
page,
limit,
total,
totalPage
};
}
};
var index_default = QueryBuilder;
;