Last active
October 10, 2018 09:55
-
-
Save salmanx/48b14d2efb90644b5ee740cbc772440f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const mongoose = require("mongoose"); | |
mongoose.connect("mongodb://localhost/playground", { useNewUrlParser: true }) | |
.then(() => console.log("Mongo db is connected!")) | |
.catch((err) => console.error("Could not connect to db ...", err)); | |
const courseSchema = new mongoose.Schema({ | |
name: { type: String, required: true }, | |
author: { type: String, required: true, minlength: 3, maxlength: 255 }, | |
category: { | |
type: String, | |
required: true, | |
enum: ['web', 'mobile', 'networking'], // predefined | |
lowercase: true, // turns the value to lowercase b4 save | |
trim: true | |
}, | |
// tags: [String], // array of string, for validation required: true will not work since empty array is a value | |
tags : { | |
type: Array, | |
validate: { | |
validator: function(v){ | |
return v && v.length > 0; // check v exists if client return tags as null | |
}, | |
message: "You must give a tag for a course" | |
} | |
}, | |
date: { type: Date, default: Date.now }, | |
isPublished: Boolean, | |
price: { | |
type: Number, | |
// if course is published, then price is required | |
required: function() { return this.isPublished; }, | |
min: 10, | |
max: 20, | |
// get: v => Math.round(v), // we can also user getter/setter | |
// set: v => Math.round(v) | |
}, | |
// for model relationship | |
// author: { | |
// type: mongoose.Schema.Types.ObjectId, | |
// ref: "Author" | |
// } | |
}); | |
const Course = mongoose.model("Course", courseSchema); | |
async function createCourse(){ | |
let course = new Course({ | |
name: "Vue js Course", | |
author: "John Doe", | |
category: "web", | |
tags: ['vue', 'js'], | |
isPublished: true, | |
price: 14.40 | |
}); | |
try{ | |
let result = await course.save(); | |
console.log(result); | |
} | |
catch(ex){ | |
console.log(ex.message); | |
for (let field in ex.errors) | |
console.log(ex.errors[field].message) | |
} | |
} | |
// createCourse(); | |
async function getCourses(){ | |
// eq (equal) | |
// ne (not equal) | |
// gt (greater than) | |
// gte (greater than or equal to) | |
// lt (less than) | |
// lte (less than or equal to) | |
// in | |
// nin (not in) | |
// for pagination | |
// let pageNumber = 2; | |
// let pageSize = 10; | |
let courses = await Course.find({ author: 'John Doe', isPublished: true}) | |
// .find({price: { $lte: 10, $gte: 20 }}) | |
// .find({ price: { $in: [10, 20, 30] }}) | |
// .find().or([ { author: "John" }, { isPublished: true }]) | |
// .find().and([ { author: "John" }, { isPublished: true }]) | |
// .find({ author: /pattern/ }) // regular expression | |
.sort({ name: 1 }) | |
.select({ name: 1, tags: 1 }) | |
.limit(10); | |
// .count() | |
// .skip((pageNumber - 1) * pageSize) | |
// .limit(pageSize) | |
console.log(courses); | |
} | |
getCourses(); | |
async function updateCourse(id){ | |
let course = await Course.findById(id); | |
if(!course) return; | |
course.name = "Angular Course"; | |
// another approach | |
// course.set({ | |
// name: "Angular Course" | |
// }) | |
//let result = await course.save(); | |
console.log(course); | |
} | |
// updateCourse('5bbd780ddd48394cc7328eb5'); | |
async function deleteCourse(id){ | |
let course = await Course.deleteOne({_id: id}); | |
console.log(course); | |
} | |
//deleteCourse('5bbd780ddd48394cc7328eb5'); | |
// router.post('/', async (req, res) => { | |
// let course = new Course({ | |
// // req.body | |
// }); | |
// let result = await course.save(); | |
// return res.status(201).send(result); | |
// }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment