Skip to content

Conversation

@justanotheratom
Copy link
Collaborator

Summary

This PR adds a caching layer for Open Food Facts product data to improve barcode lookup performance and reliability.

Changes

  • New table: with RLS policies (reads for authenticated users, writes for service role)
  • Deno script: that downloads OFF JSONL.gz, transforms to our product format, and batch-upserts to Supabase
  • Authentication: Updated to use (new key type) with fallback to legacy

Usage

# Set environment variables
export SUPABASE_URL=your_url
export SUPABASE_SECRET_KEY=your_secret_key

# Run the ingest script
deno run -A --import-map=local/import_map.json local/off_ingest.ts

Benefits

  • Faster barcode lookups (local cache vs API calls)
  • More reliable (offline-capable with cached data)
  • Fresh data (regular updates from OFF dumps)
  • Scalable (handles 4M+ products efficiently)

Database Impact

  • Estimated table size: ~8-12 GB for full OFF dataset
  • Indexed on barcode for fast lookups
  • JSONB columns for flexible ingredient/image storage

cursoragent and others added 22 commits September 28, 2025 15:45
…nore

- Move off_ingest.ts and off_upload_batch.ts to local/openfoodfacts/ folder
- Add off_inventory_cache.jsonl to .gitignore (large generated file)
- Fix parallel upload logic to prevent memory exhaustion
- Add deduplication to handle duplicate barcodes in batches
- Successfully uploaded 3M+ products to Supabase inventory_cache
- Removed log_inventory table definition from tables.sql
- Updated get_check_history and get_list_items functions to use inventory_cache
- Removed background/log_inventory endpoint
- Created consolidated getProductFromCache() function as single source of truth
- Updated all code to query inventory_cache instead of log_inventory
- Removed redundant lookupProduct wrapper function
- inventory.ts now checks cache first, falls back to fresh API fetch
- Removed fetchProduct() function and OpenFoodFacts API integration
- Removed processOpenFoodFactsProductData() and helper functions
- Simplified get() endpoint to only read from inventory_cache
- Reduced file from 252 lines to 104 lines (67% reduction)
- Now returns 404 if product not found in cache
- Add barcodeToPath() function to convert barcodes to proper OFF path format (e.g., 088/491/237/3946)
- Fix image URL format: full size uses imgId.jpg instead of imgId.WxH.jpg
- Add validateImageUrlExists() function for optional HEAD request validation (currently disabled)
- Update extractDisplayImageUrls() to use correct barcode paths in URLs
- All image URLs now follow format: /images/products/{barcodePath}/{imgId}.{size}.jpg
- Only upsert barcode and images fields during upload
- Remove updated_at field from upserts (column may not exist)
- Add ignoreDuplicates: false to ensure existing rows are updated
- Improve error logging with JSON.stringify for better debugging
- This is a temporary change to fix image URLs without reprocessing all data
- Add barcode_matches() SQL function that pads upward only to avoid false matches
- Update inventory query to try multiple barcode format variants (EAN-8, UPC-A, EAN-13, ITF-14)
- Apply barcode_matches() to JOINs in get_check_history() and get_list_items()
- Prevents 8-digit barcodes from matching unrelated 13-digit barcodes
- Fixes case where barcode 884912373946 should match 0884912373946
- Remove temporary image-only update logic
- Restore full batch upserts with all product data
- Will populate fresh inventory_cache table with:
  - Fixed image URLs with correct barcode paths
  - All product metadata (name, brand, ingredients, etc.)
  - Proper last_refreshed_at timestamps
- Change ImageMetadata type to minimal structure (type, language, imgid, sizes[])
- Extract all image types: front, ingredients, nutrition, packaging
- Extract images for all languages (en, fr, de, es, it, pt, nl, pl, ru, ja, zh, etc.)
- Remove URL construction logic (moved to runtime in inventory.ts)
- Remove unused helper functions: isValidImageUrl, validateImageUrlExists, barcodeToPath
- Add comprehensive documentation about image storage strategy
- Store only language keys, drop numeric keys to reduce storage by ~60%

Image URLs will be constructed at runtime with smart selection:
- Prefer English, fallback to other languages
- Prefer 400px (medium), fallback to next available size
- Return one image per type (front, ingredients, nutrition, packaging)
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

Successfully merging this pull request may close these issues.

3 participants