name: nexus-supabase-db description: Rules for interacting with Supabase, PostgreSQL Row Level Security (RLS), and pgvector for AI embeddings in Project Nexus. Use this when writing database queries, SQL migrations, or vector similarity search logic.
Supabase & Database Guidelines
Backend Architecture & Workflows
- Modular Code: Separate database queries, API routing, and business logic. Use a service-based architecture (e.g.,
services/queryService.ts) to keep the codebase highly modular and testable. - Data Validation: Always rigorously validate incoming payload data at the API boundary using schema validation (e.g., Zod) before performing database operations.
- Logical Workflows: Follow standard industry workflows for data mutations: Validate Input -> Verify Authorization -> Execute Database Transaction -> Return Standardized Response. Handle errors centrally and return appropriate HTTP status codes.
Row Level Security (RLS) - CRITICAL
- Every table MUST have RLS enabled.
- Write policies ensuring users can only
SELECT,INSERT,UPDATE, orDELETErows where theuser_idmatchesauth.uid(). - Never bypass RLS using the Supabase Service Role Key on the client side.
Vector Search (pgvector)
- The
nodestable contains anembeddingcolumn of typevector(1536)(optimized for OpenAI/standard embedding dimensions). - When querying for similar nodes to build the Knowledge Graph, use the Cosine Distance operator (
<=>). - Create a Postgres function named
match_nodesthat accepts a query embedding and a similarity threshold (e.g.,0.8), returning the matching records.
Spaced Repetition Logic (SuperMemo-2)
- The
reviewstable tracks the memory states. - When updating a review, calculate the new
intervalandease_factorpurely in TypeScript utility functions, then push the resultingnext_review_dateto Supabase. - Always fetch records where
next_review_date <= CURRENT_DATEordered by the oldest dates first.