forked from UKSOURCE/cms.hailearning.edu.vn
49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
const mongoose = require("mongoose");
|
|
const dotenv = require("dotenv");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
// Load environment variables
|
|
dotenv.config();
|
|
|
|
const AboutUs = require("../models/aboutUs");
|
|
|
|
const migrate = async () => {
|
|
try {
|
|
console.log("🚀 Starting About Us migration...");
|
|
|
|
// 1. Connect to MongoDB
|
|
await mongoose.connect(process.env.MONGODB_URI);
|
|
console.log("✅ MongoDB Connected");
|
|
|
|
// 2. Read about.json from Backend (Source of Truth)
|
|
const jsonPath = path.join(__dirname, "../data/about.json");
|
|
if (!fs.existsSync(jsonPath)) {
|
|
throw new Error(`Source about.json not found at: ${jsonPath}`);
|
|
}
|
|
|
|
const rawData = fs.readFileSync(jsonPath, "utf8");
|
|
const jsonData = JSON.parse(rawData);
|
|
console.log("✅ Read about.json successfully");
|
|
|
|
// 3. Delete existing AboutUs documents (Singleton pattern)
|
|
await AboutUs.deleteMany({});
|
|
console.log("✅ Cleared existing AboutUs collection");
|
|
|
|
// 4. Create new AboutUs document with JSON data
|
|
const newAboutUs = new AboutUs(jsonData);
|
|
await newAboutUs.save();
|
|
console.log("✅ Successfully migrated about.json data to MongoDB");
|
|
|
|
} catch (error) {
|
|
console.error("❌ Migration failed:", error.message);
|
|
} finally {
|
|
// 5. Close connection
|
|
await mongoose.connection.close();
|
|
console.log("👋 Database connection closed");
|
|
process.exit(0);
|
|
}
|
|
};
|
|
|
|
migrate();
|