68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import 'dotenv/config';
|
|
import { db, poolConnection } from '../src/db/index.ts';
|
|
import chalk from 'chalk';
|
|
import fs from "fs";
|
|
//import path from "node:path";
|
|
import { parse, stringify, transform } from 'csv';
|
|
|
|
async function PricesFromCSV() {
|
|
|
|
const inputFilePath = 'scripts/test.csv';
|
|
const outputFilePath = 'scripts/output.csv';
|
|
|
|
// Create read and write streams
|
|
const inputStream = fs.createReadStream(inputFilePath, 'utf8');
|
|
const outputStream = fs.createWriteStream(outputFilePath);
|
|
|
|
// Define the transformation logic
|
|
const transformer = transform({ parallel: 1 }, async function(this: any, row: any, callback: any) {
|
|
try {
|
|
// Get the ProductId from the first column
|
|
const productId = Number(Object.values(row)[0]);
|
|
console.log('Processing productId:', productId);
|
|
|
|
// Get and write all variants
|
|
const variants = await db.query.cards.findMany(
|
|
{ with: { prices: true, tcgdata: true },
|
|
where: { tcgdata: { productName: row["Product Name"] } }
|
|
});
|
|
|
|
if (variants.length === 0) {
|
|
console.log('No variants found for productId:', productId);
|
|
}
|
|
|
|
for (const variant of variants) {
|
|
const newRow = { ...row };
|
|
newRow.Variant = variant.variant;
|
|
newRow.marketPrice = variant.prices.find(p => p.condition === 'Near Mint')?.marketPrice;
|
|
this.push(newRow);
|
|
}
|
|
callback();
|
|
} catch (error) {
|
|
callback(error);
|
|
}
|
|
});
|
|
|
|
// Pipe the streams: Read -> Parse -> Transform -> Stringify -> Write
|
|
inputStream
|
|
.on('error', (error) => console.error('Input stream error:', error))
|
|
.pipe(parse({ columns: true, trim: true }))
|
|
.on('error', (error) => console.error('Parse error:', error))
|
|
.pipe(transformer)
|
|
.on('error', (error) => console.error('Transform error:', error))
|
|
.pipe(stringify({ header: true }))
|
|
.on('error', (error) => console.error('Stringify error:', error))
|
|
.pipe(outputStream);
|
|
|
|
outputStream.on('finish', () => {
|
|
console.log(`Successfully written to ${outputFilePath}`);
|
|
poolConnection.end();
|
|
});
|
|
|
|
outputStream.on('error', (error) => {
|
|
console.error('An error occurred in the process:', error);
|
|
poolConnection.end();
|
|
});
|
|
}
|
|
|
|
await PricesFromCSV(); |