How to create query builder?

I want to make a query builder. Saw the recipe in this article https://medium.com/starschema-blog/advanced-server-side-filtering-for-tables-in-appsmith-1c8bd595559c

This is my query:

SELECT * FROM companies
where 1=1
{{ JSObject1.getCompaniesConditionSql() }}
ORDER BY {{ companies_table.sortOrder.column || 'id'}} {{ companies_table.sortOrder.order || 'ASC'}}
LIMIT {{companies_table.pageSize}}
OFFSET {{(companies_table.pageNo - 1) * companies_table.pageSize}};

This is js code:

	getCompaniesConditionSql: () => {
		var conditionSql=""
		if (!!companies_global_status_select.selectedOptionValue){
			conditionSql= conditionSql + "and global_status='" + companies_global_status_select.selectedOptionValue+"'"
		}
		return conditionSql
	}

But when I run, there is an error. Looks like appsmith adds additional quotes and it breaks sql.
Is there a way to implement query builder?

Can’t add more pictures to the post, so adding it here.
This is the error with empty condition:

And this is the error with condition:

Hi! Can you try to run this query with Use Prepared Statement turned OFF?

1 Like

@Amelia thank you, it solved the problem!
By the way, I think query builder is really common pattern, it would be nice to have some reference in docs.