import { createClient } from '@supabase/supabase-js'; import * as dotenv from 'dotenv'; import * as path from 'path'; import * as fs from 'fs'; // Load environment variables const envLocalPath = path.resolve(process.cwd(), '../.env.local'); if (fs.existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); } async function checkTools() { const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!supabaseUrl || !supabaseKey) { console.error('Missing Supabase environment variables'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseKey); // Try to find the tools by ID explicitly const toolIds = [614, 615, 616, 621, 632, 637]; console.log('Checking for tools referenced in the junction table for category 61:'); for (const id of toolIds) { const { data, error } = await supabase .from('tools') .select('id, name, published') .eq('id', id); if (error || !data || data.length === 0) { console.log(`Tool ID ${id}: Not found or error: ${error?.message || 'No data'}`); } else { console.log(`Tool ID ${id}: Found - ${data[0].name} (Published: ${data[0].published})`); } } // Get count of all tools const { count, error: countError } = await supabase .from('tools') .select('*', { count: 'exact', head: true }); if (countError) { console.error('Error counting tools:', countError); } else { console.log(`\nTotal tools in database: ${count}`); } // Get min and max tool IDs to see the range const { data: minMaxData, error: minMaxError } = await supabase .from('tools') .select('id') .order('id', { ascending: true }); if (minMaxError || !minMaxData || minMaxData.length === 0) { console.error('Error getting min/max tool IDs'); } else { const minId = minMaxData[0].id; const maxId = minMaxData[minMaxData.length - 1].id; console.log(`Tool ID range: ${minId} to ${maxId}`); } // Check for orphaned entries in the junction table console.log('\nLooking for orphaned entries in tools_categories:'); const { data: allJunctions, error: junctionError } = await supabase .from('tools_categories') .select('*') .limit(100); if (junctionError) { console.error('Error fetching junction entries:', junctionError); } else if (allJunctions) { let orphanedCount = 0; for (const junction of allJunctions.slice(0, 20)) { // Check just the first 20 to avoid too many queries const { data: toolData } = await supabase .from('tools') .select('id') .eq('id', junction.tool_id) .single(); if (!toolData) { orphanedCount++; console.log(`Orphaned entry: tool_id=${junction.tool_id}, category_id=${junction.category_id}`); } } console.log(`Found ${orphanedCount} orphaned entries out of ${Math.min(20, allJunctions.length)} checked`); } } checkTools().catch(console.error);