forked from UKSOURCE/cms.hailearning.edu.vn
first commit
This commit is contained in:
38
scripts/2025_12_02_114127_contact.js
Normal file
38
scripts/2025_12_02_114127_contact.js
Normal file
@@ -0,0 +1,38 @@
|
||||
require("dotenv").config();
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const connectDB = require("../config/database");
|
||||
const Contact = require("../models/contact");
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
/**
|
||||
* Migration: contact
|
||||
* Migrate contact data from contact-data.json
|
||||
*/
|
||||
async function migrate() {
|
||||
try {
|
||||
await connectDB();
|
||||
|
||||
// Read contact-data.json file
|
||||
const contactJsonPath = path.join(__dirname, "../data/contact-data.json");
|
||||
const contactData = JSON.parse(await fs.readFile(contactJsonPath, "utf8"));
|
||||
|
||||
// Migrate data using the model's static method
|
||||
await Contact.migrateFromJson(contactData);
|
||||
|
||||
console.log("Contact migration completed successfully");
|
||||
|
||||
await mongoose.disconnect();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error("Migration error:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Chạy migration nếu được gọi trực tiếp
|
||||
if (require.main === module) {
|
||||
migrate();
|
||||
}
|
||||
|
||||
module.exports = { migrate };
|
||||
Reference in New Issue
Block a user