From 3163c9018f34c4ea8dd66d91a8792f958322d99a Mon Sep 17 00:00:00 2001 From: MOHAN Date: Wed, 22 Jul 2026 18:31:32 +0530 Subject: [PATCH] Make Supabase client lazy --- src/supabase/supabase.service.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/supabase/supabase.service.ts b/src/supabase/supabase.service.ts index a71f9af..cb4648d 100644 --- a/src/supabase/supabase.service.ts +++ b/src/supabase/supabase.service.ts @@ -3,9 +3,16 @@ import { createClient, SupabaseClient } from "@supabase/supabase-js"; @Injectable() 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 serviceKey = 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." ); } - this.client = createClient(url, serviceKey, { + return createClient(url, serviceKey, { auth: { persistSession: false } }); } - getClient() { - return this.client; - } - private requireEnv(name: string) { const value = process.env[name]; if (!value) {