Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/lib/supabase/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ import { createClient } from "@supabase/supabase-js";
import { requireServerEnv } from "@/lib/env";
import type { Database } from "./database.types";

export function createAdminClient() {
const { NEXT_PUBLIC_SUPABASE_URL: url, SUPABASE_SERVICE_ROLE_KEY: key } = requireServerEnv();
// Cache the admin client as a module-level singleton so that every API request
// reuses the same instance (and its underlying HTTP agent) rather than allocating
// a fresh one on every call. Mirrors the openAIClient singleton in openai.ts.
// The service-role client carries no user-specific session state, so sharing it
// across requests is safe.
let adminClient: ReturnType<typeof createClient<Database>> | null = null;

return createClient<Database>(url, key, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
});
export function createAdminClient() {
if (!adminClient) {
const { NEXT_PUBLIC_SUPABASE_URL: url, SUPABASE_SERVICE_ROLE_KEY: key } = requireServerEnv();
adminClient = createClient<Database>(url, key, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
});
}
return adminClient;
}
Loading