I'm building the backend of my e-shop using Sequelize. I have defined models for clients, products, orders, etc. I want any instance of these models to be "trashable", i.e. just like deleted instances, I want trashed instances not to appear in the results of calls to <Model>.findAll(), <ModelA>.get<ModelB>s() or even <ModelA>.findAll({ include: [{ model: <ModelB> }]. But I still want to be able to query those trashed instances (and restore them eventually).
The solution I'm considering is adding a .status property to my models (ENUM('active', 'trashed')), and then adding a beforeFind hook to all my models to filter out instances that are not active unless the query is explicitly searching for id(s) or trashed items specifically. I'm worried by the potential complexity of the queries passed to my beforeFind hook, though. How to detect what the queries are searching for and how should they be modified to filter-out trashed items? And there are probably other shortcomings to this solution I didn't even consider…
Before giving implementation a try, I wanted to know if others had given this pattern a try, how they did it and what difficulties they faced. Thanks :-)
via Louis-Rémi
No comments:
Post a Comment