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

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

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

DenisFerrero opened this issue Aug 25, 2023 · 1 comment

Comments

@DenisFerrero
Copy link

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
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?

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