201 lines
4.8 KiB
JavaScript
201 lines
4.8 KiB
JavaScript
/**
|
|
* Update vocabulary context entries
|
|
* This script allows you to modify vocabulary records in Context table
|
|
* You can:
|
|
* - Update specific vocab words
|
|
* - Bulk update by grade
|
|
* - Update fields like title, context, knowledge, desc
|
|
* - Delete vocab entries
|
|
*/
|
|
|
|
const { sequelize } = require('./config/database');
|
|
const { Context } = require('./models');
|
|
|
|
// ============================================
|
|
// CONFIGURATION: Customize your updates here
|
|
// ============================================
|
|
|
|
const UPDATE_MODE = 'single'; // Options: 'single', 'bulk_by_grade', 'delete'
|
|
|
|
// FOR SINGLE WORD UPDATE
|
|
const SINGLE_UPDATE = {
|
|
oldTitle: 'dad', // Current title to search for
|
|
grade: 101, // Grade to search in
|
|
updates: {
|
|
title: 'dad', // New title (if you want to change it)
|
|
context: 'Family member - father', // Updated context
|
|
knowledge: 'Common family vocabulary for beginners',
|
|
desc: 'Từ chỉ bố/cha trong tiếng Anh'
|
|
}
|
|
};
|
|
|
|
// FOR BULK UPDATE BY GRADE
|
|
const BULK_UPDATE = {
|
|
grade: 101, // Which grade to update
|
|
updates: {
|
|
context: 'Grade 1 Unit 1 Lesson 1 vocabulary',
|
|
knowledge: 'Basic greeting and family vocabulary'
|
|
}
|
|
};
|
|
|
|
// FOR DELETION
|
|
const DELETE_CONFIG = {
|
|
title: 'test_word', // Word to delete
|
|
grade: 101 // Grade to delete from
|
|
};
|
|
|
|
// ============================================
|
|
// MAIN FUNCTIONS
|
|
// ============================================
|
|
|
|
async function updateSingleVocab() {
|
|
const { oldTitle, grade, updates } = SINGLE_UPDATE;
|
|
|
|
console.log(`🔍 Looking for vocab: "${oldTitle}" in grade ${grade}...`);
|
|
|
|
const vocab = await Context.findOne({
|
|
where: {
|
|
title: oldTitle,
|
|
grade: grade,
|
|
type: 'vocabulary'
|
|
}
|
|
});
|
|
|
|
if (!vocab) {
|
|
console.log('❌ Vocab not found!');
|
|
return 0;
|
|
}
|
|
|
|
console.log(`✅ Found vocab: ${vocab.title}`);
|
|
console.log(`📝 Updating...`);
|
|
|
|
await vocab.update(updates);
|
|
console.log(`✅ Updated successfully!`);
|
|
console.log(`New data:`, {
|
|
title: vocab.title,
|
|
grade: vocab.grade,
|
|
context: vocab.context,
|
|
knowledge: vocab.knowledge,
|
|
desc: vocab.desc
|
|
});
|
|
|
|
return 1;
|
|
}
|
|
|
|
async function bulkUpdateByGrade() {
|
|
const { grade, updates } = BULK_UPDATE;
|
|
|
|
console.log(`🔍 Looking for all vocab in grade ${grade}...`);
|
|
|
|
const vocabs = await Context.findAll({
|
|
where: {
|
|
grade: grade,
|
|
type: 'vocabulary'
|
|
}
|
|
});
|
|
|
|
console.log(`✅ Found ${vocabs.length} vocab words`);
|
|
console.log(`📝 Updating...`);
|
|
|
|
for (const vocab of vocabs) {
|
|
await vocab.update(updates);
|
|
}
|
|
|
|
console.log(`✅ Updated ${vocabs.length} vocab words successfully!`);
|
|
return vocabs.length;
|
|
}
|
|
|
|
async function deleteVocab() {
|
|
const { title, grade } = DELETE_CONFIG;
|
|
|
|
console.log(`🔍 Looking for vocab: "${title}" in grade ${grade}...`);
|
|
|
|
const result = await Context.destroy({
|
|
where: {
|
|
title: title,
|
|
grade: grade,
|
|
type: 'vocabulary'
|
|
}
|
|
});
|
|
|
|
if (result === 0) {
|
|
console.log('❌ Vocab not found or already deleted!');
|
|
} else {
|
|
console.log(`✅ Deleted ${result} vocab entry!`);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
async function listVocabByGrade(grade) {
|
|
console.log(`\n📚 Listing all vocab in grade ${grade}...`);
|
|
|
|
const vocabs = await Context.findAll({
|
|
where: {
|
|
grade: grade,
|
|
type: 'vocabulary'
|
|
},
|
|
order: [['title', 'ASC']]
|
|
});
|
|
|
|
console.log(`\n✅ Found ${vocabs.length} vocab words:\n`);
|
|
vocabs.forEach((v, idx) => {
|
|
console.log(`${idx + 1}. ${v.title}`);
|
|
if (v.context) console.log(` Context: ${v.context}`);
|
|
if (v.knowledge) console.log(` Knowledge: ${v.knowledge}`);
|
|
if (v.desc) console.log(` Desc: ${v.desc}`);
|
|
console.log('');
|
|
});
|
|
|
|
return vocabs.length;
|
|
}
|
|
|
|
// ============================================
|
|
// MAIN EXECUTION
|
|
// ============================================
|
|
|
|
async function main() {
|
|
try {
|
|
console.log('🔄 Starting vocab context update...\n');
|
|
|
|
await sequelize.authenticate();
|
|
console.log('✅ Database connection OK\n');
|
|
|
|
let result = 0;
|
|
|
|
switch (UPDATE_MODE) {
|
|
case 'single':
|
|
result = await updateSingleVocab();
|
|
break;
|
|
|
|
case 'bulk_by_grade':
|
|
result = await bulkUpdateByGrade();
|
|
break;
|
|
|
|
case 'delete':
|
|
result = await deleteVocab();
|
|
break;
|
|
|
|
case 'list':
|
|
// Change grade here to list different grades
|
|
result = await listVocabByGrade(101);
|
|
break;
|
|
|
|
default:
|
|
console.log('❌ Invalid UPDATE_MODE');
|
|
}
|
|
|
|
console.log('\n✅ Operation complete!');
|
|
console.log(`📊 Records affected: ${result}`);
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('❌ Error during update:', error.message);
|
|
console.error(error.stack);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Run the script
|
|
main();
|