Supabase Setup
Relevant source files
The following files were used as context for generating this wiki page:Supabase serves as the complete backend for the ELMEC v2 application, providing the database, authentication, real-time capabilities, and file storage. A correct setup is critical for the application to function. This guide covers the entire process, from initial project creation to automated scripting, ensuring a fully operational backend environment.
The setup process involves creating a project, configuring environment variables, running database migrations, setting up authentication providers and users, and configuring file storage buckets and policies. An automated script is also available to streamline parts of this process.
1. Initial Project Creation
The first step is to create a new project within the Supabase dashboard.
- Navigate to supabase.com and start a new project.
- Use the following configuration:
- Name:
ELMEC Mobile App - Database Password: Generate and save a secure password.
- Region: Select the region closest to your user base.
- Name:
After creation, you must retrieve the project's API credentials. These are found in Settings → API. You will need the Project URL and the anon public key.
Sources: supabase/setup-instructions.md:3-19
2. Environment Configuration
The application connects to Supabase using environment variables. These must be configured in a .env or .env.local file in the project root for local development, and in the hosting provider's settings (e.g., Netlify) for deployment.
| Variable | Description | Example Value | Source File |
|---|---|---|---|
EXPO_PUBLIC_SUPABASE_URL | The unique URL for your Supabase project. | https://pdpqkgrqlubyzkcivifk.supabase.co | [DEPLOY-NETLIFY.md:30](https://github.com/softvibeslab/elmecV2-Demo/blob/HEAD/DEPLOY-NETLIFY.md#L30) |
EXPO_PUBLIC_SUPABASE_ANON_KEY | The public, anonymous key for your project. Safe to expose in a client-side app. | eyJhbGci... | [DEPLOY-NETLIFY.md:31](https://github.com/softvibeslab/elmecV2-Demo/blob/HEAD/DEPLOY-NETLIFY.md#L31) |
SUPABASE_SERVICE_ROLE_KEY | The secret service role key for admin-level access. NEVER expose this in the client. Used for scripts. | eyJhbGci... | [scripts/setup-supabase-complete.js:15](https://github.com/softvibeslab/elmecV2-Demo/blob/HEAD/scripts/setup-supabase-complete.js#L15) |
EXPO_PUBLIC_ENVIRONMENT | The application environment. | development or production | [DEPLOY-NETLIFY.md:33](https://github.com/softvibeslab/elmecV2-Demo/blob/HEAD/DEPLOY-NETLIFY.md#L33) |
Sources: supabase/setup-instructions.md:21-26, scripts/setup-supabase-complete.js:12-18, DEPLOY-NETLIFY.md:28-38
3. Database Schema
The database schema defines the structure for all application data. It must be created by running a series of SQL migration files. The recommended method is to execute them in order using the SQL Editor in the Supabase dashboard.
Required Tables
The migrations will create the following core tables:
| Table | Description |
|---|---|
users | Stores public user profile data, linked to auth.users. |
requests | Core table for managing user-submitted service requests. |
chat_rooms | Defines chat sessions between users. |
messages | Stores individual messages within a chat room. |
notifications | Manages in-app and push notifications for users. |
calculator_sessions | Stores data from the integrated calculator tools. |
Sources: supabase/setup-instructions.md:70-76, STATUS-DEPLOY.md:13-20
Entity Relationship Diagram
The following diagram illustrates the relationships between the main tables in the database.
This diagram is based on schema information from multiple report files.
Sources: REPORTE_PRUEBAS_REQUESTS_CRUD.md, REPORTE_CHAT_MODULE.md
Migration Execution Order
The SQL files located in supabase/migrations/ must be executed in the following numerical order:
001_create_users_table.sql002_create_requests_table.sql003_create_chat_system.sql004_create_notifications_system.sql005_create_calculator_data.sql006_create_analytics_and_logs.sql
Sources: supabase/setup-instructions.md:34-42
4. Authentication
URL Configuration
For authentication to work correctly, especially with third-party providers or when deployed, you must configure the application's URLs in the Supabase dashboard.
- Navigate to Authentication → URL Configuration.
- Set the Site URL to your deployed application's URL (e.g.,
https://your-app.netlify.app). - Add your URL to the Redirect URLs list.
Site URL: https://jolly-bonbon-524548.netlify.app
Redirect URLs: https://jolly-bonbon-524548.netlify.app/**
Sources: supabase/setup-instructions.md:50-54, DEPLOY-NETLIFY.md:41-49
Demo Users
The project uses a set of predefined demo users for testing and demonstration. These can be created manually in the Authentication → Users section of the dashboard or automatically via the setup script.
| Role | Password | Metadata | |
|---|---|---|---|
| Admin | i.pineda@elmec.com.mx | Elmec2024!Admin | { "rol": "admin" } |
| Agent | j.gonzalez@elmec.com.mx | Elmec2024!Agent | { "rol": "agent" } |
| Customer | cliente@gmail.com | Elmec2024!Client | { "rol": "customer" } |
For demo purposes, Email confirmation should be disabled in the authentication settings.
Sources: supabase/setup-instructions.md:56-68, scripts/setup-supabase-complete.js:43-85
5. Storage Setup
The application uses Supabase Storage for handling file uploads, such as attachments for requests. This requires creating a bucket and setting appropriate access policies.
Bucket Creation
A storage bucket named request-files must be created.
- Navigate to the Storage section in the Supabase dashboard.
- Click "Create a new bucket".
- Set the name to
request-files. - The bucket can be set to public or private, but access is ultimately controlled by RLS policies. The documentation suggests setting it as private and using policies to grant access.
- Set a file size limit, for example, 5MB.
Sources: RESUMEN_CONFIGURACION.md:28-32, TEST_REPORT.md:213-219
Storage RLS Policies
Row Level Security (RLS) policies must be applied to the storage.objects table to control who can view, upload, update, and delete files.
| Policy Action | Description | Target Role | SQL Snippet |
|---|---|---|---|
| SELECT | Allows public read access to files in the bucket. | public / authenticated | CREATE POLICY "Public read access" ON storage.objects FOR SELECT USING (bucket_id = 'request-files'); |
| INSERT | Allows any authenticated user to upload files. | authenticated | CREATE POLICY "Users can upload files" ON storage.objects FOR INSERT TO authenticated WITH CHECK (bucket_id = 'request-files'); |
| UPDATE | Allows users to update their own files. | authenticated | CREATE POLICY "Users can update own files" ON storage.objects FOR UPDATE USING (owner = auth.uid()); |
| DELETE | Allows users to delete their own files. | authenticated | CREATE POLICY "Users can delete their files" ON storage.objects FOR DELETE USING (owner = auth.uid()); |
Sources: RESUMEN_CONFIGURACION.md:38-51, TEST_REPORT.md:221-237
6. Automated Setup Script
The project includes an automated setup script, scripts/setup-supabase-complete.js, to simplify the configuration process. This script performs several key actions.
Script Workflow
The diagram below shows the sequence of operations performed by the script.
This flowchart illustrates the execution path of the setup-supabase-complete.js script.
Sources: scripts/setup-supabase-complete.js
Key Functions
step1_testConnection(): Verifies that the Supabase URL and keys are correct and that a connection can be established.step2_executeMigrations(): Attempts to run the SQL migration files. The script notes that this step may fail and might need to be done manually via the SQL Editor.step3_createUsers(): Iterates through a predefined list of demo users and creates them using thesupabase.auth.admin.createUserfunction. It skips users that already exist.step4_verifySetup(): Checks for the existence of the required tables and lists the users present in theauth.userstable to confirm the setup.
To run the script, you need Node.js and the required environment variables (EXPO_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY) set in a .env.local file.
node scripts/setup-supabase-complete.js