forked from UKSOURCE/cms.lams
Refactor Homepage, About, Footer (Controller,Model, View, Data), Update: Dashboard, Delete old file
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
require('dotenv').config();
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const connectDB = require('../config/database');
|
||||
const About = require('../models/about');
|
||||
|
||||
async function validateAboutData(data) {
|
||||
if (!data || typeof data !== 'object') {
|
||||
throw new Error('About data must be a valid object');
|
||||
}
|
||||
|
||||
// Tuỳ schema bạn chỉnh lại cho chuẩn hơn
|
||||
if (!data.title || !data.sections) {
|
||||
throw new Error('Missing required fields: title or sections');
|
||||
}
|
||||
|
||||
if (!Array.isArray(data.sections)) {
|
||||
throw new Error('sections must be an array');
|
||||
}
|
||||
}
|
||||
|
||||
async function migrateAboutData() {
|
||||
try {
|
||||
await connectDB();
|
||||
console.log('Đã kết nối MongoDB...');
|
||||
|
||||
// Xóa dữ liệu cũ
|
||||
await About.deleteMany({});
|
||||
console.log('Đã xóa dữ liệu About cũ');
|
||||
|
||||
// Đọc file JSON
|
||||
const aboutData = JSON.parse(
|
||||
await fs.readFile(
|
||||
path.join(__dirname, '../data/about.json'),
|
||||
'utf8'
|
||||
)
|
||||
);
|
||||
|
||||
// Validate
|
||||
await validateAboutData(aboutData);
|
||||
|
||||
// Transform (optional)
|
||||
const finalData = {
|
||||
...aboutData,
|
||||
updatedAt: new Date()
|
||||
};
|
||||
|
||||
// Insert
|
||||
await About.create(finalData);
|
||||
|
||||
console.log('✓ Migrate About thành công!');
|
||||
process.exit(0);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Lỗi:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
migrateAboutData();
|
||||
Reference in New Issue
Block a user