[feat] script to updating pricing

This commit is contained in:
2026-02-15 15:25:08 -05:00
parent a774360065
commit 32df33f29e
7 changed files with 94 additions and 14 deletions

View File

@@ -11,13 +11,9 @@ import chalk from 'chalk';
async function syncTcgplayer() {
// const productLines = [
// { name: "pokemon", energyType: ["Water", "Fire", "Grass", "Lightning", "Psychic", "Fighting", "Darkness", "Metal", "Fairy", "Dragon", "Colorless", "Energy"] },
// { name: "pokemon-japan", cardType: ["Water", "Fire", "Grass", "Lightning", "Psychic", "Fighting", "Darkness", "Metal", "Fairy", "Dragon", "Colorless", "Energy"] }
// ];
const productLines = [
{ name: "pokemon-japan", cardType: ["Dragon", "Colorless", "Energy"] }
{ name: "pokemon", energyType: ["Water", "Fire", "Grass", "Lightning", "Psychic", "Fighting", "Darkness", "Metal", "Fairy", "Dragon", "Colorless", "Energy"] },
{ name: "pokemon-japan", cardType: ["Water", "Fire", "Grass", "Lightning", "Psychic", "Fighting", "Darkness", "Metal", "Fairy", "Dragon", "Colorless", "Energy"] }
];
for (const productLine of productLines) {

79
scripts/sync-prices.ts Normal file
View File

@@ -0,0 +1,79 @@
import 'dotenv/config';
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';
async function resetProcessingTable() {
// Use sql.raw to execute the TRUNCATE TABLE statement
await db.execute(sql.raw('TRUNCATE TABLE processingSkus;'));
await db.insert(processingSkus).select(db.select({skuId: skus.skuId}).from(skus));
}
async function syncPrices() {
const batchSize = 1000;
await resetProcessingTable();
console.log(chalk.green('Processing table reset and populated with current SKUs.'));
while (true) {
const batch = await db.select().from(processingSkus).limit(batchSize);
if (batch.length === 0) {
console.log('All SKUs processed.');
break;
}
const skuIds = batch.map(item => item.skuId);
console.log(`${chalk.blue('Processing SKUs:')} ${chalk.gray(skuIds.join(', '))}`);
const skuResponse = await fetch('https://mpgateway.tcgplayer.com/v1/pricepoints/marketprice/skus/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ skuIds: skuIds }),
});
if (!skuResponse.ok) {
console.error('Error fetching SKU pricing:', skuResponse.statusText);
process.exit(1);
}
const skuData = await skuResponse.json();
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.lowPrice,
highestPrice: sku.highPrice,
priceCount: sku.priceCount,
calculatedAt: sku.calculatedAt ? new Date(sku.calculatedAt) : null,
})
.where(eq(skus.skuId, sku.skuId));
}
await db.delete(processingSkus).where(inArray(processingSkus.skuId, skuIds));
}
// No need to call db.end(); Drizzle ORM manages connections.
}
const start = Date.now();
await syncPrices();
await poolConnection.end();
const end = Date.now();
const duration = (end - start) / 1000;
console.log(chalk.green(`Price sync completed in ${duration.toFixed(2)} seconds.`));
export {};