-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.js
49 lines (45 loc) · 1.15 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
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const users = require('./data/users');
const products = require('./data/products');
const User = require('./models/user');
const Order = require('./models/order');
const Product = require('./models/product');
dotenv.config();
const connectDB = require('./config/db');
connectDB();
const importData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
const createdUser = await User.insertMany(users);
const adminUser = createdUser[0]._id;
const sampleProducts = products.map((product) => ({
...product,
user: adminUser,
}));
await Product.insertMany(sampleProducts);
console.log('Data Imported');
process.exit();
} catch (error) {
console.log(`${error}`);
process.exit(1);
}
};
const destroyData = async () => {
try {
await Order.deleteMany();
await Product.deleteMany();
await User.deleteMany();
process.exit();
} catch (error) {
console.log(`${error}`);
process.exit(1);
}
};
if (process.argv[2] === '-d') {
destroyData();
} else {
importData();
}