/** * Import vocabulary from JSON files into Context table * Each vocab word will become a Context record with: * - title: the vocabulary word * - grade: the grade/unit/lesson code (e.g., 101, 102, etc.) * - type: 'vocabulary' */ const { sequelize } = require('./config/database'); const { Context } = require('./models'); const fs = require('fs'); const path = require('path'); // Configuration const DATA_DIR = path.join(__dirname, 'data', 'moveup'); const GRADES = ['g1', 'g2', 'g3', 'g4', 'g5']; async function importVocabToContext() { try { console.log('šŸ”„ Starting vocab import to Context table...'); await sequelize.authenticate(); console.log('āœ… Database connection OK'); let totalImported = 0; let totalSkipped = 0; // Process each grade folder for (const gradeFolder of GRADES) { const gradeDir = path.join(DATA_DIR, gradeFolder); if (!fs.existsSync(gradeDir)) { console.log(`āš ļø Directory not found: ${gradeDir}`); continue; } // Get all JSON files in the grade directory const jsonFiles = fs.readdirSync(gradeDir).filter(f => f.endsWith('.json')); console.log(`\nšŸ“‚ Processing ${gradeFolder.toUpperCase()} (${jsonFiles.length} files)...`); for (const jsonFile of jsonFiles) { const filePath = path.join(gradeDir, jsonFile); console.log(` šŸ“„ Reading ${jsonFile}...`); try { const data = JSON.parse(fs.readFileSync(filePath, 'utf8')); if (!Array.isArray(data)) { console.log(` āš ļø Skipping ${jsonFile}: not an array`); continue; } // Process each lesson/unit entry for (const entry of data) { const { grade, vocab } = entry; if (!grade || !vocab || !Array.isArray(vocab)) { continue; } console.log(` šŸ“š Grade ${grade}: ${vocab.length} vocab words`); // Import each vocabulary word for (const word of vocab) { if (!word || word.trim() === '') { continue; } try { // Check if this vocab already exists for this grade const existing = await Context.findOne({ where: { title: word.trim(), type: 'vocabulary' } }); if (existing) { totalSkipped++; continue; } // Create new context entry for this vocab await Context.create({ title: word.trim(), grade: parseInt(grade), type: 'vocabulary', context: `Vocabulary word for grade ${grade}`, knowledge: null, desc: null }); totalImported++; } catch (err) { console.error(` āŒ Error importing "${word}": ${err.message}`); } } } } catch (err) { console.error(` āŒ Error reading ${jsonFile}: ${err.message}`); } } } console.log('\nāœ… Import complete!'); console.log(`šŸ“Š Total imported: ${totalImported}`); console.log(`ā­ļø Total skipped (duplicates): ${totalSkipped}`); process.exit(0); } catch (error) { console.error('āŒ Error during import:', error.message); console.error(error.stack); process.exit(1); } } // Run the import importVocabToContext();