Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Next Steps

Daniel Breen
Daniel Breen
14,943 Points

Is there a way to query multiple models and use .destroy() once?

I've been using sequelize for a couple of months now on a very large application at work. Thanks for the informative video.

One problem I frequently run into is querying multiple records across separate models and deleting them in an efficient manner. In an older app, we used chainers, but those are now deprecated. Now we're stuck creating some callback hell. E.g., Post.destroy(where...).then(Video.destroy(where...)).then(SomethingElse.destroy(where)).then(User.destroy(where))

Ideally, I'd like to first find everything I want to delete, then delete it all with one or two destroy calls (destroy user content first, then the user)

var posts = Post.findAll... var videos = Video.findAll... var miscItems = SomethingElse.findAl... var user = Author.findById...

sequelize.destroy(posts, videos, miscItems).then(function(){sequelize.destroy(user)});

*Yes, this is all psuedo-code. The idea is to illustrate that I'd like to avoid several .then() calls and find the most efficient way to destroy records across multiple models that all share a property. In the case of the example: posts, videos, and miscItems would all have a user_id property that is equal to user.id.

1 Answer