import { drizzle } from 'drizzle-orm/node-postgres'; import { Client } from 'pg'; import * as schema from '../drizzle/schema'; import { sql } from 'drizzle-orm'; import * as dotenv from 'dotenv'; dotenv.config(); const client = new Client({ connectionString: process.env.PG_CONNECTION, }); async function main() { await client.connect(); const db = drizzle(client, { schema }); const testEmail = 'knuth.timo@gmail.com'; const targetEmail = 'target.user@example.com'; console.log('--- Starting Debug Script ---'); // 1. Simulate finding a user to favorite (using a dummy or existing one) // For safety, let's just query existing users to see if any have favorites set const usersWithFavorites = await db.select({ id: schema.users_json.id, email: schema.users_json.email, favorites: sql`${schema.users_json.data}->'favoritesForUser'` }).from(schema.users_json); console.log(`Found ${usersWithFavorites.length} users.`); const usersWithAnyFavorites = usersWithFavorites.filter(u => u.favorites !== null); console.log(`Users with 'favoritesForUser' field:`, JSON.stringify(usersWithAnyFavorites, null, 2)); // 2. Test the specific WHERE clause used in the service // .where(sql`${schema.users_json.data}->'favoritesForUser' @> ${JSON.stringify([user.email])}::jsonb`); console.log(`Testing query for email: ${testEmail}`); try { const result = await db .select({ id: schema.users_json.id, email: schema.users_json.email }) .from(schema.users_json) .where(sql`${schema.users_json.data}->'favoritesForUser' @> ${JSON.stringify([testEmail])}::jsonb`); console.log('Query Result:', result); } catch (e) { console.error('Query Failed:', e); } await client.end(); } main().catch(console.error); //test