You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT id, name, category, price, ts_rank(...) as rank
FROM"Product"WHERE"searchVector" @@ plainto_tsquery('english', 'running shoes')
ORDER BY rank DESC;
-- Result:
id | name | category | price | rank
1 | Nike Air React | Shoes | 120 | 0.0993 | Adidas Ultraboost | Shoes | 180 | 0.0995 | Nike React Infinity | Shoes | 160 | 0.099
Test: Vector Similarity Search
-- Query: "sports audio equipment"SELECT name, category, (embedding <=> query_vec) as distance
FROM"Product"ORDER BY distance ASC;
-- Result: Sony headphones (0.41) closer than Nike shoes (0.55)
name | category | distance
----------------------+------------+-----------
Sony WH-1000XM5 | Electronics| 0.413
Apple AirPods Pro | Electronics| 0.413
Nike Air React | Shoes | 0.552
Test: Hybrid Search (FTS + Vector Rerank)
-- Stage 1: FTS candidates-- Stage 2: Vector rerank
WITH fts_results AS (
SELECT id FROM"Product"WHERE"searchVector" @@ to_tsquery('wireless headphones')
LIMIT50
)
SELECT*FROM fts_results ORDER BY semantic_distance;
2. Data Isolation ✅
Test: Multi-tenant Customer Separation
-- Alice's orders (customer_id = 1)SELECT id, customer_id, total FROM"Order"WHERE customer_id =1;
-- Result: Only Alice's orders visible-- Bob's orders (customer_id = 2) SELECT id, customer_id, total FROM"Order"WHERE customer_id =2;
-- Result: Only Bob's orders visible
3. Idempotency ✅
Test: UPSERT Cart Items
-- First add: quantity = 2INSERT INTO cart_items (cart_id, product_id, quantity)
VALUES ('cart-123', 1, 2)
ON CONFLICT (cart_id, product_id)
DO UPDATESET quantity =EXCLUDED.quantity;
-- Second add: quantity = 3 (SAME CART, SAME PRODUCT)INSERT INTO cart_items (cart_id, product_id, quantity)
VALUES ('cart-123', 1, 3)
ON CONFLICT (cart_id, product_id)
DO UPDATESET quantity =EXCLUDED.quantity;
-- Result: Only 1 row with quantity = 3 (not duplicated!)
4. Atomic Transactions ✅
Test: Order Creation with Stock Deduction
BEGIN;
-- Atomic: Check stock and deductUPDATE"Product"SET stock = stock -1WHERE id =1AND stock >0;
-- Create order only if stock was availableINSERT INTO"Order" (customer_id, total, status)
SELECT1, price, 'confirmed'FROM"Product"WHERE id =1AND stock >=0;
COMMIT;
-- Verify: Stock reduced (50 → 49)-- Verify: Order created with status 'confirmed'