|
const mongoose=require("mongoose");
|
|
const validatorId=require('mongoose-id-validator');
|
|
|
|
var taskSchema=new mongoose.Schema({
|
|
name:{ type: String, required: true },
|
|
description:{ type: String, default:"" },
|
|
parent:{ type: mongoose.Schema.Types.ObjectId, ref: 'Task' }
|
|
});
|
|
taskSchema.plugin(validatorId);
|
|
taskSchema.pre("findOneAndUpdate",function(next){
|
|
if(
|
|
this._update
|
|
&& this._update.hasOwnProperty("parent")
|
|
&& this._update.parent.toString()===this._conditions._id.toString()
|
|
){
|
|
next(new Error("parent must be different of _id."));
|
|
}else{
|
|
next();
|
|
}
|
|
});
|
|
|
|
module.exports=mongoose.model("Task",taskSchema);
|