-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
44 lines (36 loc) · 942 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
44
const fs = require('fs');
const dotenv = require('dotenv');
const { Product } = require('./models/ProductModel');
const dbConnection = require('./config/connectToDB');
require('colors');
dotenv.config();
// connect to DB
dbConnection.connectToDB();
// Read data
const products = JSON.parse(fs.readFileSync('./product.json'));
// Insert data into DB
const insertData = async () => {
try {
await Product.create(products);
console.log('Data Inserted'.green.inverse);
process.exit();
} catch (error) {
console.log(error);
}
};
// Delete data from DB
const destroyData = async () => {
try {
await Product.deleteMany();
console.log('Data Destroyed'.red.inverse);
process.exit();
} catch (error) {
console.log(error);
}
};
// node seeder.js -d
if (process.argv[2] === '-i') {
insertData();
} else if (process.argv[2] === '-d') {
destroyData();
}