File size: 1,868 Bytes
4b3a813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const mongoose = require('mongoose');
require('dotenv').config();

const Category = require('../src/models/Category');

const newCategories = [
    { name: "Periferik qurilmalar B/U", description: "Ishlatilgan periferik qurilmalar (Sichqoncha, Klaviatura va h.k)" },
    { name: "Periferik qurilmalar Yangi", description: "Yangi periferik qurilmalar" },
    { name: "Kompyuterning asosiy qismlari B/U", description: "Ishlatilgan kompyuter ehtiyot qismlari (CPU, GPU, RAM)" },
    { name: "Kompyuterning asosiy qismlari Yangi", description: "Yangi kompyuter ehtiyot qismlari" }
];

async function updateCategories() {
    try {
        await mongoose.connect(process.env.MONGO_URI);
        console.log('MongoDB Connected');

        // Clear existing
        await Category.deleteMany({});
        console.log('Old categories deleted.');

        // Add new
        for (const cat of newCategories) {
            // Generate a simple ID if model requires it, checking model definition first
            // Assuming model has 'id' field which might be string or number.
            // Let's generate a unique string ID based on random or counter if needed.
            // Based on previous views, models usually have 'id'.
            // Let's use a random 6 digit ID for simplicity or auto-increment if logic exists.

            const newCat = new Category({
                id: Math.floor(100000 + Math.random() * 900000).toString(),
                name: cat.name,
                description: cat.description
            });
            await newCat.save();
            console.log(`Added: ${cat.name}`);
        }

        console.log('All categories updated successfully!');
        process.exit();
    } catch (error) {
        console.error('Error:', error);
        process.exit(1);
    }
}

updateCategories();