Batch the author lookups on the orders page

Node · Node · intermediate · modification

Replaces the N+1 author lookups with one `WHERE id = ANY($1)` query: collect the distinct author ids, fetch them in a single round-trip, and join in memory through a Map. Deleted authors come back as null per the contract, and an empty page short-circuits before the pool.

Requirements

Files touched

--- src/orders/attachAuthors.js
 export async function attachAuthors(pool, orders) {
-  const result = [];
-  for (const order of orders) {
-    const { rows } = await pool.query(
-      'SELECT id, name, email FROM users WHERE id = $1',
-      [order.authorId],
-    );
-    result.push({ ...order, author: rows[0] ?? null });
+  if (orders.length === 0) {
+    return [];
   }
-  return result;
+  const authorIds = [...new Set(orders.map((order) => order.authorId))];
+  const { rows } = await pool.query(
+    'SELECT id, name, email FROM users WHERE id = ANY($1)',
+    [authorIds],
+  );
+  const authorsById = new Map(rows.map((row) => [row.id, row]));
+  return orders.map((order) => ({
+    ...order,
+    author: authorsById.get(order.authorId) ?? null,
+  }));
 }
 

Review this PR

Node practice