mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 15:47:48 +01:00
37 lines
687 B
JavaScript
37 lines
687 B
JavaScript
const mongoose = require('mongoose');
|
|
const slugify = require('slugify');
|
|
|
|
const { Schema } = mongoose;
|
|
|
|
const TodoListSchema = Schema({
|
|
name: {
|
|
type: String,
|
|
required: true,
|
|
},
|
|
slug: {
|
|
type: String,
|
|
required: true,
|
|
lowercase: true,
|
|
},
|
|
todos: [{ type: Schema.Types.ObjectId, ref: 'Todo' }],
|
|
});
|
|
|
|
TodoListSchema.pre('validate', function (next) {
|
|
if (!this.slug) {
|
|
this.slugify();
|
|
}
|
|
next();
|
|
});
|
|
|
|
TodoListSchema.methods.slugify = function () {
|
|
this.slug = slugify(this.name);
|
|
};
|
|
|
|
TodoListSchema.pre('remove', async function () {
|
|
this.todos.forEach(async (todo) => {
|
|
await todo.remove();
|
|
});
|
|
});
|
|
|
|
mongoose.model('TodoList', TodoListSchema);
|