-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
48 lines (44 loc) · 1.04 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
const fs = require('fs');
const mongoose = require('mongoose');
const colors = require('colors');
const dotenv = require('dotenv');
//Load env variables
dotenv.config({ path: './config/config.env' });
//Load models
const Card = require('./models/Card');
//Connect to MongoDB
mongoose.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
});
//Read JSON files
const cards = JSON.parse(
fs.readFileSync(`${__dirname}/_data/card_data.json`, 'utf-8')
);
//Import into debug
const importData = async () => {
try {
await Card.create(cards);
console.log('Data Imported....'.cyan.inverse);
console.log('run');
process.exit();
} catch (err) {
console.error(err);
}
};
//Import into debug
const deleteData = async () => {
try {
await Card.deleteMany();
console.log('Data destroyed....'.red.inverse);
process.exit();
} catch (err) {
console.error(err);
}
};
if (process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === '-d') {
deleteData();
}