-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.js
46 lines (37 loc) · 1.02 KB
/
seeder.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
const fs = require('fs')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const colors = require('colors');
// Load env config
dotenv.config({ path: './config/config.env' })
// Load Bootcamp model
const Bootcamp = require('./models/Bootcamp')
// Connect to db
mongoose.connect(process.env.MONGO_URI)
// Read json files
const bootcamps = JSON.parse(fs.readFileSync(`${__dirname}/_data/bootcamps.json`, 'utf-8'))
// Import bootcamps into db
const importData = async () => {
try {
await Bootcamp.create(bootcamps)
console.log('bootcamps imported!'.green.inverse)
process.exit()
} catch (error) {
console.error(error)
}
}
// Delete bootcamps from db
const deleteData = async () => {
try {
await Bootcamp.deleteMany()
console.log('bootcamps destroyed!'.red.inverse)
process.exit()
} catch (error) {
console.error(error)
}
}
if (process.argv[2] === '-i') {
importData()
} else if (process.argv[2] === '-d') {
deleteData()
}