fix(search): implement optimized favorites search with database-direct RPC #993
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Implements optimized favorites search functionality by creating a specialized PostgreSQL RPC that fetches user favorites directly from the database, eliminating client-side parameter passing and improving performance.
Problem Solved
Previously, the favorites tab search was broken - it returned all foods instead of filtering to only the user's favorite foods. The issue was in
fetchFoodsByNamewhich ignored theallowedFoodsparameter when calling the PostgreSQL RPC function.Implementation Details
Database Optimization
search_favorite_foods_with_scoring(user_id, search_term, limit)users.favorite_foodsdirectly instead of receiving IDs as parametersTypeScript Interface Simplification
userIdandisFavoritesSearchboolean flagsfetchFoodsByNameautomatically chooses optimal RPC based on parametersfetchFoodsByName(search, { userId, isFavoritesSearch: true })Code Quality Improvements
Technical Architecture
Files Modified
Database
database/search_favorite_foods_with_scoring.sql- New optimized RPC with database-direct accessDomain Layer
src/modules/diet/food/domain/foodRepository.ts- ExtendedFoodSearchParamsinterfaceInfrastructure Layer
src/modules/diet/food/infrastructure/supabaseFoodRepository.ts- Smart RPC selection logicApplication Layer
src/modules/search/application/searchLogic.ts- Updated to use new interfacesrc/modules/search/application/tests/searchLogic.test.ts- Tests for new behaviorTesting
Performance Benefits
Validation
Closes #990