Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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