-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9398a7b
commit bf18c50
Showing
6 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const CourseSchema = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
trim: true, | ||
required: [true, 'Please add a course title'] | ||
}, | ||
description: { | ||
type: String, | ||
required: [true, 'Please add a description'] | ||
}, | ||
weeks: { | ||
type: String, | ||
required: [true, 'Please add number of weeks'] | ||
}, | ||
tuition: { | ||
type: Number, | ||
required: [true, 'Please add a tuition cost'] | ||
}, | ||
minimumSkill: { | ||
type: String, | ||
required: [true, 'Please add a minimum skill'], | ||
enum: ['beginner', 'intermediate', 'advanced'] | ||
}, | ||
scholarshipAvaliable: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
date: { | ||
type: Date, | ||
default: Date.now | ||
}, | ||
bootcamp: { | ||
type: mongoose.Schema.ObjectId, | ||
ref: 'Bootcamp', // reference the Bootcamp model | ||
required: true | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model('Course', CourseSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 Bootcamp = require('./models/Bootcamp'); | ||
const Course = require('./models/Course'); | ||
|
||
// Connect to DB | ||
mongoose.connect(process.env.MONGO_URI, { | ||
useNewUrlParser: true, | ||
useCreateIndex: true, | ||
useFindAndModify: false, | ||
useUnifiedTopology: true | ||
}); | ||
|
||
// Read JSON files | ||
// dirname = curernt directory name | ||
const bootcamps = JSON.parse(fs.readFileSync(`${__dirname}/_data/bootcamps.json`, 'utf-8')); | ||
const courses = JSON.parse(fs.readFileSync(`${__dirname}/_data/courses.json`, 'utf-8')); | ||
|
||
|
||
// Import into DB | ||
const importData = async () => { | ||
try { | ||
await Bootcamp.create(bootcamps); | ||
await Course.create(courses); | ||
console.log('Data Imported...'.green.inverse); | ||
process.exit(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
// Delete Data | ||
const deleteData = async () => { | ||
try { | ||
await Bootcamp.deleteMany(); | ||
await Course.deleteMany(); | ||
console.log('Data Destroyed...'.red.inverse); | ||
process.exit(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
// When seeder is run, allow argument to know whether to us Import or Delete function | ||
if (process.argv[2] === '-i') { // terminal: node seeder -i | ||
importData(); | ||
} | ||
else if (process.argv[2] === '-d'){ // terminal: node seeder -d | ||
deleteData(); | ||
} |