mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 07:51:13 -05:00
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
/**
|
|
* Type-safe Supabase query helpers
|
|
*
|
|
* Provides type-safe table query builders to eliminate `as any` assertions.
|
|
*/
|
|
|
|
import { supabase } from '@/lib/supabaseClient';
|
|
import type { Database } from '@/integrations/supabase/types';
|
|
|
|
// Define valid table names from the database schema
|
|
export type TableName = keyof Database['public']['Tables'];
|
|
|
|
/**
|
|
* Create a type-safe query builder for a specific table
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* const query = createTableQuery('parks').select('*').eq('slug', 'disneyland');
|
|
* const query2 = createTableQuery('rides').select('id, name').eq('status', 'operating');
|
|
* ```
|
|
*/
|
|
export function createTableQuery<T extends TableName>(tableName: T) {
|
|
return supabase.from(tableName);
|
|
}
|
|
|
|
/**
|
|
* Dynamically query a table by name with type safety
|
|
*
|
|
* Use this when the table name is determined at runtime (e.g., version tables).
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* const versions = await queryTable('park_versions', (q) =>
|
|
* q.select('*').eq('park_id', parkId)
|
|
* );
|
|
* ```
|
|
*/
|
|
export async function queryTable<T extends TableName>(
|
|
tableName: T,
|
|
queryBuilder: (query: ReturnType<typeof createTableQuery<T>>) => any
|
|
) {
|
|
const query = createTableQuery(tableName);
|
|
return queryBuilder(query);
|
|
}
|
|
|
|
/**
|
|
* Type-safe interface for processed uploaded images
|
|
*/
|
|
export interface ProcessedImage {
|
|
url: string;
|
|
cloudflare_id: string;
|
|
order: number;
|
|
title?: string;
|
|
caption?: string;
|
|
}
|