[chore] refactor indexing scripts

This commit is contained in:
2026-03-12 08:18:40 -04:00
parent c10e34cc34
commit 485f26de7b
4 changed files with 111 additions and 151 deletions

View File

@@ -3,16 +3,11 @@ import 'dotenv/config';
import chalk from 'chalk';
import { db, ClosePool } 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';
import { skus, processingSkus, priceHistory } from '../src/db/schema.ts';
import { toSnakeCase } from 'drizzle-orm/casing';
import * as Indexing from './indexing.ts';
const DollarToInt = (dollar: any) => {
if (dollar === null) return null;
return Math.round(dollar * 100);
}
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@@ -73,7 +68,7 @@ async function syncPrices() {
marketPrice: sku.marketPrice,
priceCount: null,
}});
await db.insert(skus).values(skuUpdates).onConflictDoUpdate({
const skuRows = await db.insert(skus).values(skuUpdates).onConflictDoUpdate({
target: skus.skuId,
set: {
calculatedAt: sql.raw(`excluded.${toSnakeCase(skus.calculatedAt.name)}`),
@@ -92,22 +87,10 @@ async function syncPrices() {
}
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 Indexing.upsertSkuCollection(db);
await ClosePool();
const end = Date.now();
const duration = (end - start) / 1000;