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,56 @@
|
||||
require('dotenv').config();
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const connectDB = require('../config/database');
|
||||
const Home = require('../models/home');
|
||||
|
||||
async function validateHomeData(data) {
|
||||
if (!data || typeof data !== 'object') {
|
||||
throw new Error('Home data must be a valid object');
|
||||
}
|
||||
|
||||
// Ví dụ validate cơ bản (tuỳ schema bạn chỉnh thêm)
|
||||
if (!data.hero || !data.quickLinks) {
|
||||
throw new Error('Missing required fields: hero or quickLinks');
|
||||
}
|
||||
}
|
||||
|
||||
async function migrateHomeData() {
|
||||
try {
|
||||
await connectDB();
|
||||
console.log('Đã kết nối MongoDB...');
|
||||
|
||||
// Xóa dữ liệu cũ
|
||||
await Home.deleteMany({});
|
||||
console.log('Đã xóa dữ liệu Home cũ');
|
||||
|
||||
// Đọc file JSON
|
||||
const homeData = JSON.parse(
|
||||
await fs.readFile(
|
||||
path.join(__dirname, '../data/home.json'),
|
||||
'utf8'
|
||||
)
|
||||
);
|
||||
|
||||
// Validate
|
||||
await validateHomeData(homeData);
|
||||
|
||||
// Transform nếu cần (optional)
|
||||
const finalData = {
|
||||
...homeData,
|
||||
updatedAt: new Date()
|
||||
};
|
||||
|
||||
// Insert
|
||||
await Home.create(finalData);
|
||||
|
||||
console.log('✓ Migrate Home thành công!');
|
||||
process.exit(0);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Lỗi:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
migrateHomeData();
|
||||
Reference in New Issue
Block a user