-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsertProduct.mjs
More file actions
97 lines (82 loc) · 2.84 KB
/
insertProduct.mjs
File metadata and controls
97 lines (82 loc) · 2.84 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
import fs from 'fs/promises';
import path from 'path';
import mysql from 'mysql2/promise';
async function main() {
const connection = await mysql.createConnection({
host: 'wit-database-2.chcygck0gruw.ap-northeast-2.rds.amazonaws.com',
user: 'root',
password: '12345678',
database: 'wit'
});
const filePath = path.resolve('products.json');
try {
const data = await fs.readFile(filePath, 'utf8');
if (!data) {
console.error('파일이 비어 있습니다.');
return;
}
let products;
try {
products = JSON.parse(data);
} catch (parseErr) {
console.error('JSON 파싱 오류:', parseErr);
return;
}
// 서브 카테고리 ID를 얻기 위한 쿼리
const categoryQuery = `
SELECT sc.sub_category_id
FROM sub_category sc
JOIN main_category mc ON sc.main_category_id = mc.main_category_id
WHERE mc.main_category_name = ? AND sc.sub_category_name = ?
`;
// 제품을 삽입하기 위한 쿼리
const insertQuery = `
INSERT INTO product
(name, description, won_price, en_price, image, sales_area, manufacturing_country, product_type, sub_category_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
for (const product of products) {
console.log(`처리 중인 제품: ${product.name}`);
console.log(`메인 카테고리: ${product.main_category}`);
console.log(`서브 카테고리: ${product.sub_category}`);
// 서브 카테고리 ID를 가져오기 위해 쿼리 실행
const [rows] = await connection.query(categoryQuery, [product.main_category, product.sub_category]);
if (rows.length === 0) {
console.error(`서브 카테고리를 찾을 수 없습니다: ${product.main_category} - ${product.sub_category}`);
continue;
}
const subCategoryId = rows[0].sub_category_id;
const wonPrice = parseInt(product.won_price.replace(/[^0-9]/g, ''), 10);
const enPrice = parseInt(product.en_price.replace(/[^0-9]/g, ''), 10);
const productTypeMap = {
'식품': 'food',
'뷰티코스메틱': 'cosmetic',
'생활용품': 'household goods',
'의약품': 'medicine'
};
const productType = productTypeMap[product.main_category] || 'unknown';
try {
await connection.query(insertQuery, [
product.name,
product.description,
wonPrice,
enPrice,
product.image,
product.sales_area,
product.manufacturing_country,
productType,
subCategoryId
]);
console.log(`제품 삽입 성공: ${product.name}`);
} catch (err) {
console.error('쿼리 실행 오류:', err);
}
}
} catch (err) {
console.error('파일 읽기 오류:', err);
} finally {
await connection.end();
}
}
// 실행
main();