-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportData.js
29 lines (23 loc) · 973 Bytes
/
importData.js
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
// importData.js
import fs from 'fs';
import path from 'path';
import connectDatabase from './db.js';
import csvtojson from 'csvtojson';
async function importData() {
try {
const db = await connectDatabase();
const dataFolder = path.join('C:/Users/MSI/Desktop/r/Zywa Assignment', 'data');
fs.readdirSync(dataFolder).forEach(async (file) => {
const collectionName = path.parse(file).name;
const collection = db.collection(collectionName.replace(/ /g, '_')); // Replace spaces with underscores
const csvFilePath = path.join(dataFolder, file);
const jsonArray = await csvtojson().fromFile(csvFilePath);
await collection.deleteMany({}); // Clear existing data
await collection.insertMany(jsonArray);
console.log(`Data imported into ${collectionName} collection`);
});
} catch (error) {
console.error('Error importing data:', error);
}
}
export default importData;