Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case insensitive search [moleculer-db-adapter-sequelize] #368

Closed
DenisFerrero opened this issue Aug 25, 2023 · 1 comment
Closed

Case insensitive search [moleculer-db-adapter-sequelize] #368

DenisFerrero opened this issue Aug 25, 2023 · 1 comment

Comments

@DenisFerrero
Copy link
Contributor

Rather than Like wouldn't be more useful the iLike pattern? We have many entities called "Machine" and "STATIONS" but when the user searches for "machine" or "stations" he does not find anything here the search logic

@DenisFerrero
Copy link
Contributor Author

I found that iLike is available only in PG so to achieve the same result I created this solution

if (_.isString(params.search) && params.search !== "") {
	let fields = [];
	if (params.searchFields) {
		fields = _.isString(params.searchFields) ? params.searchFields.split(" ") : params.searchFields;
	}

-	const searchConditions = fields.map(f => {
-		return {
-			[f]: {
-				[Op.like]: "%" + params.search + "%"
-			}
-		};
-	});

+	const lowerCaseSearch = "%" + (params.search).toLowerCase() + "%";  
+	const searchConditions = fields.map(f => {
+		return Sequelize.where(
+			Sequelize.fn("lower", Sequelize.col(f)),
+			Op.like,
+			lowerCaseSearch
+		)
+	});

	if (params.query) {
		q.where[Op.and] = [
			params.query,
			{ [Op.or]: searchConditions }
		];
	} else {
		q.where[Op.or] = searchConditions;
	}
	...

This solution seems to work but I have some trouble making the tests work. Can someone help me out with that?

@icebob icebob closed this as completed in eaa6897 Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant