1

Let's say I have got 2 models.

User and Clubs.

To simplify my problem let's say that User got only Id and Clubs got the ClubId and UserId so one club can have many different users.

Here is my problem :

When I delete for example User with Id 3, I want also to delete all Users from Clubs with this Id 3. How to make it happen?

CSharpBeginner
  • 1,625
  • 5
  • 22
  • 36

2 Answers2

8

Sails doesn't support cascading deletes yet, but you can handle this yourself:

User.destroy({id:3}).exec(function(err, users) {
   if (err) {return res.serverError();}
   var userIds = users.map(function(user){return user.id;});
   Club.destroy({user_id: userIds}).exec(function(err, clubs) {
      // do something
   });
});

destroy returns an array of all the objects that were destroyed. In your case, since you're searching by ID, only one would (hopefully) be returned, but if you were using criteria that could destroy several users at once you could still use the above code to wipe out all the related clubs.

sgress454
  • 24,870
  • 4
  • 74
  • 92
  • there can be many users in clubs for exame user with Id 3 can be in Club with ClubId 1 and ClubId2 so if i delete user with id 3 i hope 2 rows from Clubs model will be deleted too – CSharpBeginner Mar 17 '14 at 20:31
  • Yes, the code above will do that. Any club whose `user_id` is in the list of users returned from the `destroy` would be deleted. – sgress454 Mar 17 '14 at 20:52
  • Actually, re-reading your comment, I'm not sure I quite followed what you wanted. The code above will delete any clubs a deleted user belongs to, which maybe isn't what you wanted. If all you need is for the relationship between a user and their Clubs to be deleted along with the User record, you don't have to do anything: Sails handles that for you. It's only if you want to delete the related Club records that you would need the code above. – sgress454 Mar 17 '14 at 21:05
  • Thank you, so I got 2 question more, hope not bother you too much :( 1. In your code, this line do smth, when I just want to delete them i can just let it be empty? 2. If I also want to delete from another model for exampe Friends all users with that Id, how to add deleting also from another one model? – CSharpBeginner Mar 17 '14 at 21:15
  • 1) You can leave it empty, but if you're calling it from inside a controller you most likely will want to send a response there. 2) You can nest these `delete` calls indefinitely, but it can get hard to read, so it's better to use something like the [async library](https://github.com/caolan/async) to organize your asynchronous code into more manageable blocks. – sgress454 Mar 18 '14 at 01:15
  • So i got one last question Dear Scott Gress, As I know in Sails.js we got automatically operations like deleting in every model, so how to set that this User.destory function will be raised every time i delete anyone ? – CSharpBeginner Mar 18 '14 at 11:49
  • If you're using the current master branch of Waterline, and starting with the next patch release, you'll be able to use the model class method `afterDestroy (values, cb)` to do this. – sgress454 Mar 18 '14 at 18:02
  • Is cascading supported now? – Noitidart Dec 10 '18 at 01:57
6

You can get afterDestroy event after deleting any model. Simple demo as below

In comtroller :

 Company.destroy({id: 12346798})
    .exec(function(e,r){

          return res.json(r);
    });

In Company.js model :

afterDestroy: function(destroyedCompany, cb) {

    var ids = _.pluck(destroyedCompany, 'id');

    if(ids && ids.length){

         Department.destroy({company_id: ids})
        .exec(function(e,r){
            CompanyArticle.destroy({company_id: ids}).exec(cb);
        });

    }
    else{
        cb();
    }

}
Nishchit
  • 18,284
  • 12
  • 54
  • 81