parallelized set Import
This commit is contained in:
@@ -1,14 +1,20 @@
|
|||||||
const { fetchAllSets } = require('../services/ygoproService');
|
const { fetchAllSets } = require('../services/ygoproService');
|
||||||
const { upsertSet } = require('../models/setModel');
|
const { upsertSet } = require('../models/setModel');
|
||||||
|
|
||||||
|
const BATCH_SIZE = 10;
|
||||||
|
|
||||||
async function importSets(req, res) {
|
async function importSets(req, res) {
|
||||||
try {
|
try {
|
||||||
const sets = await fetchAllSets();
|
const sets = await fetchAllSets();
|
||||||
let added = 0;
|
let added = 0;
|
||||||
|
|
||||||
for (const set of sets) {
|
// Parallelized batch insert
|
||||||
|
for (let i = 0; i < sets.length; i += BATCH_SIZE) {
|
||||||
|
const batch = sets.slice(i, i + BATCH_SIZE);
|
||||||
|
await Promise.all(batch.map(async (set) => {
|
||||||
await upsertSet(set);
|
await upsertSet(set);
|
||||||
added++;
|
added++;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
@@ -16,7 +22,7 @@ async function importSets(req, res) {
|
|||||||
total: sets.length
|
total: sets.length
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error('Error importing sets:', err);
|
||||||
res.status(500).json({ error: 'Failed to import sets' });
|
res.status(500).json({ error: 'Failed to import sets' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
const db = require('../config/db');
|
|
||||||
|
|
||||||
async function upsertSet(set) {
|
|
||||||
const { set_name, set_code, num_of_cards, tcg_date } = set;
|
|
||||||
|
|
||||||
const sql = `
|
|
||||||
INSERT INTO sets (set_name, set_code, num_of_cards, tcg_date)
|
|
||||||
VALUES (?, ?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
set_name = VALUES(set_name),
|
|
||||||
num_of_cards = VALUES(num_of_cards),
|
|
||||||
tcg_date = VALUES(tcg_date)
|
|
||||||
`;
|
|
||||||
await db.execute(sql, [set_name, set_code, num_of_cards, tcg_date]);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { upsertSet };
|
|
||||||
Reference in New Issue
Block a user