본문 바로가기

express

UdemyAPI_Define model schema

User Schema

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
	name: {
		type: String,
		required: [true, 'Please add a name']
	},
	email: {
		type: String,
		required: [true,'Please add an email'],
		unique: true,
		match: [
     	 /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
      	'Please add a valid email'
    	]
	},
	role: {
		type: String,
		enum: ['user', 'instructor'],
		defalut: 'user'
	},
	password: {
		type: String,
		required: [true, 'Please add a password'],
		minlength: 6,
		select: false
	},
	createdAt: {
		type: Date,
		default: Date.now
	}
});

module.exports = mongoose.model('User', UserSchema);

// this Schema doesnt have any authentications or permissions till now

Course Schema

const mongoose = require('mongoose');
const slugify - require('slugify');

const CourseSchema = new mongoose.Schema(
	{
		name: {
			type: String,
			required: [true, 'Please add a name'],
			unique: true,
			trim: ture,
			maxlength: [50, 'Name can not be more than 50 characters']
		},
		slug: String
		description: {
			type: String,
			required: [true, 'Please add a description'],
			maxlength: [500, 'Description can not be more than 500 characters']
		},
		photo: {
			type: String,
			default: 'no-photo.jgp'
		},
		user: {
			type: mongoose.Schema.ObjectId,
			ref: 'User',
			required: true
		}		
	},
	{
		toJSON: { virtuals: true},
		toObject: {virtuals: true}
	}
);

CourseSchema.pre('save', function(next) {
	this.slug = slugify(this.name, { lower: true });
	next();
});

CourseSchema.virtual('contents', {
	ref: 'Content',
	localField: '_id',
	foreignField: 'course',
	justOne: false
});

CourseSchema.virtual('reviews', {
	ref: 'Review'
	localField: '_id',
	foreignField: 'course',
	justOne: false
});

// Cascade Delete


module.exports = mongoose.model('Course', CourseSchema);

Content Schema

const mongoose = require('mongoose');

const ContentSchema = new mongoose.Schema({
	title: {
		type: String,
		trim: true,
		required: [true, 'Please add a course title']
	},
	description: {
		type: String,
		required: [true, 'Please add a description']
	},
	video: {
		type: String,
		default: 'wait !! not developed yet !!'
	}
	createdAt: {
		type: Date,
		default: Date.now
	},
	course: {
		type: mongoose.Schema.ObjectId,
		ref: 'Course',
		required: true
	},
	user: {
		type: mongoose.Schema.ObjectId,
		ref: 'User',
		required: true
	}
});

module.exports = mongoose.model('Content', ContentSchema);

Review Schema

const mongoose = require('mongoose');

const ReviewSchema = new mongoose.Schema({
	text: {
		type: String,
		required: [true, 'Please add some text']
	},
	rating: {
		type: Number,
		min: 1,
		max: 5,
		required: [true, 'Please add a rating between 1 and 10']
	},
	createdAt: {
		type: Date,
		default: Date.now
	},
	course: {
		type: mongoose.Schema.ObjectId,
		ref: 'Course',
		required: true
	},
	user: {
		type: mongoose.Schema.ObjectId,
		ref: 'User',
		required: true
	}
});

ReviewSchema.index({ bootcamp: 1, user: 1}, {unique: true });

module.exports = mongoose.model('Review', ReviewSchema);

 

CourseSchema.virtual('contents', {
        ref: 'Content',
        ~~
);

This virtaul is used to populate contents to course model

 

UserSchema.index({ bootcamp: 1, user: 1}, {unique: ture}); 
this make sure can make only 1 review per course

This make sure that user can make only 1 review per course.

'express' 카테고리의 다른 글

How does HTTP request looks like  (0) 2020.05.15
How do I do object CRUD?  (0) 2020.05.15
"Udemy" API by myself  (0) 2020.05.13
Udemy course spec/APIs  (0) 2020.05.13
How to manage of 1:N relationship? mongoose  (0) 2020.05.12