import { createClient } from '@supabase/supabase-js'; import { existsSync } from 'fs'; import * as path from 'path'; import * as dotenv from 'dotenv'; // Load environment variables from .env.local const envLocalPath = path.resolve(process.cwd(), '.env.local'); if (existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); // Try loading from default .env } // Get Supabase credentials from environment variables const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || ''; const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ''; if (!supabaseUrl || !supabaseServiceKey || !supabaseAnonKey) { console.error('Missing Supabase environment variables'); process.exit(1); } // Create a service role client to bypass RLS policies const serviceClient = createClient(supabaseUrl, supabaseServiceKey, { auth: { persistSession: false, }, }); // Create a regular anon client like the API would use const regularClient = createClient(supabaseUrl, supabaseAnonKey, { auth: { persistSession: false, }, }); async function main() { console.log('Debugging Supabase blog queries...'); // 1. Test service client query - this should always work console.log('\n1. Testing service client (admin access)...'); try { console.log('Querying all blog posts with service key...'); const { data: allPosts, error: allError } = await serviceClient .from('blog_posts') .select('id, title, slug, published') .order('created_at', { ascending: false }); if (allError) { console.error('Error querying all posts:', allError); } else { console.log(`Found ${allPosts.length} total blog posts with service key`); console.log('Sample posts:'); allPosts.slice(0, 3).forEach((post, i) => { console.log(`${i+1}. ${post.title} (${post.slug}) - Published: ${post.published}`); }); } // Check published posts with service key console.log('\nQuerying published blog posts with service key...'); const { data: publishedPosts, error: pubError } = await serviceClient .from('blog_posts') .select('id, title, slug, published') .eq('published', true) .order('created_at', { ascending: false }); if (pubError) { console.error('Error querying published posts:', pubError); } else { console.log(`Found ${publishedPosts.length} published blog posts with service key`); console.log('Published posts:'); publishedPosts.forEach((post, i) => { console.log(`${i+1}. ${post.title} (${post.slug})`); }); } } catch (error) { console.error('Error with service client:', error); } // 2. Test regular client query - this might be affected by RLS console.log('\n2. Testing regular client (user access like API would use)...'); try { console.log('Querying blog posts with anon key...'); const { data: regularPosts, error: regularError } = await regularClient .from('blog_posts') .select('id, title, slug, published') .order('created_at', { ascending: false }); if (regularError) { console.error('Error querying with regular client:', regularError); } else { console.log(`Found ${regularPosts.length} total blog posts with anon key`); console.log('Sample posts:'); regularPosts.slice(0, 3).forEach((post, i) => { console.log(`${i+1}. ${post.title} (${post.slug}) - Published: ${post.published}`); }); } // Check published posts with regular key console.log('\nQuerying published blog posts with anon key...'); const { data: regularPublished, error: regPubError } = await regularClient .from('blog_posts') .select('id, title, slug, published') .eq('published', true) .order('created_at', { ascending: false }); if (regPubError) { console.error('Error querying published posts with anon key:', regPubError); } else { console.log(`Found ${regularPublished.length} published blog posts with anon key`); console.log('Published posts:'); regularPublished.forEach((post, i) => { console.log(`${i+1}. ${post.title} (${post.slug})`); }); } } catch (error) { console.error('Error with regular client:', error); } // 3. Check for specific post (hello-world) console.log('\n3. Checking for specific blog post...'); try { // With service key const { data: helloPostAdmin, error: helloErrorAdmin } = await serviceClient .from('blog_posts') .select('id, title, slug, published, content') .eq('slug', 'hello-world') .single(); if (helloErrorAdmin) { console.error('Error fetching hello-world with service key:', helloErrorAdmin); } else { console.log('Found hello-world post with service key:', { title: helloPostAdmin.title, published: helloPostAdmin.published, contentLength: helloPostAdmin.content.length }); } // With anon key const { data: helloPostAnon, error: helloErrorAnon } = await regularClient .from('blog_posts') .select('id, title, slug, published') .eq('slug', 'hello-world') .single(); if (helloErrorAnon) { console.error('Error fetching hello-world with anon key:', helloErrorAnon); } else { console.log('Found hello-world post with anon key:', { title: helloPostAnon.title, published: helloPostAnon.published }); } } catch (error) { console.error('Error checking hello-world post:', error); } console.log('\nAnalysis:'); console.log('- If service client returns posts but regular client does not: Row Level Security (RLS) issue'); console.log('- If both clients return posts but API doesn\'t: API implementation issue'); console.log('- If neither client returns posts: Database connection or schema issue'); } main();