Cascade Delete though remove middleware

606 views
Skip to first unread message

jonah...@gmail.com

unread,
Jul 2, 2015, 12:56:54 PM7/2/15
to mongoo...@googlegroups.com

I'm wondering if Mongoose.js supports cascade deletes in the the pre or post remove middleware. For instance if I have a TODO app and a user deleted there account would it also delete everything associated with that user account if I had something like this set up?


userSchema = new Schema({email: String})

userSchema.post('remove', function() {
  TodoList = require('todo_list');
  TodoList.remove({user: this._id} )
})

mongoose.model("User", userSchema)


todoSchema = new Schema({user: {type:ObjectId, ref:"User"}})

todoSchema.post('remove', function() {
  TodoItem = require('todo_item');
  TodoItem.remove({list: this._id} )
});

mongoose.model("ToDoList", todoSchema)


todoItemSchema = new Schema({list: {type:ObjectId, ref:"ToDoList"}})

todoItemSchema.post('remove', function() {
  console.log('DELETED') // <<---- this should be triggered when the user is deleted
});

mongoose.model("ToDoItem", todoItemSchema)
 
Just to sum things up if a user deletes their account the ToDoList associated with that account are deleted & since the ToDoList is deleted the ToDoItems associated with the list are deleted.

Thanks.

Reply all
Reply to author
Forward
0 new messages