mcp-ts-template
Version:
A production-grade TypeScript template for building robust Model Context Protocol (MCP) servers, featuring built-in observability with OpenTelemetry, advanced error handling, comprehensive utilities, and a modular architecture.
68 lines • 2.77 kB
JavaScript
/**
* @fileoverview Initializes and exports a singleton Supabase client instance.
* This module ensures that the Supabase client is initialized once and shared
* across the application, using credentials from the central configuration.
* It handles both the standard client and the admin client (using the service role key).
*
* @module src/services/supabase/supabaseClient
*/
import { createClient } from "@supabase/supabase-js";
import { config } from "../../config/index.js";
import { BaseErrorCode, McpError } from "../../types-global/errors.js";
import { logger, requestContextService } from "../../utils/index.js";
let supabase = null;
let supabaseAdmin = null;
const initializeSupabase = () => {
const context = requestContextService.createRequestContext({
operation: "initializeSupabase",
});
if (config.supabase?.url && config.supabase?.anonKey) {
if (!supabase) {
supabase = createClient(config.supabase.url, config.supabase.anonKey, {
auth: {
persistSession: false,
autoRefreshToken: false,
},
});
logger.info("Supabase client initialized.", context);
}
if (!supabaseAdmin && config.supabase.serviceRoleKey) {
supabaseAdmin = createClient(config.supabase.url, config.supabase.serviceRoleKey, {
auth: {
persistSession: false,
autoRefreshToken: false,
},
});
logger.info("Supabase admin client initialized.", context);
}
}
else {
logger.warning("Supabase URL or anon key is missing. Supabase clients not initialized.", context);
}
};
// Initialize on load
initializeSupabase();
/**
* Returns the singleton Supabase client instance.
* Throws an McpError if the client is not initialized.
* @returns The Supabase client.
*/
export const getSupabaseClient = () => {
if (!supabase) {
throw new McpError(BaseErrorCode.SERVICE_NOT_INITIALIZED, "Supabase client has not been initialized. Please check your SUPABASE_URL and SUPABASE_ANON_KEY environment variables.");
}
return supabase;
};
/**
* Returns the singleton Supabase admin client instance.
* This client uses the service role key and bypasses RLS.
* Throws an McpError if the admin client is not initialized.
* @returns The Supabase admin client.
*/
export const getSupabaseAdminClient = () => {
if (!supabaseAdmin) {
throw new McpError(BaseErrorCode.SERVICE_NOT_INITIALIZED, "Supabase admin client has not been initialized. Please check your SUPABASE_SERVICE_ROLE_KEY environment variable.");
}
return supabaseAdmin;
};
//# sourceMappingURL=supabaseClient.js.map