-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-products.js
More file actions
145 lines (116 loc) · 4.07 KB
/
import-products.js
File metadata and controls
145 lines (116 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require('dotenv').config();
const { Pool } = require('pg');
const fs = require('fs');
const path = require('path');
const pool = new Pool({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 26257,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME || 'freshcart',
ssl: false
});
function parseCSV(file_path) {
const content = fs.readFileSync(file_path, 'utf-8');
const lines = content.split('\n');
const headers = lines[0].split(',').map(h => h.trim().replace(/"/g, ''));
const products = [];
for (let i = 1; i < lines.length; i++) {
if (!lines[i].trim()) continue;
const values = lines[i].split(',');
if (values.length < 5) continue;
const product = {};
headers.forEach((header, index) => {
product[header] = values[index] ? values[index].trim().replace(/"/g, '') : '';
});
products.push(product);
}
return products;
}
const regions = ['North', 'South', 'East', 'West'];
const warehouses = ['WH-1', 'WH-2', 'WH-3', 'WH-4', 'WH-5', 'WH-6'];
async function importProducts() {
console.log('\nStarting Amazon Product Import...\n');
try {
const csv_path = path.join(__dirname, '../data/home/sdf/amazon_products.csv');
console.log('Reading CSV file:', csv_path);
if (!fs.existsSync(csv_path)) {
console.error('CSV file not found!');
return;
}
const products = parseCSV(csv_path);
console.log(`Found ${products.length} products in CSV\n`);
console.log('Clearing existing products...');
await pool.query('TRUNCATE TABLE products CASCADE');
console.log('Products table cleared\n');
console.log('Importing products...');
let imported = 0;
let failed = 0;
for (const product of products) {
try {
let price = 0;
if (product['Selling Price']) {
const match = product['Selling Price'].match(/[\d,]+\.?\d*/);
if (match) {
price = parseFloat(match[0].replace(/,/g, ''));
}
}
if (!price || price === 0) continue;
const stock = Math.floor(Math.random() * 500) + 50;
const region = regions[Math.floor(Math.random() * regions.length)];
const warehouse = warehouses[Math.floor(Math.random() * warehouses.length)];
await pool.query(
`INSERT INTO products (
product_id, name, category, warehouse, region,
stock, price, description, image_url
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
[
product['Uniq Id'] || `PROD-${Date.now()}-${imported}`,
(product['Product Name'] || 'Unknown Product').substring(0, 255),
(product['Category'] || 'General').substring(0, 100),
warehouse,
region,
stock,
price,
(product['About Product'] || '').substring(0, 1000),
product['Image'] || ''
]
);
imported++;
if (imported % 100 === 0) {
process.stdout.write(` Imported ${imported} products...\r`);
}
} catch (err) {
failed++;
}
}
console.log(`\nImport Complete!`);
console.log(`- Successfully imported: ${imported} products`);
console.log(`- Failed: ${failed} products`);
console.log('\nDistribution by Region:');
const region_stats = await pool.query(`
SELECT region, COUNT(*) as count, SUM(stock) as total_stock
FROM products
GROUP BY region
ORDER BY region
`);
region_stats.rows.forEach(row => {
console.log(` ${row.region}: ${row.count} products, ${row.total_stock} total stock`);
});
console.log('\nDistribution by Warehouse:');
const warehouse_stats = await pool.query(`
SELECT warehouse, COUNT(*) as count
FROM products
GROUP BY warehouse
ORDER BY warehouse
`);
warehouse_stats.rows.forEach(row => {
console.log(` ${row.warehouse}: ${row.count} products`);
});
} catch (error) {
console.error('\nImport failed:', error.message);
} finally {
await pool.end();
}
}
importProducts();