fix(supabase): lazy client init — validates env vars at first use, not at import time
This commit is contained in:
parent
c7e09ed647
commit
97c32222ce
1 changed files with 19 additions and 5 deletions
|
|
@ -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];
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue