/**
* Quick script to check forum posts in Supabase
* Run with: node check-posts.js
*/
const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
// Read .env file manually (handle Windows line endings)
const envContent = fs.readFileSync('.env', 'utf-8');
const envVars = {};
envContent.split(/\r?\n/).forEach(line => {
// Skip comments and empty lines
if (line.trim().startsWith('#') || !line.trim()) return;
const match = line.match(/^([^=]+)=(.*)$/);
if (match) {
envVars[match[1].trim()] = match[2].trim().replace(/^["']|["']$/g, '');
}
});
const supabaseUrl = envVars.NEXT_PUBLIC_SUPABASE_URL;
const serviceRoleKey = envVars.SUPABASE_SERVICE_ROLE_KEY;
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing environment variables:');
console.error('NEXT_PUBLIC_SUPABASE_URL:', supabaseUrl ? '✓' : '✗');
console.error('SUPABASE_SERVICE_ROLE_KEY:', serviceRoleKey ? '✓' : '✗');
process.exit(1);
}
const supabase = createClient(supabaseUrl, serviceRoleKey, {
auth: { autoRefreshToken: false, persistSession: false }
});
async function checkPosts() {
console.log('\n🔍 Checking forum_posts table...\n');
// Get all posts
const { data: allPosts, error: allError } = await supabase
.from('forum_posts')
.select('*')
.order('created_at', { ascending: false })
.limit(10);
if (allError) {
console.error('❌ Error fetching posts:', allError);
return;
}
console.log(`📊 Total posts found: ${allPosts?.length || 0}\n`);
if (allPosts && allPosts.length > 0) {
allPosts.forEach((post, index) => {
console.log(`\n--- Post ${index + 1} ---`);
console.log(`ID: ${post.id}`);
console.log(`Type: ${post.post_type}`);
console.log(`Bill: ${post.bill_session}/${post.bill_number}`);
console.log(`Author ID: ${post.author_id}`);
console.log(`Author Name: ${post.author_name}`);
console.log(`Title: ${post.title}`);
console.log(`Content: ${post.content.substring(0, 100)}...`);
console.log(`Depth: ${post.depth}`);
console.log(`Deleted: ${post.is_deleted}`);
console.log(`Created: ${post.created_at}`);
console.log(`Section: ${post.entity_metadata?.section_ref || 'general'}`);
});
} else {
console.log('❌ No posts found in the database.');
}
// Check user_profiles
console.log('\n\n🔍 Checking user_profiles table...\n');
const { data: profiles, error: profileError } = await supabase
.from('user_profiles')
.select('id, display_name')
.limit(5);
if (profileError) {
console.error('❌ Error fetching profiles:', profileError);
} else {
console.log(`📊 User profiles found: ${profiles?.length || 0}`);
profiles?.forEach(p => {
console.log(` - ${p.display_name} (${p.id})`);
});
}
}
checkPosts()
.then(() => {
console.log('\n✅ Check complete!');
process.exit(0);
})
.catch(err => {
console.error('\n❌ Error:', err);
process.exit(1);
});