feat: standardise PostgREST string escaping across codebase (Closes #1543)#1894
Conversation
dipexplorer
left a comment
There was a problem hiding this comment.
The PostgREST quoting syntax is incorrect and breaks the search.
When you use .ilike.%${escapePostgrest(val)}%, the resulting string evaluates to .ilike.%"val". In PostgREST, double quotes must wrap the entire value to escape commas, not sit inside the % wildcards. Because the quotes are in the middle, PostgREST will either throw a syntax error or search for literal double-quotes.
Additionally, in interactions.ts, escaped is already processed by escapeIlike(). Passing it into escapePostgrest(escaped) causes double-escaping (\\\\, \\%).
How to fix:
- Modify
escapePostgrestto just escape characters without hardcoding the surrounding double quotes:
export function escapePostgrest(val: string): string {
return val
.replace(/\\/g, "\\\\")
.replace(/%/g, "\\%")
.replace(/_/g, "\\_")
.replace(/"/g, '""');
}- For
.ilike()filters, wrap the entire expression in double quotes in your template literals, placing the%wildcards inside:
.or(`brand_name.ilike."%${escapePostgrest(trimmed)}%",batch_number.ilike."%${escapePostgrest(trimmed)}%"`)Please update this across all modified files.
|
Fixed all issues raised in review:
|
|
🎉 Congratulations @nimkarprachi17! Your Pull Request "feat: standardise PostgREST string escaping across codebase (Closes #1543)" has been successfully merged by @dipexplorer. Thank you for your valuable contribution to SahiDawa! 🇮🇳 Follow us on LinkedIn: https://www.linkedin.com/company/ratloopz/ to get shoutout |
🛑 STOP: Assignment & File Scope Check
📋 PR Summary & Link
escapePostgrest()utility and replaces all naked string interpolation in Supabase.or()and.ilike()filters across bothapps/apiandapps/web.📸 Proof of Work
npx tsc --noEmitclean on both api and web)git diff --stat mainconfirms exactly 6 files changed — all scoped to this issueAnalyticsCharts.tsxandCacheStatsCard.tsxare unrelated to this PRFiles changed:
apps/api/src/utils/db.ts— addedescapePostgrest()apps/web/lib/supabase/utils.ts— new file withescapePostgrest()apps/api/src/routes/interactions.ts— fixed 2 naked.or()filtersapps/api/src/routes/scan.ts— fixed 2 naked.or()filtersapps/api/src/services/medicineRag.service.ts— fixed 1 naked.or()filterapps/web/app/[locale]/components/SearchBar.tsx— fixed 1 naked.or()filter🏷️ PR Type
type: featuretype: securitytype: refactor✅ Checklist
Closes #1543)mainand resolved any conflicts