I have Products and Tags which have a Many to Many relationship. Tags have a type field I would like to have the ability to filter and sort products by missing Tag type. (Give me all the products that do not have a Tag of type X)
Is the best way to create a smart field on Products and put a filter on that smart field ?
I understand your question. You want kind of a not in operator ?
That could be a possibility. For example you override the behaviour of not_equal filter but I guess it would require complexe queries to compute all the ids that matchs.
{
field: 'fakeMissingTags',
isFilterable: true,
type: 'String',
get: (product) => {
// you can compute something like a string of missing tags
return 'we dont care';
},
filter({ condition, where }) {
const value = !!condition.value && condition.value;
switch (condition.operator) {
// Choose whatever filter you want to implement
case 'not_equal':
const complexeQueriesToFindProdutsIds = await models.products
.findAll({
include: [{
model: models.tags,
as: "tagsThroughProductTags",
where: { [Op.not]: { tagType: 0 } },
}],
});
const ids = complexeQueriesToFindProdutsIds.map(product => product.id);
// Feed it with the array of ids.
return { id: { [Op.in]: ids } };
// ... implement the operator you want: equal for example pour the presence comportement
default:
return null;
}
},
}
async filter({ condition, where }) {
switch (condition.operator) {
case 'not_contains':
const complexeQueriesToFindProdutsIds = await models.products.findAll({
include: [{ model: models.tags, as: "tagsThroughProductTags", where: {
[Op.not]: {
tagType: 0
}
}
}]
});
console.log({complexeQueriesToFindProdutsIds})
let productIdsToDisplay = complexeQueriesToFindProdutsIds.map(product => product.id)
return { id: { [Op.In]: complexeQueriesToFindProdutsIds } };
// ... implement the operator you want: equal for example pour the presence comportement
default:
return true;
}
},
But am getting the following error:
Unexpected error: invalid input syntax for integer: “[object Object]”
Which I believe is caused by the return value of the “not_contains” case.
I’m not sure I understand what the return value should be, If I return a single integer it works fine and the UI shows only one record with the id of the returned value, but If I pass an array of integers [607, 608].
I get this error:
Unexpected error: Support for literal replacements in the `where` object has been removed.
I would like to either display or not display a list of records.
Could you shed some light on what is the expected return value of the “not_contains” block ?