Schema Overview
The CEDIS Pedidos database uses PostgreSQL with 6 main tables, 5 enum types, and Row Level Security policies. The schema is defined insupabase/schema.sql.
The database contains 168 pre-seeded materials across 5 categories:
- 40 Materias Primas (Raw Materials)
- 82 Esencias (Fragrances)
- 21 Varios (Miscellaneous)
- 14 Envases Vacíos (Empty Containers)
- 11 Colores (Colors)
Entity Relationship Diagram
Enum Types
categoria_enum
Material categories for organizing the catalog:rol_enum
User roles in the system:estado_pedido
Order workflow states:Additional Enums (in TypeScript)
These are enforced in the application layer and database constraints:- EstadoCuenta:
'pendiente' | 'activo' | 'inactivo' - TipoEntrega:
'HINO' | 'Recolección en CEDIS' - EstadoSolicitud:
'pendiente' | 'aprobado' | 'rechazado'
Tables
sucursales
Branch office locations.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PK | Unique identifier |
nombre | text | NOT NULL | Full branch name |
abreviacion | text | UNIQUE, NOT NULL | Short code (e.g., “PAC1”, “GDL”) |
ciudad | text | NOT NULL | City location |
activa | boolean | NOT NULL, DEFAULT true | Active status flag |
View Pre-loaded Branch Offices
View Pre-loaded Branch Offices
| Nombre | Abreviación | Ciudad |
|---|---|---|
| Pachuca I | PAC1 | Pachuca |
| Guadalajara | GDL | Guadalajara |
| CDMX Norte | CDMX | Ciudad de México |
supabase/schema.sql:193src/lib/types.ts:22
users
User profiles extending Supabase auth.users.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PK, FK → auth.users | User ID from Supabase Auth |
nombre | text | NOT NULL | Full name |
email | text | UNIQUE, NOT NULL | Email address |
rol | rol_enum | NOT NULL | User role (admin/sucursal) |
sucursal_id | uuid | FK → sucursales, NULL for admins | Assigned branch |
estado_cuenta | text | CHECK constraint | Account status |
es_superadmin | boolean | DEFAULT false | Superadmin flag (bypass some restrictions) |
src/lib/types.ts:30
The
users table extends Supabase’s auth.users table. When an auth user is deleted, the profile is automatically removed (CASCADE).materiales
Material catalog with 168 pre-seeded items.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PK | Unique identifier |
codigo | text | UNIQUE, nullable | Material code (not all materials have codes) |
nombre | text | NOT NULL | Material name |
categoria | categoria_enum | NOT NULL | Category (see enum types) |
unidad_base | text | NOT NULL, DEFAULT ‘kgs’ | Unit of measurement |
peso_aproximado | numeric | nullable | Approximate weight per container |
envase | text | nullable | Container type/size |
orden | integer | NOT NULL | Display order within category |
activo | boolean | NOT NULL, DEFAULT true | Active status (added in migration) |
src/lib/types.ts:55
Material Categories Distribution:
Materias Primas (40 items)
Materias Primas (40 items)
Raw materials for chemical production:
- Aceite De Pino, Aceite De Silicon, Aceite Mineral
- Alcohol Etilico, Alcohol Laurico, Alfagin, Amgin
- Amida De Coco, Antiespumante, Blend CHJO-22
- Butil Cellosolve, Conservadores, Creolina
- Edgin, Emulsificante, Formol, Gas Nafta
- Glicerina, Hexano, Lasgin, Less, Nacarante
- Nonil, Oxagin, Pasta Suavizante, Peroxido
- Q60, Silicon, Sosa Liquida, Syngin, T-20
- Trieta, Vaselina Solida
supabase/schema.sql:203Esencias (82 items)
Esencias (82 items)
Fragrances for cleaning products:
- Alaska, Almendra, Aloe Vera, Amaderado, Amor
- Aqua Fresh, Azahar, Baby, Bebe Fresh, Blue Softener
- Brisas, Brisa Tropical, Canela, Cedar Wood, Cereza
- Citronela, Citrus, Clavel, Coco, Cocktail, Cuero
- Durazno, Eucalipto, Floral, Flores Blancas, Fresa
- Gardenia, Herbal, Jazmin, Lavanda (Francesa/Inglesa)
- Lemon Fresh, Limon, Lila, Lluvia, Magnolia
- Mango, Manzana, Marine, Melon, Menta, Miel
- Naranja, Nardo, Neroli, Ocean, Orquidea
- Pina, Pino, Primavera, Rosas, Sandalo, Talco
- Uva, Vainilla, Verde, Violeta, White Musk, Yuzu
- And many more…
supabase/schema.sql:246Varios (21 items)
Varios (21 items)
Miscellaneous supplies:
- Amonaco, Antiginscal, Alcohol Cetilico
- Base Insecticida, Citrico, Gyrgen
- Jarras, Optgin, Past. Tikilín 1”
- Pesalejías, Probetas, Rollos de Ticket
- Sal Industrial, Sebo Destilado, Silica Gel
- Tiras pH, Tinopal CBS-X, Tripolifosfato
- Urea, Zeolita, Zeolita Cargada
supabase/schema.sql:331Envases Vacíos (14 items)
Envases Vacíos (14 items)
Empty containers and packaging:
- Botella nueva de 1 Lt (150 pcs packages)
- Contenedor de 1,000 Lts
- Tambo De Plastico 200 Lts (Abierto/Cerrado)
- Tambo De Metal 200 Lts
- Garrafon 10 Lts c/Tapa (24 pcs packages)
- Garrafon 20 Lts c/Tapa (12 pcs packages)
- Cubeta Blanca 19 Lts (con/sin tapa)
- Cubeta Amarilla 19 Lts
- Bolsa Polietileno 10kg
- Cajas de Carton
- Tapa Garrafon 20 Lts
supabase/schema.sql:355Colores (11 items)
Colores (11 items)
Colorants and dyes:
- Amarillo Fluorescente, Amarillo Huevo
- Azul, Azul Brillante, Azul Marino
- Color Morado, Naranja, Pigmento Rojo
- Rosa, Rojo, Verde
supabase/schema.sql:372pedidos
Main order/requisition records.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PK | Unique identifier |
codigo_pedido | text | UNIQUE, NOT NULL | Order code (e.g., “PAC1-2024-001”) |
sucursal_id | uuid | FK → sucursales | Branch that created the order |
fecha_entrega | date | NOT NULL | Requested delivery date |
tipo_entrega | text | nullable | Delivery type (HINO/Recolección) |
total_kilos | numeric | NOT NULL, DEFAULT 0 | Total weight in kilograms |
estado | estado_pedido | NOT NULL, DEFAULT ‘borrador’ | Current order status |
created_at | timestamptz | NOT NULL, DEFAULT now() | Creation timestamp |
updated_at | timestamptz | NOT NULL, DEFAULT now() | Last update timestamp |
enviado_at | timestamptz | nullable | Submission timestamp |
enviado_por | uuid | FK → users | User who submitted the order |
updated_at timestamp on every UPDATE.
TypeScript Type:
src/lib/types.ts:67
Orders have an 11,500 kg frontend limit with a 13,000 kg absolute database maximum enforced by the
validate_pedido_limit() function.pedido_detalle
Order line items (many-to-many between orders and materials).| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PK | Unique identifier |
pedido_id | uuid | FK → pedidos, CASCADE delete | Parent order |
material_id | uuid | FK → materiales | Material being requested |
cantidad_kilos | numeric | nullable | Quantity in kilograms |
cantidad_solicitada | numeric | nullable | Requested quantity (units) |
peso_total | numeric | nullable | Total weight for this line |
lote | text | nullable | Batch/lot number |
peso | numeric | nullable | Individual weight |
src/lib/types.ts:82
When an order is deleted, all associated detail records are automatically removed due to
ON DELETE CASCADE.solicitudes_acceso
User registration and access approval workflow.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PK | Unique identifier |
user_id | uuid | FK → users, nullable | Linked user after approval |
nombre | text | NOT NULL | Requester’s full name |
email | text | NOT NULL | Requester’s email |
sucursal_id | uuid | FK → sucursales, nullable | Requested branch assignment |
mensaje | text | nullable | Request message/reason |
estado | text | CHECK constraint | Request status |
revisado_por | uuid | FK → users, nullable | Admin who reviewed |
revisado_at | timestamptz | nullable | Review timestamp |
created_at | timestamptz | NOT NULL, DEFAULT now() | Request creation time |
src/lib/types.ts:41
Database Functions
validate_pedido_limit
Validates that an order doesn’t exceed the 13,000 kg absolute database maximum. The frontend enforces a stricter 11,500 kg operational limit.- Defined as
SECURITY DEFINER- runs with function owner’s privileges - Allows RLS-restricted users to check limits on their own orders
supabase/schema.sql:97
TypeScript Type Definitions
Complete database type definitions are available insrc/lib/types.ts:114:
These types provide full IDE autocomplete and type safety when working with Supabase queries.
Schema Migration History
Initial Schema
Source:supabase/schema.sql
- Core tables and relationships
- Enums and constraints
- RLS policies
- 168 seeded materials
- 3 seeded branch offices
Access Control Migration
Source:supabase/add_auth_access_control.sql
- Added
estado_cuentacolumn to users - Added
es_superadmincolumn to users - Created
solicitudes_accesotable - Added RLS policies for access requests
- Set superadmin flags for initial administrators
Query Examples
Get All Active Materials by Category
Create Order with Details
Get Orders with Relations
All queries are automatically filtered by Row Level Security policies based on the authenticated user’s role and branch assignment.