-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertPlants.js
More file actions
41 lines (38 loc) · 1.25 KB
/
insertPlants.js
File metadata and controls
41 lines (38 loc) · 1.25 KB
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
const { MongoClient } = require('mongodb');
const fs = require('fs').promises;
const path = require('path');
const dotenv = require('dotenv');
dotenv.config({ path: path.resolve(__dirname, '.env.local') });
async function insertPlants() {
const uri = process.env.MONGODB_URI;
if (!uri) {
throw new Error('MONGODB_URI is not defined in .env.local');
}
const client = new MongoClient(uri);
try {
await client.connect();
console.log('Connected to MongoDB');
const database = client.db('plantdb');
const collection = database.collection('plants');
const plantData = JSON.parse(await fs.readFile('./data/plants.json', 'utf8'));
const plants = plantData.plants.map(plant => ({
name: plant.name,
description: plant.description,
image: plant.image,
category: plant.category,
}));
await collection.deleteMany({});
const result = await collection.insertMany(plants);
console.log(`Inserted ${result.insertedCount} plants successfully`);
} catch (error) {
console.error('Error inserting plants:', error);
throw error;
} finally {
await client.close();
console.log('MongoDB connection closed');
}
}
insertPlants().catch(error => {
console.error('Script failed:', error);
process.exit(1);
});