Overview
CEDIS Pedidos uses Supabase as its backend platform, providing PostgreSQL database, authentication, and real-time capabilities. This guide walks you through setting up a complete Supabase project with the required schema and security policies.Prerequisites
- A Supabase account (sign up here)
- Basic understanding of PostgreSQL and SQL
- Access to the Supabase SQL Editor
Project Setup
Create a new Supabase project
- Log in to your Supabase dashboard
- Click New Project
- Enter your project details:
- Name: CEDIS Pedidos (or your preferred name)
- Database Password: Strong password (save this securely)
- Region: Choose the closest region to your users
- Pricing Plan: Select based on your needs (Free tier works for development)
- Click Create new project
Project creation takes 1-2 minutes. Supabase will provision a PostgreSQL database and set up authentication services.
Access the SQL Editor
Once your project is ready:
- Navigate to SQL Editor in the left sidebar
- Click New Query to create a new SQL script
- You’ll use this editor to run the database schema
Database Schema
The CEDIS Pedidos database consists of five main tables with strict Row Level Security policies:Schema Overview
Complete Schema SQL
Execute this complete SQL script in your Supabase SQL Editor:schema.sql
The schema includes seed data for 3 branches (sucursales) and 168 materials across 5 categories. The complete materials seed data is included in the full
schema.sql file.Row Level Security (RLS) Explained
RLS is the core security mechanism that protects data in CEDIS Pedidos:Security Model
Key Security Features
- Branch Isolation: Each sucursal can only see and modify their own orders
- Admin Privileges: Admin users have full read/write access across all data
- Draft Protection: Only draft orders can be modified by branches
- Cascade Security: Order details (
pedido_detalle) inherit security from parent orders - Weight Validation: RPC function validates 13,000 kg absolute maximum (frontend enforces 11,500 kg operational limit)
Authentication Setup
Configure authentication providers
Navigate to Authentication → Providers in your Supabase dashboard.For CEDIS Pedidos, configure:
- Email: Enable email/password authentication
- Disable social providers (not required for this application)
Configure email templates
Go to Authentication → Email Templates to customize:
- Confirmation emails
- Password reset emails
- Change email confirmation
Database Indexes
The schema includes optimized indexes for common queries:- Filtering orders by branch
- Filtering orders by status (borrador, enviado, aprobado, impreso)
- Filtering orders by delivery date
- Joining order details with orders
- Looking up materials in orders
Seeded Data
The schema includes initial data:Branches (Sucursales)
| Name | Abbreviation | City |
|---|---|---|
| Pachuca I | PAC1 | Pachuca |
| Guadalajara | GDL | Guadalajara |
| CDMX Norte | CDMX | Ciudad de México |
Materials (Materiales)
The schema seeds 168 materials across 5 categories:- Materias Primas (Raw Materials): 40 items
- Esencias (Fragrances): 82 items
- Varios (Miscellaneous): 21 items
- Envases Vacíos (Empty Containers): 14 items
- Colores (Colors): 11 items
Testing Your Setup
Verify tables exist
Run this query in the SQL Editor:You should see:
materiales, pedido_detalle, pedidos, sucursales, usersCheck seed data
Verify branches and materials were seeded:Expected results: 3 branches, 168 materials
Troubleshooting
Schema Execution Errors
Error:type "categoria_enum" already exists
Solution: The enum types already exist. Either drop them first or skip the enum creation statements.
RLS Policy Conflicts
Error:policy "policy_name" already exists
Solution: Drop existing policies before recreating:
Connection Issues
Error: Cannot connect from application Solution:- Verify your
VITE_SUPABASE_URLandVITE_SUPABASE_ANON_KEYare correct - Check that RLS is enabled on all tables
- Ensure your project is not paused (free tier projects pause after inactivity)
Next Steps
With your Supabase project configured:- Deploy to Vercel - Deploy your application to production
- Create admin and branch user accounts through Supabase Auth
- Test the complete application flow from order creation to approval