mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 13:45:23 +02:00
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:
co-authored by
Claude Opus 4.6
parent
2bce0a839b
commit
16f7af7df3
+19
-7
@@ -1,10 +1,20 @@
|
|||||||
|
|
||||||
import { createClient } from '@supabase/supabase-js';
|
import { createClient, SupabaseClient } from '@supabase/supabase-js';
|
||||||
|
|
||||||
const supabaseUrl = process.env.SUPABASE_URL || '';
|
const supabaseUrl = process.env.SUPABASE_URL || '';
|
||||||
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY || '';
|
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 {
|
export interface VendorDailyRow {
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -40,7 +50,7 @@ export const fetchVendorData = async (filters: VendorFilters): Promise<VendorDai
|
|||||||
let hasMore = true;
|
let hasMore = true;
|
||||||
|
|
||||||
while (hasMore) {
|
while (hasMore) {
|
||||||
let query = supabase
|
let query = getClient()
|
||||||
.from('vendor_daily_data')
|
.from('vendor_daily_data')
|
||||||
.select('*')
|
.select('*')
|
||||||
.order('date', { ascending: true })
|
.order('date', { ascending: true })
|
||||||
@@ -81,21 +91,23 @@ export const fetchVendorFilterOptions = async (): Promise<{
|
|||||||
tags: string[];
|
tags: string[];
|
||||||
dateRange: { min: string; max: string } | null;
|
dateRange: { min: string; max: string } | null;
|
||||||
}> => {
|
}> => {
|
||||||
const { data: marketData } = await supabase
|
const client = getClient();
|
||||||
|
|
||||||
|
const { data: marketData } = await client
|
||||||
.from('vendor_daily_data')
|
.from('vendor_daily_data')
|
||||||
.select('market');
|
.select('market');
|
||||||
|
|
||||||
const { data: tagData } = await supabase
|
const { data: tagData } = await client
|
||||||
.from('vendor_daily_data')
|
.from('vendor_daily_data')
|
||||||
.select('tags');
|
.select('tags');
|
||||||
|
|
||||||
const { data: dateMin } = await supabase
|
const { data: dateMin } = await client
|
||||||
.from('vendor_daily_data')
|
.from('vendor_daily_data')
|
||||||
.select('date')
|
.select('date')
|
||||||
.order('date', { ascending: true })
|
.order('date', { ascending: true })
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
const { data: dateMax } = await supabase
|
const { data: dateMax } = await client
|
||||||
.from('vendor_daily_data')
|
.from('vendor_daily_data')
|
||||||
.select('date')
|
.select('date')
|
||||||
.order('date', { ascending: false })
|
.order('date', { ascending: false })
|
||||||
|
|||||||
Reference in New Issue
Block a user