I seem to be stuck in the classic ORM problem and don't know how to handle it, so any help is welcome at this point.
Is there a way to get a pivot table in a hasManyThrough query? Better yet, apply some sort of filter or sort to it. Typical example
Table products
id,title
Table categories
id,title
table products_categories
productsId, categoriesId, orderBy, main
So, in the above scenario, let's say you want to get all product categories X (main = true) or you want to sort product categories using orderBy .
What is happening now is the first SELECT for products to get product data, the second SELECT on products_categories to get categoriesId and the final SELECT by category to get actual categories. Ideally, filters and sorting should apply to 2nd SELECT , e.g.
SELECT `id`,`productsId`,`categoriesId`,`orderBy`,`main` FROM `products_categories` WHERE `productsId` IN (180) WHERE main = 1 ORDER BY `orderBy` DESC
Another typical example would be the desire to order product images depending on which user wants them
so you will have a products_images table
id,image,productsID,orderBy
and you want
SELECT from products_images WHERE productsId In (180) ORDER BY orderBy ASC
Is it possible?
EDIT: Here is the link needed for the staging table to get what I need based on my schema.
Products.hasMany(Images, { as: "Images", "foreignKey": "productsId", "through": ProductsImagesItems, scope: function (inst, filter) { return {active: 1}; } });
Thing is a scope function giving me access to the final result, not to the staging table.