Get product import started
This commit is contained in:
@@ -465,6 +465,7 @@ async function importProducts(prodConnection, localConnection) {
|
|||||||
|
|
||||||
console.log(`Inserting ${batch.length} products`);
|
console.log(`Inserting ${batch.length} products`);
|
||||||
|
|
||||||
|
// First insert the products
|
||||||
const values = batch.flatMap(r => [
|
const values = batch.flatMap(r => [
|
||||||
r.product_id, r.title, r.description, r.SKU, r.created_at, r.first_received,
|
r.product_id, r.title, r.description, r.SKU, r.created_at, r.first_received,
|
||||||
r.stock_quantity, r.preorder_count, r.notions_inv_count, r.price, r.regular_price,
|
r.stock_quantity, r.preorder_count, r.notions_inv_count, r.price, r.regular_price,
|
||||||
@@ -477,10 +478,14 @@ async function importProducts(prodConnection, localConnection) {
|
|||||||
r.baskets, r.notifies, r.date_last_sold
|
r.baskets, r.notifies, r.date_last_sold
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Create a constant for the SQL query to improve readability
|
// Get the number of columns from the first row's values
|
||||||
const insertProductsSQL = `
|
const columnsPerRow = values.length / batch.length;
|
||||||
INSERT INTO products (
|
const placeholders = batch.map(() =>
|
||||||
product_id, title, description, SKU, created_at, first_received,
|
`(${Array(columnsPerRow).fill('?').join(',')})`
|
||||||
|
).join(',');
|
||||||
|
|
||||||
|
const sql = `INSERT INTO products (
|
||||||
|
pid, title, description, SKU, created_at, first_received,
|
||||||
stock_quantity, preorder_count, notions_inv_count, price, regular_price,
|
stock_quantity, preorder_count, notions_inv_count, price, regular_price,
|
||||||
cost_price, landing_cost_price, barcode, harmonized_tariff_code,
|
cost_price, landing_cost_price, barcode, harmonized_tariff_code,
|
||||||
updated_at, visible, managing_stock, replenishable, vendor,
|
updated_at, visible, managing_stock, replenishable, vendor,
|
||||||
@@ -490,7 +495,7 @@ async function importProducts(prodConnection, localConnection) {
|
|||||||
width, height, country_of_origin, location, total_sold,
|
width, height, country_of_origin, location, total_sold,
|
||||||
baskets, notifies, date_last_sold
|
baskets, notifies, date_last_sold
|
||||||
)
|
)
|
||||||
VALUES ${batch.map(() => '(' + '?,'.repeat(45).slice(0,-1) + ')').join(',')}
|
VALUES ${placeholders}
|
||||||
ON DUPLICATE KEY UPDATE
|
ON DUPLICATE KEY UPDATE
|
||||||
title = VALUES(title),
|
title = VALUES(title),
|
||||||
description = VALUES(description),
|
description = VALUES(description),
|
||||||
@@ -534,7 +539,33 @@ async function importProducts(prodConnection, localConnection) {
|
|||||||
notifies = VALUES(notifies),
|
notifies = VALUES(notifies),
|
||||||
date_last_sold = VALUES(date_last_sold)`;
|
date_last_sold = VALUES(date_last_sold)`;
|
||||||
|
|
||||||
await localConnection.query(insertProductsSQL, values);
|
await localConnection.query(sql, values);
|
||||||
|
|
||||||
|
// Then insert the category relationships for this batch
|
||||||
|
const categoryPromises = batch.map(async product => {
|
||||||
|
// Get the category IDs from the production query
|
||||||
|
const [categoryRows] = await prodConnection.query(`
|
||||||
|
SELECT DISTINCT cat_id
|
||||||
|
FROM product_category_index
|
||||||
|
WHERE pid = ? AND cat_id IN (
|
||||||
|
SELECT cat_id FROM product_categories WHERE hidden = 0
|
||||||
|
)
|
||||||
|
`, [product.product_id]);
|
||||||
|
|
||||||
|
return categoryRows.map(row => [row.cat_id, product.product_id]);
|
||||||
|
});
|
||||||
|
|
||||||
|
const categoryResults = await Promise.all(categoryPromises);
|
||||||
|
const categoryRelationships = categoryResults.flat();
|
||||||
|
|
||||||
|
if (categoryRelationships.length > 0) {
|
||||||
|
const catPlaceholders = categoryRelationships.map(() => '(?, ?)').join(',');
|
||||||
|
await localConnection.query(`
|
||||||
|
INSERT INTO product_categories (cat_id, pid)
|
||||||
|
VALUES ${catPlaceholders}
|
||||||
|
ON DUPLICATE KEY UPDATE cat_id = VALUES(cat_id)
|
||||||
|
`, categoryRelationships.flat());
|
||||||
|
}
|
||||||
|
|
||||||
current += batch.length;
|
current += batch.length;
|
||||||
updateProgress(current, total, 'Products import', startTime);
|
updateProgress(current, total, 'Products import', startTime);
|
||||||
|
|||||||
Reference in New Issue
Block a user