script para actualizar index

This commit is contained in:
Cat /dev/Nulo 2024-10-15 09:06:44 -03:00
parent cdeb3c8d7d
commit 6cfe008b2b
2 changed files with 28 additions and 4 deletions

View file

@ -196,10 +196,7 @@ export const sucursalesRelations = relations(sucursales, ({ one }) => ({
})); }));
// para actualizar la tabla: // para actualizar la tabla:
// insert into productos_descripcion_index // bun run scripts/refresh-descripciones-index.ts
// select distinct id_producto, productos_descripcion, productos_marca from precios
// on conflict do nothing;
// probablemente se pueda poner un where en el select para solo seleccionar precios recientes (tipo id_dataset > X)
export const productos_descripcion_index = pgTable( export const productos_descripcion_index = pgTable(
"productos_descripcion_index", "productos_descripcion_index",
{ {

View file

@ -0,0 +1,27 @@
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "../db/schema";
import { formatISO, subDays } from "date-fns";
import { sql } from "drizzle-orm";
const db = drizzle(postgres(process.env.DATABASE_URL!), {
schema,
logger: true,
});
const aWeekAgo = subDays(new Date(), 5);
db.execute(sql`
insert into productos_descripcion_index
select distinct id_producto, productos_descripcion, productos_marca from precios
where id_dataset in (
SELECT d1.id FROM datasets d1
JOIN (
SELECT id_comercio, MAX(date) as max_date
FROM datasets
WHERE date > ${formatISO(aWeekAgo, { representation: "date" })}
GROUP BY id_comercio
) d2 ON d1.id_comercio = d2.id_comercio AND d1.date = d2.max_date
ORDER BY d1.id_comercio)
on conflict do nothing;
`);