mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 15:47:48 +01:00
29 lines
605 B
JavaScript
29 lines
605 B
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const { Schema } = mongoose;
|
|
|
|
const TodoListSchema = Schema({
|
|
name: {
|
|
type: String,
|
|
required: true,
|
|
},
|
|
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }],
|
|
});
|
|
|
|
TodoListSchema.pre('remove', async function () {
|
|
this.todos.forEach(async (todo) => {
|
|
await todo.remove();
|
|
});
|
|
});
|
|
|
|
TodoListSchema.methods.toJson = function () {
|
|
const todos = this.populated('todos') ? this.todos.map(todo => todo.toJson()) : this.todos;
|
|
return {
|
|
id: this._id.toString(),
|
|
name: this.name,
|
|
todos,
|
|
};
|
|
};
|
|
|
|
mongoose.model('TodoList', TodoListSchema);
|