Make Supabase client lazy

This commit is contained in:
MOHAN 2026-07-22 18:31:32 +05:30
parent 4d9d3aed8a
commit 3163c9018f

View File

@ -3,9 +3,16 @@ import { createClient, SupabaseClient } from "@supabase/supabase-js";
@Injectable() @Injectable()
export class SupabaseService { export class SupabaseService {
private readonly client: SupabaseClient; private client: SupabaseClient | null = null;
constructor() { getClient() {
if (!this.client) {
this.client = this.createClient();
}
return this.client;
}
private createClient() {
const url = this.requireEnv("SUPABASE_URL"); const url = this.requireEnv("SUPABASE_URL");
const serviceKey = const serviceKey =
process.env.SUPABASE_SERVICE_KEY ?? process.env.SUPABASE_ANON_KEY; process.env.SUPABASE_SERVICE_KEY ?? process.env.SUPABASE_ANON_KEY;
@ -14,15 +21,11 @@ export class SupabaseService {
"Missing SUPABASE_SERVICE_KEY (preferred) or SUPABASE_ANON_KEY environment variable." "Missing SUPABASE_SERVICE_KEY (preferred) or SUPABASE_ANON_KEY environment variable."
); );
} }
this.client = createClient(url, serviceKey, { return createClient(url, serviceKey, {
auth: { persistSession: false } auth: { persistSession: false }
}); });
} }
getClient() {
return this.client;
}
private requireEnv(name: string) { private requireEnv(name: string) {
const value = process.env[name]; const value = process.env[name];
if (!value) { if (!value) {