-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
69 lines (63 loc) · 1.93 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
63
64
65
66
67
68
69
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { mongoose } from 'mongoose';
import dotenv from 'dotenv';
import Bootcamp from './models/Bootcamp.js';
import Course from './models/Course.js';
import User from './models/User.js';
import Review from './models/Review.js';
dotenv.config({ path: './config/config.env' });
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Connect to mongodb database
mongoose.set('strictQuery', false);
mongoose.connect(`${process.env.MONGO_URI}/${process.env.DB_NAME}`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Read data from the file
const bootcamps = JSON.parse(
fs.readFileSync(`${__dirname}/_data/bootcamps.json`, 'utf-8')
);
const courses = JSON.parse(
fs.readFileSync(`${__dirname}/_data/courses.json`, 'utf-8')
);
const users = JSON.parse(
fs.readFileSync(`${__dirname}/_data/users.json`, 'utf-8')
);
const reviews = JSON.parse(
fs.readFileSync(`${__dirname}/_data/reviews.json`, 'utf-8')
);
// Import all data from the file to the database
const importData = async () => {
try {
await Bootcamp.create(bootcamps);
await Course.create(courses);
await User.create(users);
await Review.create(reviews);
console.log('Data imported successfully...');
process.exit();
} catch (error) {
console.log(error);
}
};
// Delete all the data from the database
const deleteData = async () => {
try {
await Bootcamp.deleteMany();
await Course.deleteMany();
await User.deleteMany();
await Review.deleteMany();
console.log('Data deleted successfully...');
process.exit();
} catch (error) {
console.log(error);
}
};
// Reading flags from the terminal and calling functions accordingly
if (process.argv[2] === 'import' || process.argv[2] === '-i') {
importData();
} else if (process.argv[2] === 'delete' || process.argv[2] === '-d') {
deleteData();
}