I have two tables: tbl_product
, tbl_product_category
I want to make an API that take a keyword and return all product and product category that match with the keyword.
If the result from tbl_product
then it also return that it is product.
If the result from tbl_product_category
then it also return that it is category.
tbl_product_catagory.findAll({
raw: true,
attributes: [[sequelize.literal("catagory_name"), "type"]],
include: [{
model: tbl_product,
attributes: [[sequelize.literal(["product_name"]), "name"]],
required: false,
where: {
product_name: {
[Op.like]: "%" + data.word + "%"
}
}
}]
})
According to this answer and this answer we can conclude that sequelize don't have good support for unions.
I know this is very old, but i like to show a turnaround using nodejs for this issue because sequelize yet do not have union support. Is very simple but sometimes we don't realize this:
Example:
Promise.all([
Blog.findAll({ include: { model: Author, required: true } }),
Blog.findAll({ include: { model: AnonymInfo, required: true } }),
]).then((modelReturn) => resolve(modelReturn.flat()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With