fix(supabase): lazy client init — validates env vars at first use, not at import time

This commit is contained in:
admin-valentin 2026-07-31 14:44:00 +00:00
parent c7e09ed647
commit 97c32222ce

View file

@ -1,6 +1,20 @@
import { createClient } from '@supabase/supabase-js';
import { createClient, type SupabaseClient } from '@supabase/supabase-js';
export const supabaseAdmin = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
);
let _client: SupabaseClient | null = null;
function getClient(): SupabaseClient {
if (_client) return _client;
const url = process.env.SUPABASE_URL;
const key = process.env.SUPABASE_SERVICE_ROLE_KEY;
if (!url || !key) {
throw new Error('SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY must be set before using supabaseAdmin');
}
_client = createClient(url, key);
return _client;
}
export const supabaseAdmin = new Proxy({} as SupabaseClient, {
get(_target, prop) {
return (getClient() as Record<string | symbol, unknown>)[prop];
},
});