[feat] script to updating pricing
This commit is contained in:
79
scripts/sync-prices.ts
Normal file
79
scripts/sync-prices.ts
Normal 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 {};
|
||||
Reference in New Issue
Block a user