Skip to content

Commit

Permalink
update query builder search function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehedihasan444 committed Sep 10, 2024
1 parent 433a9bd commit 0a3f676
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
22 changes: 22 additions & 0 deletions dist/app/builder/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@ class QueryBuilder {
this.modelQuery = modelQuery;
this.query = query;
}
// search(searchableFields: string[]) {
// const searchTerm = [this?.query?.searchTerm];
// console.log(searchTerm)
// if (searchTerm) {
// this.modelQuery = this.modelQuery.find({
// $or: searchableFields.map(
// (field) =>
// ({
// [field]: { $regex: searchTerm, $options: "i" },
// } as FilterQuery<T>)
// ),
// });
// }
// return this;
// }
search(searchableFields) {
var _a;
const searchTerm = (_a = this === null || this === void 0 ? void 0 : this.query) === null || _a === void 0 ? void 0 : _a.searchTerm;
if (searchTerm) {
// Split the comma-separated string into an array of features
const featureArray = typeof searchTerm === 'string' ? searchTerm.split(',').map(f => f.trim()) : [];
this.modelQuery = this.modelQuery.find({
$or: searchableFields.map((field) => ({
[field]: { $regex: searchTerm, $options: "i" },
})),
});
if (featureArray && featureArray.length > 0) {
this.modelQuery = this.modelQuery.find({
features: { $in: featureArray }, // Matches cars with any of the selected features
});
}
}
return this;
}
Expand Down
30 changes: 29 additions & 1 deletion src/app/builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,29 @@ class QueryBuilder<T> {
this.query = query;
}

// search(searchableFields: string[]) {
// const searchTerm = [this?.query?.searchTerm];
// console.log(searchTerm)
// if (searchTerm) {
// this.modelQuery = this.modelQuery.find({
// $or: searchableFields.map(
// (field) =>
// ({
// [field]: { $regex: searchTerm, $options: "i" },
// } as FilterQuery<T>)
// ),
// });
// }

// return this;
// }
search(searchableFields: string[]) {
const searchTerm = this?.query?.searchTerm;


if (searchTerm) {
// Split the comma-separated string into an array of features
const featureArray = typeof searchTerm === 'string' ? searchTerm.split(',').map(f => f.trim()) : [];
this.modelQuery = this.modelQuery.find({
$or: searchableFields.map(
(field) =>
Expand All @@ -20,10 +40,18 @@ class QueryBuilder<T> {
} as FilterQuery<T>)
),
});
if (featureArray && featureArray.length > 0) {
this.modelQuery = this.modelQuery.find({
features: { $in: featureArray }, // Matches cars with any of the selected features
});
}
}



return this;
}



filter() {
const queryObj = { ...this.query };
Expand Down

0 comments on commit 0a3f676

Please sign in to comment.