-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
62 lines (55 loc) · 1.65 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
59
60
61
62
import Order from './models/order.js';
import Product from './models/product.js';
import User from './models/user.js';
import connectDB from './config/db.js';
import dotenv from 'dotenv';
import products from './data/products.js';
import users from './data/users.js';
dotenv.config();
// We have to do this again because it is no way
// connected to our server, as it's completely seperate
connectDB();
// It is important that the process is only run
// The firstime the app is deployed
// else ensure that the data in the staging/prod is not necessary
const seedData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
const createdUsers = await User.insertMany(users);
// since the admin user was the first to be inserted
// above
const adminUser = createdUsers[0]._id;
const sampleProducts = products.map((product) => ({
...product,
user: adminUser,
}));
await Product.insertMany(sampleProducts);
console.log('Data Successfully Seeded'.green.inverse);
process.exit();
} catch (error) {
console.error('An error occured during Seeding'.red.inverse);
process.exit(1);
}
};
const destroyData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
console.log('Data Successfully Destroyed'.red.inverse);
process.exit();
} catch (error) {
console.error('An error occured during Seeding'.red.inverse);
process.exit(1);
}
};
// this is used for passing termaials
// eg seeddata -d
// npm install <pkg_name> -d
if (process.argv[2] === '-d') {
destroyData();
} else {
seedData();
}