-
Notifications
You must be signed in to change notification settings - Fork 2
/
seeder.js
43 lines (36 loc) · 817 Bytes
/
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
// MODEL
const User = require('./models/User')
// MIGRATION DATA
const users = require('./migrate/users.js')
// MongoDB Connect
const connectDB = require('./config/db');
connectDB()
const importData = async () => {
try {
await destroyData(true)
// USER MIGRATION
for (let index = 0; index < users.length; index ++) {
await User.create(users[index])
}
console.log('Users Imported!')
process.exit()
} catch (error) {
console.error(`${error}`)
process.exit(1)
}
}
const destroyData = async (onImport = false) => {
try {
await User.deleteMany()
console.log('Data Destroyed!')
if (!onImport) process.exit()
} catch (error) {
console.error(`${error}`)
process.exit(1)
}
}
if (process.argv[2] === '-d') {
destroyData()
} else {
importData()
}