-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeder.js
58 lines (46 loc) · 1.3 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
56
57
58
import mongoose from 'mongoose'
import colors from 'colors'
import dotenv from 'dotenv'
import users from './data/users.js'
import products from './data/products.js'
import User from './models/userModel.js'
import Product from './models/productModel.js'
import Order from './models/orderModel.js'
import ConnectDB from './config/db.js'
dotenv.config()
ConnectDB()
const importData = async () => {
try {
await User.deleteMany()
await Product.deleteMany()
await Order.deleteMany()
const createdUsers = await User.insertMany(users)
const adminUser = createdUsers[0]._id
const sampleProducts = await products.map((product) => {
return { ...product, user: adminUser }
})
await Product.insertMany(sampleProducts)
console.log('Data Transmitted!'.green.inverse)
process.exit()
} catch (error) {
console.log(`${error}`.red.inverse)
process.exit(1)
}
}
const destroyData = async () => {
try {
await User.deleteMany()
await Product.deleteMany()
await Order.deleteMany()
console.log('Data Destroyed!'.red.inverse)
process.exit()
} catch (error) {
console.log(`${error}`.red.inverse)
process.exit(1)
}
}
if (process.argv[2] === '-d') {
destroyData()
} else {
importData()
}