[chore] price sync updates index

This commit is contained in:
2026-03-03 07:59:44 -05:00
parent 1f521afa3a
commit 11dfcd38c1
2 changed files with 87 additions and 58 deletions

View File

@@ -4,8 +4,14 @@ import chalk from 'chalk';
import { db, poolConnection } from '../src/db/index.ts';
import { sql, inArray, eq } from 'drizzle-orm';
import { skus, processingSkus } from '../src/db/schema.ts';
import { client } from '../src/db/typesense.ts';
const DollarToInt = (dollar: any) => {
if (dollar === null) return null;
return Math.round(dollar * 100);
}
async function resetProcessingTable() {
// Use sql.raw to execute the TRUNCATE TABLE statement
await db.execute(sql.raw('TRUNCATE TABLE processingSkus;'));
@@ -14,6 +20,7 @@ async function resetProcessingTable() {
async function syncPrices() {
const batchSize = 1000;
// const skuIndex = client.collections('skus');
await resetProcessingTable();
console.log(chalk.green('Processing table reset and populated with current SKUs.'));
@@ -46,31 +53,53 @@ async function syncPrices() {
if (skuData.length !== batchSize) {
console.error(chalk.yellow(`Expected ${batchSize} SKUs, got ${skuData.length}`));
//process.exit(1);
}
for (const sku of skuData) {
await db.update(skus)
.set({
marketPrice: sku.marketPrice,
lowestPrice: sku.lowestPrice,
highestPrice: sku.highestPrice,
priceCount: sku.priceCount,
calculatedAt: sku.calculatedAt ? new Date(sku.calculatedAt) : null,
})
.where(eq(skus.skuId, sku.skuId));
}
const skuUpdates = skuData.map((sku: any) => { return {
skuId: sku.skuId,
cardId: 0,
productId: 0,
condition: '',
language: '',
variant: '',
calculatedAt: sku.calculatedAt ? new Date(sku.calculatedAt) : null,
highestPrice: sku.highestPrice,
lowestPrice: sku.lowestPrice,
marketPrice: sku.marketPrice,
priceCount: null,
}});
await db.insert(skus).values(skuUpdates).onDuplicateKeyUpdate({
set: {
calculatedAt: sql`values(${skus.calculatedAt})`,
highestPrice: sql`values(${skus.highestPrice})`,
lowestPrice: sql`values(${skus.lowestPrice})`,
marketPrice: sql`values(${skus.marketPrice})`,
}
});
// remove skus from the 'working' processingSkus table
await db.delete(processingSkus).where(inArray(processingSkus.skuId, skuIds));
}
// No need to call db.end(); Drizzle ORM manages connections.
}
async function indexPrices() {
const skus = await db.query.skus.findMany();
await client.collections('skus').documents().import(skus.map(sku => ({
id: sku.skuId.toString(),
condition: sku.condition,
highestPrice: DollarToInt(sku.highestPrice),
lowestPrice: DollarToInt(sku.lowestPrice),
marketPrice: DollarToInt(sku.marketPrice),
})), { action: 'upsert' });
}
const start = Date.now();
await syncPrices();
await indexPrices();
await poolConnection.end();
const end = Date.now();
const duration = (end - start) / 1000;