const { createClient } = require('@supabase/supabase-js'); const fs = require('fs'); const path = require('path'); // Load environment variables require('dotenv').config(); const supabaseUrl = process.env.SUPABASE_URL; const supabaseServiceKey = process.env.SUPABASE_SERVICE_KEY; if (!supabaseUrl || !supabaseServiceKey) { console.error('โŒ Missing Supabase credentials'); console.error('Make sure SUPABASE_URL and SUPABASE_SERVICE_KEY are set in .env'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseServiceKey); async function setupVectorDatabase() { try { console.log('๐Ÿš€ Setting up Supabase vector database...'); // Read the SQL setup script const sqlScript = fs.readFileSync(path.join(__dirname, 'supabase_vector_setup.sql'), 'utf8'); // Split the script into individual statements const statements = sqlScript .split(';') .map(stmt => stmt.trim()) .filter(stmt => stmt.length > 0 && !stmt.startsWith('--')); console.log(`๐Ÿ“ Executing ${statements.length} SQL statements...`); // Execute each statement for (let i = 0; i < statements.length; i++) { const statement = statements[i]; if (statement.trim()) { console.log(` Executing statement ${i + 1}/${statements.length}...`); const { data, error } = await supabase.rpc('exec_sql', { sql: statement }); if (error) { console.error(`โŒ Error executing statement ${i + 1}:`, error); // Don't exit, continue with other statements } else { console.log(` โœ… Statement ${i + 1} executed successfully`); } } } // Test the setup by checking if the table exists console.log('๐Ÿ” Verifying table structure...'); const { data: columns, error: tableError } = await supabase .from('document_chunks') .select('*') .limit(0); if (tableError) { console.error('โŒ Error verifying table:', tableError); } else { console.log('โœ… document_chunks table verified successfully'); } // Test the search function console.log('๐Ÿ” Testing vector search function...'); const testEmbedding = new Array(1536).fill(0.1); // Test embedding const { data: searchResult, error: searchError } = await supabase .rpc('match_document_chunks', { query_embedding: testEmbedding, match_threshold: 0.5, match_count: 5 }); if (searchError) { console.error('โŒ Error testing search function:', searchError); } else { console.log('โœ… Vector search function working correctly'); console.log(` Found ${searchResult ? searchResult.length : 0} results`); } console.log('๐ŸŽ‰ Supabase vector database setup completed successfully!'); } catch (error) { console.error('โŒ Setup failed:', error); process.exit(1); } } // Alternative approach using direct SQL execution async function setupVectorDatabaseDirect() { try { console.log('๐Ÿš€ Setting up Supabase vector database (direct approach)...'); // First, enable vector extension console.log('๐Ÿ“ฆ Enabling pgvector extension...'); const { error: extError } = await supabase.rpc('exec_sql', { sql: 'CREATE EXTENSION IF NOT EXISTS vector;' }); if (extError) { console.log('โš ๏ธ Extension error (might already exist):', extError.message); } // Create the table console.log('๐Ÿ—๏ธ Creating document_chunks table...'); const createTableSQL = ` CREATE TABLE IF NOT EXISTS document_chunks ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, document_id TEXT NOT NULL, content TEXT NOT NULL, embedding VECTOR(1536), metadata JSONB DEFAULT '{}', chunk_index INTEGER NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); `; const { error: tableError } = await supabase.rpc('exec_sql', { sql: createTableSQL }); if (tableError) { console.error('โŒ Error creating table:', tableError); } else { console.log('โœ… Table created successfully'); } // Test simple insert and select console.log('๐Ÿงช Testing basic operations...'); const { data, error } = await supabase .from('document_chunks') .select('count', { count: 'exact' }); if (error) { console.error('โŒ Error testing table:', error); } else { console.log('โœ… Table is accessible'); } console.log('๐ŸŽ‰ Basic vector database setup completed!'); } catch (error) { console.error('โŒ Setup failed:', error); } } // Run the setup setupVectorDatabaseDirect();