2

I want to remove all members that a collection has , but I do not want to pass every member ID to .member() method . waterline documentation explain a way to delete specific members like :

await User.removeFromCollection(3, 'pets')
.members([99,98]);

I want to have sth like :

await User.removeFromCollection(3, 'pets')
.members(['*']);
SayJeyHi
  • 1,559
  • 4
  • 19
  • 39

1 Answers1

6

As far as I can tell, this should be done using .destroy() without a criteria.

Edit (2019-07-10): Added the empty curlies as per the comment from noobular

await User.destroy({});                // Removes all records from your User collection

await User.destroy({name:'Bill'};    // Removes all records from your User collection
                                        where the 'name' is 'Bill'

Docs: .destroy()

Update

After you pointed out I misread your question, I came up with this solution. The docs for .removeFromCollection() states that passing in an array of parent ID's will remove all children in the specified collection, but this does not seem to function as written.

I did however find a working solution for you using .replaceCollection().

await User.replaceCollection(3, 'pets', []);

OR

await User.replaceCollection(3, 'pets').members([]);

Passing in an empty array will replace the current array of associations with the empty array, clearing out the current associations.

Docs: .replaceCollection()

Community
  • 1
  • 1
Canis
  • 4,130
  • 1
  • 23
  • 27
  • I do not want to delete `user` it has many-to-many relation with my `pets` , I just want to delete relation – SayJeyHi Jan 22 '19 at 08:09
  • @Jafarrezaei Ah, I'm sorry. I misread your question. I'll update my answer. – Canis Jan 22 '19 at 08:12
  • 1
    thanks , actually today I solve this with passing needed members ids , but this is correct answer and accepted . – SayJeyHi Jan 22 '19 at 14:05
  • As of 7/7/19, you'll get an error with just destroy(). Sails will tell you to provide empty criteria, ie User.destroy({}). – noobular Jul 07 '19 at 17:26