I have managed to write a raw query that allows me to get all products inside a category, even the products inside the category's sub categories all the way down the tree.
I am using a raw query because I didn't know how to use objection's / knex api to do so.
The problem I am having is that I also want to join the related tables, in this case, for my products I have "products_meta" and "categories"
Even though I know I can simply modify the raw query to do that, I would like to know if there is a simpler way by using some objection/knex methods, like joinEager.
My code so far looks something like this: (the sql string below is just getting all products inside a category and its subcategories, this is not the problem)
const sql = `
with recursive get_subcols as(
select id
from collections
where "parentId" = ${req.params.id}
union all
select c.id
from collections as c
inner join get_subcols gs
on c."parentId" = gs.id
), get_all_collections as (
select id from collections
where id = ${req.params.id}
union
select * from get_subcols
), get_all_products as (
select p.*, c.name as collection
from products as p
join "collectionProducts" cp
on cp."productId" = p.id
join get_all_collections gac
on cp."collectionId" = gac.id
left join collections as c
on cp."collectionId" = c.id
)
select * from get_all_products;
`
const result = await Collection.knex().raw(sql)
const products = result.rows
My question is, is it possible to do something like this:
.joinEager('[meta, collections]')
to the results so I can also join the related tables in the result?