-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseedDatabase.js
48 lines (39 loc) · 1.35 KB
/
seedDatabase.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const fs = require('fs');
require('dotenv').config();
const mongoose = require('mongoose');
const DJ = require('./models/dj');
const Song = require('./models/song');
const Timeslot = require('./models/timeslot');
const DJs = JSON.parse(fs.readFileSync('./data/djs.json', 'utf-8'));
const Songs = JSON.parse(fs.readFileSync('./data/songs.json', 'utf-8'));
const Timeslots = JSON.parse(fs.readFileSync('./data/timeslots.json', 'utf-8'));
const password = process.env.MONGODB_PASSWORD;
mongoose.connect(`mongodb+srv://hackerflowuser:${password}@cluster0.i0mb71s.mongodb.net/?retryWrites=true&w=majority`, {
})
.then(() => console.log('Connected to MongoDB Atlas...'))
.catch(err => console.error('Could not connect to MongoDB Atlas...', err));
const seedDB = async () => {
// Clear database
await DJ.deleteMany({});
await Song.deleteMany({});
await Timeslot.deleteMany({});
// Import Songs data
for (let song of Songs) {
let newSong = new Song(song);
await newSong.save();
}
// Import DJs data
for (let dj of DJs) {
let newDJ = new DJ(dj);
await newDJ.save();
}
// Import Timeslots data
for (let timeslot of Timeslots) {
let newTimeslot = new Timeslot({ slot: timeslot });
await newTimeslot.save();
}
};
seedDB().then(() => {
console.log('Seeding complete...');
mongoose.connection.close();
});