fix: lazy-init Supabase client to prevent crash when env vars missing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Christian Vidal Wolf
2026-02-20 13:23:29 +01:00
co-authored by Claude Opus 4.6
parent 2bce0a839b
commit 16f7af7df3
+19 -7
View File
@@ -1,10 +1,20 @@
import { createClient } from '@supabase/supabase-js';
import { createClient, SupabaseClient } from '@supabase/supabase-js';
const supabaseUrl = process.env.SUPABASE_URL || '';
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY || '';
export const supabase = createClient(supabaseUrl, supabaseAnonKey);
let _client: SupabaseClient | null = null;
const getClient = (): SupabaseClient => {
if (!_client) {
if (!supabaseUrl || !supabaseAnonKey) {
throw new Error('Supabase credentials not configured. Add SUPABASE_URL and SUPABASE_ANON_KEY to environment variables.');
}
_client = createClient(supabaseUrl, supabaseAnonKey);
}
return _client;
};
export interface VendorDailyRow {
id?: number;
@@ -40,7 +50,7 @@ export const fetchVendorData = async (filters: VendorFilters): Promise<VendorDai
let hasMore = true;
while (hasMore) {
let query = supabase
let query = getClient()
.from('vendor_daily_data')
.select('*')
.order('date', { ascending: true })
@@ -81,21 +91,23 @@ export const fetchVendorFilterOptions = async (): Promise<{
tags: string[];
dateRange: { min: string; max: string } | null;
}> => {
const { data: marketData } = await supabase
const client = getClient();
const { data: marketData } = await client
.from('vendor_daily_data')
.select('market');
const { data: tagData } = await supabase
const { data: tagData } = await client
.from('vendor_daily_data')
.select('tags');
const { data: dateMin } = await supabase
const { data: dateMin } = await client
.from('vendor_daily_data')
.select('date')
.order('date', { ascending: true })
.limit(1);
const { data: dateMax } = await supabase
const { data: dateMax } = await client
.from('vendor_daily_data')
.select('date')
.order('date', { ascending: false })