mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 15:47:48 +01:00
28 lines
658 B
JavaScript
28 lines
658 B
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const TodoList = mongoose.model('TodoList');
|
|
const { Schema } = mongoose;
|
|
|
|
const TodoSchema = Schema({
|
|
text: {
|
|
type: String,
|
|
required: true,
|
|
},
|
|
list: { type: Schema.Types.ObjectId, ref: 'TodoList', required: true },
|
|
completed: { type: Boolean, default: false },
|
|
});
|
|
|
|
TodoSchema.pre('save', async function () {
|
|
const list = await TodoList.findById(this.list);
|
|
list.todos.push(this.id);
|
|
await list.save();
|
|
});
|
|
|
|
TodoSchema.pre('remove', async function () {
|
|
const list = await TodoList.findById(this.list);
|
|
list.todos.remove(this.id);
|
|
await list.save();
|
|
});
|
|
|
|
mongoose.model('Todo', TodoSchema);
|