-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathseeder.js
55 lines (46 loc) · 1.04 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
45
46
47
48
49
50
51
52
53
54
55
const fs = require('fs')
const mongoose = require('mongoose')
const colors = require('colors')
const dotenv = require('dotenv')
// Load env vars
dotenv.config({ path: './config/.env' })
const User = require('./models/User')
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
})
const users = JSON.parse(
fs.readFileSync(
`${__dirname}/_data/${
process.env.NODE_ENV == 'production' ? 'users-prod.json' : 'users.json'
}`,
'utf-8'
)
)
const importData = async () => {
try {
await User.create(users)
console.log('Data Imported...'.green.inverse)
process.exit()
} catch (err) {
console.error(err)
}
}
const deleteData = async () => {
try {
await User.deleteMany()
console.log('Data Destroyed...'.red.inverse)
process.exit()
} catch (err) {
console.error(err)
}
}
if (process.argv[2] === '-i') {
// node seeder -i
importData()
} else if (process.argv[2] === '-d') {
// node seeder -d
deleteData()
}