···11-import { neon, NeonQueryFunction } from '@neondatabase/serverless';
11+import { neon, NeonQueryFunction } from "@neondatabase/serverless";
2233let sql: NeonQueryFunction<any, any> | undefined = undefined;
44let connectionInitialized = false;
···1414export async function initDB() {
1515 const sql = getDbClient();
16161717- console.log('🧠 Connecting to DB:', process.env.NETLIFY_DATABASE_URL);
1717+ console.log("🧠 Connecting to DB:", process.env.NETLIFY_DATABASE_URL);
18181919 try {
2020- const res: any = await sql`SELECT current_database() AS db, current_user AS user, NOW() AS now`;
2121- console.log('✅ Connected:', res[0]);
2020+ const res: any =
2121+ await sql`SELECT current_database() AS db, current_user AS user, NOW() AS now`;
2222+ console.log("✅ Connected:", res[0]);
2223 } catch (e) {
2323- console.error('❌ Connection failed:', e);
2424+ console.error("❌ Connection failed:", e);
2425 throw e;
2526 }
2627···143144 `;
144145145146 // ==================== ENHANCED INDEXES FOR PHASE 2 ====================
146146-147147+147148 // Existing indexes
148149 await sql`CREATE INDEX IF NOT EXISTS idx_source_accounts_to_check ON source_accounts(source_platform, match_found, last_checked)`;
149150 await sql`CREATE INDEX IF NOT EXISTS idx_source_accounts_platform ON source_accounts(source_platform)`;
···159160160161 // For sorting
161162 await sql`CREATE INDEX IF NOT EXISTS idx_atproto_matches_stats ON atproto_matches(source_account_id, found_at DESC, post_count DESC, follower_count DESC)`;
162162-163163+163164 // For session lookups (most frequent query)
164164-await sql`CREATE INDEX IF NOT EXISTS idx_user_sessions_did ON user_sessions(did)`;
165165-await sql`CREATE INDEX IF NOT EXISTS idx_user_sessions_expires ON user_sessions(expires_at)`;
166166-165165+ await sql`CREATE INDEX IF NOT EXISTS idx_user_sessions_did ON user_sessions(did)`;
166166+ await sql`CREATE INDEX IF NOT EXISTS idx_user_sessions_expires ON user_sessions(expires_at)`;
167167+167168 // For OAuth state/session cleanup
168169 await sql`CREATE INDEX IF NOT EXISTS idx_oauth_states_expires ON oauth_states(expires_at)`;
169170 await sql`CREATE INDEX IF NOT EXISTS idx_oauth_sessions_expires ON oauth_sessions(expires_at)`;
170170-171171+171172 // For upload queries by user
172173 await sql`CREATE INDEX IF NOT EXISTS idx_user_uploads_did_created ON user_uploads(did, created_at DESC)`;
173173-174174+174175 // For upload details pagination (composite index for ORDER BY + JOIN)
175176 await sql`CREATE INDEX IF NOT EXISTS idx_user_source_follows_upload_created ON user_source_follows(upload_id, source_account_id)`;
176176-177177+177178 // For match status queries
178179 await sql`CREATE INDEX IF NOT EXISTS idx_user_match_status_match_id ON user_match_status(atproto_match_id)`;
179179-180180+180181 // Composite index for the common join pattern in get-upload-details
181182 await sql`CREATE INDEX IF NOT EXISTS idx_atproto_matches_source_active ON atproto_matches(source_account_id, is_active) WHERE is_active = true`;
182182-183183+183184 // For bulk operations - normalized username lookups
184185 await sql`CREATE INDEX IF NOT EXISTS idx_source_accounts_normalized ON source_accounts(normalized_username, source_platform)`;
185186186186- console.log('✅ Database indexes created/verified');
187187+ console.log("✅ Database indexes created/verified");
187188}
188189189190export async function cleanupExpiredSessions() {
190191 const sql = getDbClient();
191191-192192+192193 // Use indexes for efficient cleanup
193193- const statesDeleted = await sql`DELETE FROM oauth_states WHERE expires_at < NOW()`;
194194- const sessionsDeleted = await sql`DELETE FROM oauth_sessions WHERE expires_at < NOW()`;
195195- const userSessionsDeleted = await sql`DELETE FROM user_sessions WHERE expires_at < NOW()`;
196196-197197- console.log('🧹 Cleanup:', {
194194+ const statesDeleted =
195195+ await sql`DELETE FROM oauth_states WHERE expires_at < NOW()`;
196196+ const sessionsDeleted =
197197+ await sql`DELETE FROM oauth_sessions WHERE expires_at < NOW()`;
198198+ const userSessionsDeleted =
199199+ await sql`DELETE FROM user_sessions WHERE expires_at < NOW()`;
200200+201201+ console.log("🧹 Cleanup:", {
198202 states: (statesDeleted as any).length,
199203 sessions: (sessionsDeleted as any).length,
200200- userSessions: (userSessionsDeleted as any).length
204204+ userSessions: (userSessionsDeleted as any).length,
201205 });
202206}
203207204204-export { getDbClient as sql };208208+export { getDbClient as sql };