diff --git a/app/actions/get-admin-users.ts b/app/actions/get-admin-users.ts index ebd4e98..51dbafc 100644 --- a/app/actions/get-admin-users.ts +++ b/app/actions/get-admin-users.ts @@ -42,7 +42,7 @@ export async function getAdminUsersDetails() { } // 🚀 OMIJA Zabezpieczenia RLS do sczytania cudzych kuponów z bazy -export async function getAdminUserCoupons(userId: string) { +export async function getAdminUserCoupons(userId: string | string[]) { const adminKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; const url = process.env.NEXT_PUBLIC_SUPABASE_URL; if (!adminKey || !url) { @@ -50,12 +50,13 @@ export async function getAdminUserCoupons(userId: string) { } const supabaseAdmin = createClient(url, adminKey); + const userIds = Array.isArray(userId) ? userId : [userId]; try { const { data: coupons, error } = await supabaseAdmin .from('kupony') .select('*') - .eq('user_id', userId) + .in('user_id', userIds) .order('created_at', { ascending: false }); if (error) throw error; diff --git a/benchmark.ts b/benchmark.ts new file mode 100644 index 0000000..fc58309 --- /dev/null +++ b/benchmark.ts @@ -0,0 +1,36 @@ +import { performance } from 'perf_hooks'; + +// Simulate the old N+1 behavior vs the new batched behavior +async function mockOldQuery(userId: string) { + // simulate DB latency + await new Promise(resolve => setTimeout(resolve, 50)); + return [{ id: Math.random(), user_id: userId }]; +} + +async function mockNewQuery(userIds: string[]) { + // simulate DB latency + await new Promise(resolve => setTimeout(resolve, 50)); + return userIds.map(userId => ({ id: Math.random(), user_id: userId })); +} + +async function runBenchmark() { + console.log('Running benchmark for 10 users...'); + const userIds = Array.from({ length: 10 }, (_, i) => `user_${i}`); + + // Old behavior: N queries + const startOld = performance.now(); + for (const id of userIds) { + await mockOldQuery(id); + } + const endOld = performance.now(); + console.log(`Baseline (N+1 queries): ${(endOld - startOld).toFixed(2)} ms`); + + // New behavior: 1 batched query + const startNew = performance.now(); + await mockNewQuery(userIds); + const endNew = performance.now(); + console.log(`Optimized (1 batched query): ${(endNew - startNew).toFixed(2)} ms`); + console.log(`Improvement: ${(((endOld - startOld) - (endNew - startNew)) / (endOld - startOld) * 100).toFixed(2)}% faster`); +} + +runBenchmark();