From e90a567446f3713037ae8369180bc205557c24a8 Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Fri, 3 Apr 2026 11:17:04 +0200 Subject: [PATCH] feat: implement bulk PostgreSQL data fetching client to replace per-article HTTP requests --- src/lib/pg-ff-client.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/lib/pg-ff-client.ts b/src/lib/pg-ff-client.ts index 85b2c75..3bb5dd7 100644 --- a/src/lib/pg-ff-client.ts +++ b/src/lib/pg-ff-client.ts @@ -7,7 +7,7 @@ * Chaque fonction fait 1 seule requête filtrée par codefou — O(1) au lieu de O(N). */ -import { db, getPool } from "@/db"; +import { db } from "@/db"; import { sql, type SQL } from "drizzle-orm"; // Cache module-level pour éviter les requêtes information_schema répétées @@ -749,21 +749,22 @@ export interface StockBySite { /** * Retourne le stock actuel (stockdispo) par codein pour une liste de codeins. * 1 seule requête bulk — utilisé pour enrichir le Hit Parade. + * + * On passe les codeins comme une SEULE chaîne CSV ($1) et on utilise + * string_to_array côté PG — cela évite l'expansion Drizzle en $1,$2,...$N + * qui déclencherait le PG error 54011 (ROW > 1664 entries). */ export async function pgGetStockForCodeins(codeins: string[]): Promise> { if (codeins.length === 0) return new Map(); - // Bypass Drizzle entirely — it always expands arrays into individual params, - // hitting PG's 1664-entry ROW expression limit. Use the raw pg Pool directly - // with a native $1::text[] binding (entire array = single parameter). - const res = await getPool().query<{ codein: string; site: string; stockdispo: number }>( - `SELECT a.codein, cs.site, cs.stockdispo::float AS stockdispo - FROM cube_stock cs - JOIN articles a ON a.no_id = cs.artnoid - WHERE a.codein = ANY($1::text[])`, - [codeins] + const codeinsStr = codeins.join(","); + const result = await pgNoParallel( + sql`SELECT a.codein, cs.site, cs.stockdispo::float AS stockdispo + FROM cube_stock cs + JOIN articles a ON a.no_id = cs.artnoid + WHERE a.codein = ANY(string_to_array(${codeinsStr}, ','))` ); - const rows = res.rows; + const rows = result.rows as { codein: string; site: string; stockdispo: number }[]; const map = new Map(); for (const r of rows) {