Skip to main content

Overview

The database.types.ts file contains all TypeScript type definitions for the database schema, ensuring type safety throughout the application. Location: src/types/database.types.ts

Base Types

UUID

type UUID = string;
Represents unique identifiers for database records.

Timestamp

type Timestamp = string;
ISO 8601 formatted timestamp strings.

Enum Types

EncuestaTipo

type EncuestaTipo = "opcion_multiple" | "abierta" | "calificacion";
Values:
  • opcion_multiple - Multiple choice surveys
  • abierta - Open-ended text responses
  • calificacion - Rating-based surveys

ReporteCategoria

type ReporteCategoria =
  | "alumbrado"      // Street lighting
  | "baches"         // Potholes
  | "limpieza"       // Cleaning
  | "agua"           // Water supply
  | "alcantarillado" // Sewage
  | "parques"        // Parks
  | "señalizacion"   // Signage
  | "seguridad"      // Security
  | "ruido"          // Noise
  | "otro";          // Other

ReporteEstado

type ReporteEstado =
  | "pendiente"      // Initial state
  | "en_revision"    // Admin evaluating
  | "en_proceso"     // Accepted and in progress
  | "resuelto"       // Successfully completed
  | "rechazado";     // Rejected or duplicate
States are simplified according to ISO/IEC 25010 (maximum 5 states for usability).

ReportePrioridad

type ReportePrioridad = "baja" | "media" | "alta" | "urgente";

UsuarioTipo

type UsuarioTipo = "ciudadano" | "administrador";

Table Interfaces

AdministradoresRow

interface AdministradoresRow {
  id: UUID;
  email: string;
  nombres: string;
  apellidos: string;
  cedula: string;           // Length = 10 (Ecuadorian ID)
  activo: boolean | null;
  created_at: Timestamp | null;
  updated_at: Timestamp | null;
}
Insert Type:
type AdministradoresInsert = Omit<AdministradoresRow, "created_at" | "updated_at" | "activo"> & {
  activo?: boolean;
  created_at?: Timestamp | null;
  updated_at?: Timestamp | null;
};
Update Type:
type AdministradoresUpdate = Partial<AdministradoresRow> & { id: UUID };

UsuariosRow

interface UsuariosRow {
  id: UUID;
  email: string;
  nombres: string;
  apellidos: string;
  cedula: string;           // Length = 10
  parroquia: string;
  barrio: string;
  tipo: UsuarioTipo;
  activo: boolean | null;
  created_at: Timestamp | null;
  updated_at: Timestamp | null;
}

EncuestasRow

interface EncuestasRow {
  id: UUID;
  titulo: string;
  descripcion: string;
  tipo: EncuestaTipo;
  opciones: any | null;     // JSONB field
  fecha_inicio: Timestamp;
  fecha_fin: Timestamp;
  activa: boolean | null;
  parroquia_destino: string | null;
  barrio_destino: string | null;
  created_at: Timestamp | null;
  updated_at: Timestamp | null;
}

RespuestasEncuestasRow

interface RespuestasEncuestasRow {
  id: UUID;
  encuesta_id: UUID;
  usuario_id: UUID;
  respuesta: any;           // JSONB field
  created_at: Timestamp | null;
}

NoticiasRow

interface NoticiasRow {
  id: UUID;
  titulo: string;
  contenido: string;
  imagen_url: string | null;
  parroquia_destino: string | null;
  barrio_destino: string | null;
  administrador_id: UUID;
  created_at: Timestamp | null;
  updated_at: Timestamp | null;
}

ReportesRow

interface ReportesRow {
  id: UUID;
  usuario_id: UUID;
  categoria: ReporteCategoria;
  descripcion: string;
  ubicacion_parroquia: string;
  ubicacion_barrio: string;
  ubicacion_direccion: string;
  ubicacion_lat: number | null;
  ubicacion_lng: number | null;
  estado: ReporteEstado;
  prioridad: ReportePrioridad;
  imagen_url: string | null;
  respuesta_admin: string | null;
  fecha_resolucion: Timestamp | null;
  created_at: Timestamp | null;
  updated_at: Timestamp | null;
}

Database Type

The Database interface provides complete type safety for Supabase operations:
interface Database {
  public: {
    Tables: {
      administradores: {
        Row: AdministradoresRow;
        Insert: AdministradoresInsert;
        Update: AdministradoresUpdate;
      };
      usuarios: {
        Row: UsuariosRow;
        Insert: UsuariosInsert;
        Update: UsuariosUpdate;
      };
      encuestas: {
        Row: EncuestasRow;
        Insert: EncuestasInsert;
        Update: EncuestasUpdate;
      };
      respuestas_encuestas: {
        Row: RespuestasEncuestasRow;
        Insert: RespuestasEncuestasInsert;
        Update: RespuestasEncuestasUpdate;
      };
      noticias: {
        Row: NoticiasRow;
        Insert: NoticiasInsert;
        Update: NoticiasUpdate;
      };
      reportes: {
        Row: ReportesRow;
        Insert: ReportesInsert;
        Update: ReportesUpdate;
      };
    };
    Views: Record<string, never>;
    Functions: Record<string, never>;
    Enums: Record<string, never>;
  };
}

Usage Example

import { createClient } from '@supabase/supabase-js';
import type { Database } from './types/database.types';

const supabase = createClient<Database>(
  process.env.VITE_SUPABASE_URL,
  process.env.VITE_SUPABASE_ANON_KEY
);

// Type-safe database operations
const { data, error } = await supabase
  .from('noticias')
  .select('*')
  .eq('activa', true);

// data is automatically typed as NoticiasRow[]

Type Pattern

Each table follows this pattern:
  • Row: Complete database record (read operations)
  • Insert: Fields required/optional for creating records
  • Update: Partial fields for updating records (requires id)
This ensures type safety for all CRUD operations.

Build docs developers (and LLMs) love