File size: 2,169 Bytes
c01955c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import mongoose from "mongoose";
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import { DB_NAME } from '../constants/constants';
import Template from "../models/templates.models.js";

dotenv.config({
    path: "./.env"
});

const seedTemplates = async function seedTemplates() {
    if (!process.env.MONGODB_URI) {
        console.error("MONGODB_URI is not defined");
        return;
    }
    try {
        const connectionString = process.env.MONGODB_URI 
          ? `${process.env.MONGODB_URI}/${DB_NAME}`
          : `mongodb://localhost:27017/${DB_NAME}`;
      await mongoose.connect(connectionString);
      console.log(`Connected to MongoDB: ${DB_NAME}`);

        const templatesDir = path.join(process.cwd(), "src", "templates");
        const files = fs.readdirSync(templatesDir);

        for (const file of files) {
            if (file.endsWith(".ejs")) {
                const match = file.match(/resume(\d+)\.ejs/);
                if (!match) continue;
                const id = match[1];
                const templatePath = path.join(templatesDir, file);
                const dataPath = path.join(templatesDir, `resume${id}.data.json`);

                const templateContent = fs.readFileSync(templatePath, "utf-8");
                
                // Read the JSON data file synchronously
                const tempDataRaw = fs.readFileSync(dataPath, "utf-8");
                const tempData = JSON.parse(tempDataRaw);

                const title = `Resume Template ${id}`;

                // Update or create template
                await Template.findOneAndUpdate(
                    { title },
                    {
                        template: templateContent,
                        temp_data: tempData
                    },
                    { upsert: true, new: true }
                );

                console.log(`Seeded/Updated template: ${title}`);
            }
        }

        console.log("Seeding completed successfully");
    } catch (error) {
        console.error("Error seeding templates:", error);
    } finally {
        await mongoose.disconnect();
    }
};

seedTemplates();